We know that most of the time memory is allocated more than the requested amount. Is there a way to take advantage of that extra trailing space? (And potentially the pages that come after that.)

import core.memory;

void main()
{
    const count = 1;

    // I think there is extra capacity beyond the 'count' elements
    int* ptr = cast(int*)GC.malloc(count * int.sizeof);
    int[] arr = ptr[0 .. count];

    assert(arr.capacity == 0);
    arr.assumeSafeAppend;
    assert(arr.capacity == 0);    // still 0. :(
}

This issue puts std.array.array to a disadvantage compared to proper slices because array() involves the following call chain, the last of which does call GC.malloc:

  trustedAllocateArray
  uninitializedArray
  arrayAllocImpl

As a result, iota(10).array.assumeSafeAppend ends up having 0 capacity. :(

Ali

Reply via email to