www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Compile-time class instances

reply bearophile <bearophileHUGS lycos.com> writes:
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
Apr 03 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
This idea can be related to the section "Pointer Hydration" written by Walter,
the main difference is that it's done by the compiler, so for the programmer
it's safer and simpler to use :-)

http://www.artima.com/cppsource/backyard.html

Such loading of already initialized class instances is commonly done in games
written in C++, see for example the "Fast data load trick" in the Game
Programming Gems 1:
http://alturl.com/ipje

Bye,
bearophile
Apr 03 2010
parent bearophile <bearophileHUGS lycos.com> writes:
 Such loading of already initialized class instances is commonly done in games
written in C++,
Many things in D are designed starting from a widespread C++ idiom, where it's often implemented in a more or less manual way, and changing it into a explicit D construct, that is semantically clean and possibly safer. So compile-time class instances can be another example of such kind of idiom conversion. Bye, bearophile
Apr 04 2010