Runtime reflection costs

Christopher Wright dhasenan at gmail.com
Sun Sep 28 06:25:38 PDT 2008


Hey all,

A lot of my code uses a large amount of compile-time reflection. I was 
working with my dependency injection library, coming up with speed 
tests, and noticed that one sample (373 classes) took several minutes to 
compile and resulted in a 4MB executable. The version with direct 
constructor calls was 380KB -- dconstructor had an overhead of 8KB per 
class! Templates can't be anywhere near as efficient as runtime 
reflection in that case. There's pretty much no code reuse.

Now I'm thinking about runtime reflection. D doesn't support significant 
runtime reflection -- you can invoke a default constructor, and that's it.

For my purposes, it would be convenient to have something of this nature:
class ConstructorInfo
{
	void* functionPtr;
	TypeInfo[] arguments;
	Object invoke (void*[] args)
	{
		_d_invoke (arguments, args, functionPtr);
	}
}

The runtime would have to be able to take a void*[] and a TypeInfo[] and 
unroll that into a method call. That should be reasonable, if D has a 
consistent and not overly complicated ABI with decent documentation.

What's the overhead for this?
  * It adds a function to the runtime -- one for each supported 
platform, at least, or possibly one for each platform/compiler tuple.
  * It adds a number of ConstructorInfo objects. In this case, they will 
be sizeof(Object) + 3 * sizeof(size_t) (20 bytes on x86).
  * You have to store pointers to the parameter types.
  * It adds an array of ConstructorInfo in classinfo. This costs 2 * 
sizeof(size_t) plus N * sizeof(size_t), where N is the number of 
constructors.

Overall cost: 2D + (6 + R)N, where D is the number of classes, N is the 
number of constructors, and R is the average number of parameters per 
constructor; plus some inline assembly in the runtime, plus the cost of 
documenting the ABI. The ABI documentation is required in other places, 
and the runtime functions should not need updating once written.

For my 373-class sample, runtime reflection would add 16KB to the 
executable. After optimization, templates cost 1.7MB.

Walter, would you accept patches to add runtime reflection to D? If so, 
would you add them to D1?



More information about the Digitalmars-d mailing list