This is a mirror of official site: http://jasper-net.blogspot.com/

Virtual Method Dispatch and Object Layout Changes in CLR 4.0

| Tuesday, March 20, 2012
As part of the upcoming Pro .NET Performance book, there’s quite a bit of research we need to do on various facets of CLR internals. During my research for the Type Internals chapter I discovered a change in CLR object layout and virtual method dispatch as of CLR 4.0 – possibly not the most exciting of changes but one that invalidates most of the existing material on this subject, such as the popular JIT and Run article or theAdvanced .NET Debugging book.

First, a quick overview of how reference type instances are laid out on the heap. Suppose that we have anEmployee class with two instance fields, _name and _id, as well as a virtual method called Work. On a 32-bit managed heap, an Employee instance occupies 16 bytes, and has the following layout (each cell = 4 bytes):

Object Header Word (Sync Block Index)
Method Table Pointer
The _name field
The _id field

The method table pointer points to Employee’s method table, which contains, among other things, code addresses for Employee’s methods. On CLR 2.0, the method table has roughly the following layout:

      Flags, Size, EEClass, Module Ptr, etc.
      Interface Map Pointer
+0x28 Object.ToString
+0x2c Object.Equals
+0x30 Object.GetHashCode
+0x34 Object.Finalize
+0x38 Employee.Work
      Employee..ctor 
Interface MTs implemented by Employee 

Read more: DZone - Net zone
QR: Inline image 1

Posted via email from Jasper-net

0 comments: