Compile-time class instances

bearophile bearophileHUGS at lycos.com
Sat Apr 3 17:32:50 PDT 2010


This is a (possibly silly) idea for a low-priority enhancement, to allow the allocation of class instances at compile-time. This can be useful because their creation time can be moved from run-time to compile-time.

When a class is instantiated at compile-time, it can be used and it can even get garbage collected. When the compile-time computation ends, the compile-time GC must collect all unused class instances (otherwise you can't tell what class instance the compiled program will contain. The compile-time GC can be a reference counter too, that is more deterministic) and put the active ones in a data segment of the binary.

If a class instance is mutable, but the data segment is read-only, then at the start of run time the runtime has to copy it into the heap and to update the internal pointers and references.

To reduce implementation complexity, the compile-time class instances can be immutable only, so there's no need to copy them to the heap and change the pointers. If the the class instance is immutable but its reference is mutable then the class instance may need a mutable boolean somewhere to tell if it's it's garbage-collected at runtime (because it can't be really deleted from the ROM).

I am probably missing some things, and this idea can be too much complex to implement for its perceived usefulness (and I will remove bug 4047 now).

Bye,
bearophile



More information about the Digitalmars-d mailing list