[Mono-list] C#/.NET Generics update and summary

Stefan Matthias Aust sma@3plus4.de
Sat, 29 Mar 2003 12:48:16 +0100

David Jeske wrote:
> Since I'm very interested in C# Generics, in particular it's ability
> to improving typing and help close the performance gap with C/C++, I
> figured I'd post a summary of recent Generics information for anyone
> who is interested:

I'd also like to see generics (both in Java and C#) so I'd like to 
comment on your compiled information.  Thanks for your work.

For me, improved type safety is the more important point.  By my 
definition, a language like Java or C# must be considered statically 
unsafe as lony as null pointer/reference exceptions or class 
cast/invalid class exceptions can occur.

At least the latter can be worked around with generic types.

A more powerful type system (see for example nice.sf.net for a really 
nice superset of Java which doesn't have these problems anymore) would 
be required to get rid of null pointer exceptions.

>   similar needs as C++ templates or Generic Java (GJC). Currently the
>   performance of collections in .NET (or JVM) is similar to that of
>   Python, Perl, or Smalltalk because collections involve runtime
>   dynamic type casting.

Smalltalk - I know for sure - has no concept of static types, therefore 
no concept for typecasts whatsoever and therfore has no such overhead 
and its collections work with every type of object without any runtime 
penalty.  So I don't think that your comparison works.  The performance 
is worse because to regain static type-safeness, both Java and C# have 
to inject runtime type checks into the getter methods.

Java furthermore suffers because you have to explicitely box and unbox 
primitive types.  It might be possible that .NET could provide faster 
boxing and unboxing because the VM deals with this issue but I don't 
know.  As languages like Smalltalk have no primitives types and use 
objects all the time, this penalty isn't there and collections in 
Smalltalk are more efficient. (Of course, not having primitives 
datatypes can cause larger memory footprint or less efficient 
arithmetics in other parts of the program but that's a different story. 
In fact, by using objects all the time, there's no integer overflow in 
Smalltalk and the transition from small to large integers (with 
unlimited precision) is transparent for the user.)

 > Generics for .NET support runtime
>   specialization into static code, eliminating the extra typechecks,
>   and bringing the performance of collections closer to that of C/C++.

Support, but not require.  As I understand the specification, it would 
be pretty valid to use the same method as generic Java to fulfill the 
specification.  However, there's an opportunity to generate special 
kinds of collections for primitive types.

It's still unclear for me what would be the better approach to spread 
the use of generics.  The Java way which says we will not change the VM 
so that any code written with generic types will still run on old 
installations (although not faster than the old non-generic-code) or the 
.NET way which says you'll need a new VM (aka VES) but you might get 
better performance in exchange.

> 5) How is C# Generics different from Generic Java?

Isn't the extended type system which is the foundation for C# generics 
also more powerful that what Java has because its modeled after the ILX 
requirements which shall help to generate more efficient code for 
functional languages like F# (Ocaml) or Haskell(.NET)?

>    "retrofit" with parametric type-specifications. Generic Java is
>    rumored to be slated for the next Java release (1.5?) for which no
>    date is set.

It's not a rumor but part of the release 1.5 plan AFAIK.  The generics 
compiler is already available, although that compiler doesn't feature 
the other syntax changes (boxing, enums, foreach-statement etc), yet 
which will also be introduced to 1.5 to probably should make Java to 
catch up with C# again.

The Eclipse project announced to add generics to their compiler as part 
of the 2.2 release which might be available in six months or so.  Early 
access versions are hopefully available is a few months.

Java 1.5 should be available in early 2004 with a beta which will 
probably be released in July at the JavaOne conferrence (my guess, not 

>    run-time casts. Gyro, a reference implementation of Generic C#/CIL,
>    is available as a patch to the Microsoft Shared Source CLI.

Does Gyro already has the mentioned CLI 2.0 and is there a measurable 
performance improvement?

Stefan Matthias Aust   //
www.3plus4software.de // Inter Deum Et Diabolum Semper Musica Est