Menu

Monthly Archives: September 2007

Miguel blogged yesterday about my committing a piece of code enabling sharing of methods of generic classes. His post gives the impression as if this was already fully implemented now, but it is actually just the basis for what’s to come.

Previous to this change Mono always compiled every instantiation of a method of a generic class separately. We’re trying to get to the point where all instantiations for reference types share the same native code. My first implementation only shares a method between instantiations if the method’s code doesn’t refer to the type argument of the class. As an example, let’s take this (nonsensical) class:

public class GenA<T> {
static T[] arr;

public static GenA () {
arr = new T [3];
}

public GenA () {}

public GenA<T> newGen () {
return new GenA<T> ();
}

public GenA<int> newGenInt () {
return new GenA<int> ();
}

public T[] getArr () {
return arr;
}

public T[] newArr () {
return new T [3];
}

public int hash (T obj) {
return obj.GetHashCode ();
}

public T ident (T obj) {
return obj;
}

public T cast (Object obj) {
return (T)obj;
}
}

What will be shared here are the constructor (not the static constructor, though), newGenInt, hash and ident. The static constructor and getArr will not be shared because they refer to a static member which is currently translated with the address hard-coded. Since each generic instantiation of Gen has its own static members a shared method will not work. newGen will not be shared because it depends on the type argument T to create the array. Neither will cast because it needs to know T to decide whether the cast is valid.

Right now generics sharing is still disabled by default, but can be turned on with -O=gshared. If you’re using Mono SVN and are working with generics code, then it’d be nice if you could try it out and report any problems you encounter.

Among the responses of Flickr staff in this thread was this gem from Heather: “[…] we as a team can decide if this is something that we want to see on the site […]”. So, basically, the Flickr deal is this: You pay them to host your photos and if they’re something they want to see they won’t delete them. Of course they won’t tell you what they want or don’t want to see. The good news is that no photos of flowers or sunsets have been deleted. Yet.