How to get layout of managed object conveniently? #94481
-
During development of vm code, it's often necessary to confirm object layout. Currently I'm relying on checks in corelib.h, which is inconvenient and can miss other architectures. @jkotas any advise? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
We have been trying to minimize the number of mirrored structures and keep the data structures that need to be mirrored simple. corelib.h is about 2x smaller compared to what it was in .NET Core 1.0 days. There is no silver bullet for computing the matching layouts for complex data structures. Both the managed and unmanaged field layout algorithms are architecture specific. |
Beta Was this translation helpful? Give feedback.
-
The sequential field layout is simple. The auto layout - that everything that contains GC references uses - is reordering the fields to maximize packing (primitive fields of the same size are reordered together) and improve GC reference locality (all GC references are reordered together). |
Beta Was this translation helpful? Give feedback.
-
You can create custom IL to enumerate all fields and then get their offsets and sizes, that's something done here for example: If you copy that code and play around with it, you'll see it works OK for |
Beta Was this translation helpful? Give feedback.
https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEUCuA7AHwAEAmARgFgAoagYjxwBtGBDYRmAAgBMBLAZzYdq1AJJ5+ABxhgMAOgASMFpIAUeGAHdOAFRj8MqgJRGA3CKpEAzJ1K79GagG9qnN7ZsRgAKxkZOLJwAvJwamsbmVO4enF6+spzAwaFaEa7u1pziGAAKGFCcYMlkkdGZ2XkF3MkkpRk2vHj+XCFWdW6ZwBAQjJwAZsl9LIz8MO0xXT2cAObJ+Thj1AC+QA===