www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17881] New: Provide mechanism to preallocate memory from the GC


          Issue ID: 17881
           Summary: Provide mechanism to preallocate memory from the GC
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: druntime
          Assignee: nobody puremagic.com
          Reporter: schveiguy yahoo.com

There are some times where you know you are going to allocate a bunch of blocks
from the GC, but you need to deal with them one at a time, and you don't want
them tied to one giant array for GC purposes.

A good example is an associative array:

int[int] aa;
foreach(i; 0 .. 20_000_000) aa[i] = i;

Each assignment requires a new GC allocation, which is unavoidable. However, we
could streamline the allocation:

1. If we told the GC that in the next few lines we were going to need 20
million AA elements of size X, it could set aside this data in a specialized
place that would not be collected, and would not be handed out elsewhere
(probably a new page bit is needed).
2. Each allocation for this purpose, until the preallocated cache is exhausted,
would simply return the next one, and not need to do any collections.
3. The GC could optimize locking. For instance it may only need to release for
possible collection the elements a page at a time, so only one lock/unlock when
it flips those bits. The idea is once you reserve, you plan to quickly use
those blocks.

A strawman high level API:

auto preallocate(T)(size_t quantity);


x = GC.preallocate!int(100_000); // give me a block of preallocated ints

foreach(i; 0 .. 100_000)
   assert(x.remaining + 1 + i == 100_000);
   int *p = x.allocNext(); //  nogc

When x goes out of scope, any remaining preallocated blocks that weren't used
are freed.

Oct 06