Suppose you want to know if (the class of) a particular object overrides a virtual method. For an example of this see OpenJDK's Thread.isCCLOverriden() (line 1573).
In Java the obvious way to do this would be to use reflection. On the CLR there is another way that is both more accurate1 and more efficient.
Here's the MSIL method from IKVM's java.lang.Thread.isCCLOverridden() implementation:
.method private hidebysig static bool isCCLOverridden(class java.lang.Thread A_0) cil managed
ldftn instance class java.lang.ClassLoader java.lang.Thread::getContextClassLoader()
ldvirtftn instance class java.lang.ClassLoader java.lang.Thread::getContextClassLoader()
ldftn instance void java.lang.Thread::setContextClassLoader(class java.lang.ClassLoader)
ldvirtftn instance void java.lang.Thread::setContextClassLoader(class java.lang.ClassLoader)
Instead of running a zillion instructions and accessing a lot of cold data for reflection, this simply leverages the information the JIT already has about virtual methods.
Here's the x86 code this turns into:
To get an idea what JIT_VirtualFunctionPointer does, take a look at the Shared Source CLI.
On the CLR, in the common case it only executes about 40 instructions.
The downside to this method is that it only works if you have an object instance. Although you could use FormatterServices.GetUninitializedObject() to create an instance.
Why Optimize This?
In the OpenJDK code, isCCLOverridden() is only called if a SecurityManager is installed, but I wanted to use it always to avoid calling getContextClassLoader() during thread construction, because that would trigger the system class loader to be constructed and I my long term goal for IKVM is to make initialization more lazy to reduce the huge startup overhead.
1This method is more accurate (on the CLR) because you don't need to worry about non-virtual methods or virtual methods that are new (and hence don't override the base class virtual method) or explicit overrides that override a method but have a different name.
Update: See this article for a caveat.