My Language Feature Requests

Christopher Wright dhasenan at gmail.com
Sat Dec 22 19:57:45 PST 2007


Craig Black wrote:
> 2) Adding language features that would allow for a moving GC.  A modern, 
> moving GC would also be a huge performance win.  I think we would have a 
> safety problem if we currently implemented a moving GC.  Languages that 
> have moving GC greatly restrict what can be done with pointers.  We need 
> to provide a syntax that will allow pointers to be used when memory is 
> explicitly managed, but disallow pointers for GC memory.
> 
> So, here's one idea for making D more safe for moving GC.
> 
> a) Disallow overloading new and delete for classes, and make classes 
> strictly for GC, perhaps with an exception for classes instantiated on 
> the stack using scope.

Don't see the point of this. You'd map a single old value to a single 
new value...or map an old range to a new one. You're changing one 
equality check and one assignment to two comparisons and an addition. 
And this is when you're looking through the entire address space of the 
program.

> b) Allow new and delete to work with structs, and allocate them on the 
> malloc heap.  I would still want to be able to override new and delete 
> for structs, specifically to be able to use nedmalloc.

This can allow polymorphism for structs, actually, but it is a bit of a 
performance hit.

> Then the compiler could disallow taking the address of a class field, 
> since we know the resulting pointer would pointer to the GC heap.  Note 
> that this would be a compile-time check, and so would not degrade 
> run-time performance.

Ugly.

What do you do for taking the address of a class variable? Well, okay, 
you have to take the address of the reference; you can't take the 
address of the variable directly. The current method is ugly and 
undefined behavior:
*cast(void**)&obj;

And you can assume that all pointers that point to that region of memory 
have to be moved.

The problem is granularity.

class Foo {
    Foo next;
    size_t i, j, k, l, m, n, o, p;
}

Here, the current regime would mark *Foo as hasPointers. If i, j, k, l, 
m, n, o, or p just happened to look like a pointer, they'd be changed. 
You'd need to find where each object begins, then you'd need to go 
through the offset type info to see which elements are really pointers.

Since you're running the garbage collector, that's doable, if the offset 
type info is currently available (I think it wasn't, last I checked, but 
I don't really recall).

> Another idea would be to be able to pin GC objects.  C# allows this via 
> the fixed keyword.  In D, it could work like this:
> 
> a) Preceding a pointer declaration with fixed would allow that pointer 
> to take the address in the GC heap.
> b) Pointer arithmetic would be disallowed for fixed pointers.

Why?

fixed float* four_floats = std.gc.malloc(4 * float.sizeof);
fixed float* float_one = four_floats;
fixed float* float_two = four_floats + 1;
fixed float* float_three = four_floats + 2;
fixed float* float_four = four_floats + 3;

Seems fine to me. You might go beyond the allocated space, but that's 
already undefined behavior.

> c) A fixed pointer will mark the corresponding GC object as "pinned" so 
> that the GC knows not to move the object.
> d) When the fixed pointer is changed or deallocated, it will unpin the 
> object, and pin any new object that it refers to.

While there is a fixed reference to the GC object, it is pinned. If that 
reference is rebound to another GC object, the original object is 
unpinned and the new one is pinned.

How to mark these is a difficult problem. On a 64-bit machine, I'd say 
you just use the most significant bit as a flag; you're not going to use 
petabytes of address space.

> The fixed pointer will have to know whether or not it points to GC 
> memory so that it doesn't pin non-GC objects.  Using the first idea, we 
> can determine at compile time whether a pointer points to the heap or not.

The fixed pointer will just stand there shouting "I am a fixed pointer! 
Look on me and despair!" And the garbage collector will look where it's 
pointing; if it is pointing at GC memory, the garbage collector will 
indeed look on it and despair. Otherwise, it will ignore the fixedness.

> Yes, this would be a big change, but not as big as const IMO.  I feel if 
> any feature warrants breaking some code, it would be high-performance 
> GC.  But maybe someone else can find a solution that doesn't break 
> compatibility.
> 
> Thoughts?
> 
> -Craig
> 
> 



More information about the Digitalmars-d mailing list