0% found this document useful (0 votes)
38 views17 pages

Object Internals

This document provides an overview of the internals of .NET objects and the Common Language Runtime (CLR) implementation known as Rotor. It discusses how managed objects are represented in the runtime, how method tables store type metadata and code pointers, how external and static methods are implemented, how strings and arrays are laid out in memory, and how synchronization is achieved using monitors. The source code for many core CLR components is written in C++.

Uploaded by

Sajal Mitra
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
38 views17 pages

Object Internals

This document provides an overview of the internals of .NET objects and the Common Language Runtime (CLR) implementation known as Rotor. It discusses how managed objects are represented in the runtime, how method tables store type metadata and code pointers, how external and static methods are implemented, how strings and arrays are laid out in memory, and how synchronization is achieved using monitors. The source code for many core CLR components is written in C++.

Uploaded by

Sajal Mitra
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 17

Object Internals

Tomá š M a t ou š e k
&
L a d i s l a v P r oš e k

tmd.h a v i t.c z /te a c h i n g /c s h a r p .h tm

Advanced C # and . N E T p r o g r am m i ng
Introduction to SSCLI – R otor
• o n e o f t h e C L I im p le m e n t a t io n s
• s o u r c e c o d e a va i l a b l e
• b u i l t u s i n g n o n -t r i vi a l s c r i p t s ( m u l t i -p h a s e b u i l d )

• in t e r e s t in g s o u r c e c o d e d ir e c t o r ie s :
– c l r /s r c
• /v m – E x e c u ti o n E n g in e ( C + + )
• /f j i t – J I T te r ( C + + )
• /c s h a r p – C # c o mp i l e r ( C + + )
• /i l a s m – I L A s s e mb l e r to o l ( C + + )
• /i l da s m – I L D i s a s s e mb l e r to o l ( C + + )
• /b c l – B a s e C l a s s L ib r a r y ( C # )

– f x /s r c – s o m e .N E T F r a m e w o r k m a n a g e d l i b r a r i e s ( C # )
– m a n a g e d lib r a r ie s – a d d it io n a l m a n a g e d lib r a r ie s ( C # )
– js c r ip t – J S c r ip t c o m p ile r ( C # )

Advanced C # and . N E T p r o g r am m i ng
E x te rna l M e th ods
• m e t h o d s im p le m e n t e d o u t s id e a d e c la r in g a s s e m b ly
• d e c la r e d
– w it h o u t a b o d y
– u s in g m o d if ie r e x t e r n ( a C # k e y w o rd )
public extern void ExternalMethod();

• a t a r g e t s im p le m e n t a t io n d e f in e d b y c u s t o m a t t r ib u t e s
– D llI m p o r t A t t r ib u t e
• b in d in g t o a n u n m a n a g e d s t a t ic e n t r y p o in t o f a s p e c if ie d D L L
• c o n v e r t e d t o P /I n v o k e c a l l b y C # c o m p i l e r
[DllImport("kernel32.dll", EntryPoint="GetDiskFreeSpaceEx")]
public static extern bool GetDiskFreeSpace(…);

– M e t h o d I m p lA t t r ib u t e
Rotor:
• b in d in g t o a n E E in t e r n a l m e t h o d v m /e c a l l .c p p
• u s e d i n B C L ( e .g . S y s t e m .O b j e c t .G e t H a s h C o d e m e t h o d )
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public extern override int GetHashCode();

Advanced C # and . N E T p r o g r am m i ng
Rotor:

M a na g e d O b j e ct R e p re s e nta tion
b c l /ob j e c t.c s
v m /ob j e c t

• t w o p o i n t s o f vi e w o n a m a n a g e d o b je c t
– a m a n a g e d w o r ld v ie w
• th r o u g h c l a s s S y s te m.O b j e c t i n B C L
– E E v ie w
• th r o u g h c l a s s Object

• o b je c t la y o u t
– a s y n c b lo c k in d e x
• a n i n de x to E E i n te r n a l ta b l e o f s y n c b l o c k s
• a s s o c i a te s a ddi ti o n a l da ta w i th th e o b j e c t
Object

– s u c h a s a s y n c h r o n iz a t io n lo c k sync block index

– a p o in t e r t o a m e t h o d t a b le MethodTable *
• a n i n s ta n c e o f c l a s s M eth o d T a bl e component
• o n e p e r l o a de d ty p e count

– c o m p o n e n t c o u n t data

• v a l i d o n l y f o r a r r a y s a n d s tr i n g s

– s o m e b it s o f s y n c b lo c k a n d m e t h o d t a b le p o in t e r
e x p l o i t e d f o r o t h e r p u r p o s e s ( e .g . G C )
reference

Advanced C # and . N E T p r o g r am m i ng
Rotor:

M e th od T a b l e Conte nt
v m /c l a s s
v m /p re s tu b

• a n o b je c t s iz e ( f o r G C )
– B a s e S iz e + C o m p o n e n t S iz e * C o m p o n e n t C o u n t

• a p o i n t e r t o a n EEClass i n s t a n c e
– r u n -t i m e m e t a d a t a MethodTable
– a llo c a t e d s e p a r a t e ly t o m a k e component

w o r k in g s e t s m a lle r
size
...
base size thunk

• v-t a b l e s l o t s EEClass * jitted


native code
– i n c l u d i n g n o n -v i r t u a l s a n d s t a t i c s
...
interceptor
– i n i t i a l i z e d w i t h stubcalls v-table stub

• i n v o k e th e J I T te r
EEClass
stubcall jitter helper
– th u n k EEClass *parent

• a n i n di r e c ti o n a l l o w i n g n a ti v e MethodTable *
c o de r e c l a i ma ti o n
FieldDesc list
– in t e r c e p t o r s t u b s MethodDesc list
• r e mo ti n g , p r o f i l e r , s e c u r i ty , … ...

Advanced C # and . N E T p r o g r am m i ng
Sy nch roniz a tion v ia M onitors
• c la s s S y s t e m . T h r e a d in g . M o n it o r
– s t a t ic s : E n t e r , T r y E n t e r , E x it , W a it , P u ls e , P u ls e A ll
– a ll t a k e a n o b je c t t r e a t e d a s a m o n it o r
• C # k e y w o r d lo c k
lock(obj) { /* critical section */ }

– is e q u iv a le n t t o
Monitor.Enter(obj);
try
{
/* critical section */
}
finally
{
Monitor.Exit(obj);
}

• lo c k is a p a r t o f a la z ily a llo c a t e d S y n c B lo c k s t r u c t u r e
Object g_pSyncTable SyncBlock
sync block index Object * SyncBlockEntry * lock
...
MethodTable *
Advanced C # and . N E T p r o g r am m i ng
Rotor:

String s
b c l /s tri n g .c s
v m /ob j e c t.h
v m /c om s tri n g

• t w o p o i n t s o f vi e w o n a s t r in g o b je c t v m /s tri n g l i te ra l m a p

– m a n a g e d : S y s t e m .S t r i n g ( s u b c l a s s o f S y s t e m .O b j e c t )
– E E : StringObject ( s u b c l a s s o f O b j e c t )

• d a t a i n t e r n a l l y e n d s w i t h a \0 c h a r a c t e r
– a ddr e s s e s i n te r o p e r a b i l i ty w i th n a ti v e O S A P I
– h i dde n f r o m o u ts i de
– a s tr i n g c a n c o n ta i n \0 i n th e mi ddl e
StringObject
sync block index

• le n g t h < c a p a c it y ≤ 2 * le n g t h
MethodTable *

– de p e n ds o n th e w a y h o w it is b u ilt u p
capacity

length

• s t r in g s a r e im m u t a b le characters
– mo r e r e f e r e n c e s c a n p o i n t to a s i n g l e s tr i n g o b j e c t
– c o n s e rv e s a s p a c e
– ti me f o r s p a c e tr a de -o f f reference

Advanced C # and . N E T p r o g r am m i ng
Rotor: Interning

String Inte rning


b c l /s tri n g .c s
v m /s tri n g l i te ra l m a p

• in t e r n a l E E h a s h t a b le
– h o ld s r e f e r e n c e s t o s o m e s t r in g s
– t h e s e s t r i n g s a r e s a i d t o b e interned

• im p lic it in t e r n in g
– a ll s t r in g lit e r a ls in t e r n e d d u r in g J I T c o m p ila t io n

• e x p lic it in t e r n in g
– s t r in g S t r in g . I n t e r n ( s t r in g ) m e t h o d
• a d d s a s t r in g t o t h e in t e r n a l h a s h t a b le if n o t t h e r e y e t

– s t r in g S t r in g . I s I n t e r n e d ( s t r in g ) m e t h o d
• r e t u r n s in t e r n e d s t r in g h a v in g t h e s a m e v a lu e a s t h e p a s s e d o n e
• n u ll if t h e r e is n o s u c h o n e
• u s e d f o r f a s t s w i t c h i n g o v e r s t r i n g s i n C # 1 .0

Advanced C # and . N E T p r o g r am m i ng
String Lite ra l s
• s t o r e d in m e t a d a t a s t r e a m o n e b y o n e
• lo a d e d b y ld s t r I L in s t r u c t io n
– o f f s e t in t h e m e t a d a t a s t r e a m is a p a r t o f t h e in s t r u c t io n
– lo a d s a r e f e r e n c e t o a s t r in g o n e v a lu a t io n s t a c k

• w h e n a m e t h o d is b e in g c o m p ile d
– E E in t e r n a l h a s h t a b le is lo o k e d u p f o r e a c h s t r in g lit e r a l
– n o t fo u n d
⇒ a n e w o n e i s a l l o c a te d o n ma n a g e d h e a p
⇒ i ts v a l u e i s c o p i e d f r o m me ta da ta s tr e a m w i th p o s s i b l e c o n v e r s i o n ( e n di a n i ty )
⇒ th e l i te r a l i s i n te r n e d

Advanced C # and . N E T p r o g r am m i ng
Rotor:

String B uil de r
b c l /s tri n g b u i l d e r.c s
v m /ob j e c t.h
v m /c om s tri n g b u f f e r

• u s e d f o r g e n e r a t in g s t r in g s e f f ic ie n t ly

• t w o p o i n t s o f vi e w o n a m u t a b le s t r in g o b je c t
– S y s te m.T e x t.S tr i n g B u i l de r ( s u b c l a s s o f S y s te m.O b j e c t)
– E E : StringBufferObject ( s u b c l a s s o f Object)

• m o d if ie s a w r a p p e d s t r in g
– e n s u r e s it is n o t in t e r n e d n o r r e f e r e n c e d b y o t h e r o b je c t s
⇒ c o n s t r u c t o r t a k in g a s t r in g m a k e s a c o p y StringBufferObject
sync block index

• c o n ve r s i o n t o i m m u t a b l e S y s t e m . S t r i n g
MethodTable *

– v ia T o S t r in g ( ) m e t h o d
StringObject *

– d o e s n ’t d o a c o p y u n le s s
copy on write

• c a p a c i ty > 2 * l e n g th ( s h o r te n s th e r e s u l ti n g s tr i n g ) max. capacity

– s e t s c o p y -o n -w r i t e f l a g
reference

Advanced C # and . N E T p r o g r am m i ng
Rotor: A rra y s

A rra y s
b c l /a rra y .c s
v m /ob j e c t.h
v m /c l a s s .h
• b a s e c la s s e s f o r a r r a y s v m /a rra y
– ma n a g e d: S y s te m.A r r a y ( s u b c l a s s o f S y s te m.O b j e c t) v m /c om a rra y i n f o
– E E : A r r a y B a s e ( s u b c l a s s o f Object) v m /c om a rra y h e l p e rs

• s u b c l a s s e s o f S y s t e m .A r r a y c l a s s
– dy n a mi c a l l y g e n e r a te d ty p e s ( E E C l a s s s tr u c tu r e s )
– di f f e r i n e l e me n t ty p e s a n d/o r r a n k s
– g e n e r a te d me th o ds : G e t( … ), S e t( … ), A ddr e s s ( … )
• a rg u m e n t c o u n t = ra n k
• e a c h a r g u m e n t t y p e = e le m e n t t y p e

• z e r o -b a s e d s i n g l e -d i m e n s i o n a l a r r a y s ( v e c t o r s )
– i n s tr u c ti o n s i n I L ( n e w a r r , l de l e m, l dl e n , … )
– e l e me n t a c c e s s r a n g e c h e c k s
– c o mmo n p a tte r n s o p ti mi z e d b y J I T te r
int[] a;
for (int i = 0; i < a.Length; i++) a[i] = …;

• o t h e r a r r a y s ( n o n -v e c t o r s )
– a c c e s s e d v i a g e n e r a te d me th o ds
Advanced C # and . N E T p r o g r am m i ng
A rra y s

A rra y B a s e La y outs

vector non-vector
ArrayBase ArrayBase
sync block index sync block index

MethodTable * MethodTable *

element count element count

lengths[rank]

elements lowerBounds[rank]

elements
reference

reference

Advanced C # and . N E T p r o g r am m i ng
D e l e g a te s : C# Le v e l
• t y p e -s a f e p o i n t e r t o a m e t h o d
• d e c la r a t io n d e f in e s t a r g e t m e t h o d s ig n a t u r e
delegate void MyDelegate(string x);

• c a n “ p o in t ” t o b o t h s t a t ic a n d in s t a n c e m e t h o d s
class A
{
static void f(string x) { }
public virtual void g(string x) { }

static void Foo(MyDelegate d)


{
if (d != null) d("bar"); // invokes the target method
}

static void Main(string[] args)


{
Foo(new MyDelegate(f));
Foo(new MyDelegate(new A().g));
}
}

Advanced C # and . N E T p r o g r am m i ng
D e l e g a te s : M SIL Le v e l
• d e le g a t e d e c la r a t io n t u r n e d in t o c la s s d e c la r a t io n b y t h e C # c o m p ile r
.class sealed MyDelegate extends [mscorlib]System.MulticastDelegate
{
.method public instance
void .ctor(object 'object', native int 'method') runtime managed
{
}

.method public virtual instance


void Invoke(string x) runtime managed
{
}

.method public virtual instance class [mscorlib]System.IAsyncResult


BeginInvoke(string x,
class [mscorlib]System.AsyncCallback callback,
object 'object') runtime managed
{
}

.method public virtual instance


void EndInvoke(class [mscorlib]System.IAsyncResult result) runtime managed
{
}

Advanced C # and . N E T p r o g r am m i ng
D e l e g a te s : M SIL Le v e l ( cont. )
• d e le g a t e c o n s t r u c t io n ( s t a t ic m e t h o d )
ldnull
ldftn void A::f(string)
newobj instance void MyDelegate::.ctor(object, native int)

• d e le g a t e c o n s t r u c t io n ( v ir t u a l m e t h o d )
// obtain a reference to target object
dup
ldvirtftn instance void A::g(string)
newobj instance void MyDelegate::.ctor(object, native int)

• d e le g a t e in v o c a t io n
// obtain a reference to the delegate
ldstr "bar"
callvirt instance void MyDelegate::Invoke(string)

Advanced C # and . N E T p r o g r am m i ng
D e l e g a te s : E E Le v e l
• .c t o r ( o b j e c t , n a t i v e i n t ) i m p l e m e n t e d b y C O M D e l e g a t e : : D e l e g a t e C o n s t r u c t
– th e s a me me th o d f o r a l l de l e g a te s

• s p e c ia l I n v o k e e m it t e d f o r e a c h d e le g a t e c la s s
– h a r dc o r e o p ti mi z a ti o n s ( S tu b L i n k e r C P U : : E mi tM u l ti c a s tI n v o k e )
...
// Replace the pointer to a Delegate with the THIS pointer
static const BYTE replaceArg1[] = { 0x89, 0x4C, 0x24, 0x04 }; // mov [esp+4], ecx
EmitBytes(replaceArg1, sizeof(replaceArg1));
...

– i f th e de l e g a te p o i n ts to s ta ti c me th o d, a r g u me n ts mu s t b e s h u f f l e d
• I n v o k e c a lle d w it h ‘t h is ’ – r e f e r e n c e t o t h e d e le g a t e in s t a n c e
• t h e s t u b m u s t g e t r id o f t h is e x t r a p a r a m e t e r
• S t u b L in k e r C P U : : E m it S h u f f le T h u n k

• s e e D e le g a t e E E C la s s , C O M D e le g a t e c la s s e s in R o t o r f o r d e t a ils

Advanced C # and . N E T p r o g r am m i ng
D e l e g a te s : P e rf orm a nce

Millions of calls per second

235,14
Direct
233,92

123,3
Interface
138,35
Framework 2.0 (beta)
Framework 1.1
174,77
Delegate
44,58

0,17
InvokeMember
0,58

0 50 100 150 200 250

Advanced C # and . N E T p r o g r am m i ng

You might also like