Home > .NET Framework > CLR Generics – Internals

CLR Generics – Internals

IL Extensions:
The IL for List<T> looks like:
.class public auto ansi serializable beforefieldinit System.Collections.Generic.List`1<T>
Notice the backtick ‘`’ followed by arity and then followed by type parameters.
The IL for the void Add(T item) looks like:
.method public hidebysig newslot virtual final
    instance void  Add(!T item) cil managed
The _items array of type T is represented as
!0[] class System.Collections.Generic.List`1<!T>::_items
The !0 says this is a place holder and will be replaced with the actual type during code generation by the JIT. 0 is the index of the type paraeter.
 
Traditional generic implementations have followed the following routes to implement Generics.
No Specialization: Possible if every type parameter is represented as a boxed reference type. This will require boing / unboxing still.
Full Specialization: A new copy of code is generated for each specialization. The drawback being the code explosion / increased working set size.
Mixed Code Specialization / Sharing: Share code if type representations are compatible. Else specialize code. Value type representations are considered incompatible whereas reference type representations are considered compatible.
 
CLR takes the mixed mode and Specialization happens during JIT time.
 
Advertisements
Categories: .NET Framework
  1. No comments yet.
  1. November 19, 2010 at 11:00 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: