Garbage Collector profiling and the dynamic array reserve() function

Steven Schveighoffer schveiguy at yahoo.com
Tue Oct 17 13:27:24 UTC 2017


On 10/17/17 2:14 AM, Tony wrote:
> Found this unanswered question on StackOverflow.
> 
> This program:
> 
> import std.stdio;
> 
> void add(ref int[] data)
> {
>      data ~= 1;
>      data ~= 2;
> }
> 
> void main()
> {
>      int[] a;
>      writeln("capacity:",a.capacity);
>      auto cap = a.reserve(1000); // allocated may be more than requested
>      assert(cap >= 1000);
>      assert(cap == a.capacity);
>      writeln("capacity:",a.capacity);
>      a.add();
>      writeln(a);
> 
> }
> 
> compiled with "dmd -profile=gc"
> 
> has this output in profilegc.log
> 
> bytes allocated, allocations, type, function, file:line
>                4                  1    int[] profiling.add profiling.d:8
>                4                  1    int[] profiling.add profiling.d:7
> 
> The question is: why doesn't using reserve() cause an allocation to be 
> shown?

I don't know what "allocations" represents, but reserve actually calls 
gc_malloc, and the others do not (the space is available to expand into 
the block). There should be only one allocation IMO.

-Steve


More information about the Digitalmars-d-learn mailing list