[Issue 4047] [CTFE] class/struct heap allocation

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Apr 4 07:57:00 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4047



--- Comment #2 from bearophile_hugs at eml.cc 2010-04-04 07:56:58 PDT ---
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).


This whole idea is related to the section "Pointer Hydration" written by
Walter, the main difference is that it's done by the compiler (and the data is
stored inside the binary itself), 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 book.

Many things in D are designed taking the idea from a common C++ idiom, where it
is usually implemented in a more or less manual way, and change it into a
explicit D construct, that is semantically clean and possibly safer. So this
compile-time class instances can be another exaple of such kind of idiom
conversions.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list