www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16280] New: -profile=gc wrongly reports allocation when using

https://issues.dlang.org/show_bug.cgi?id=16280

          Issue ID: 16280
           Summary: -profile=gc wrongly reports allocation when using
                    reserve on dynamic arrays
           Product: D
           Version: D2
          Hardware: x86
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: cpicard openmailbox.org

When appending to dynamic arrays the GC profiler reports as many
allocations when using .reserve and when not, although the real number
of allocations is lower:

$ nl -b a test.d
     1  void main()
     2  {
     3       import std.stdio;
     4
     5       int[] arr;
     6       arr.reserve(50);
     7       writefln("capacity: %s", arr.capacity);
     8
     9       immutable limit = 100;
    10       foreach (i ; 0 .. limit)
    11       {
    12           if(append(arr, i))
    13               writefln("reallocation occurred: %s", i);
    14       }
    15  }
    16
    17  bool append(ref int[] arr, int value)
    18  {
    19      auto before = arr.ptr;
    20      arr ~= value;
    21      return arr.ptr !is before;
    22  }

$ dmd -profile=gc -run test.d ; cat profilegc.log
    capacity: 63
    reallocation occurred: 63
    bytes allocated, allocations, type, function, file:line
                400             100 int[] test.append test.d:20

We can note that 100 allocations on line 20 are reported although only
one reallocation really happens.

When commenting line 6 (arr.reserve(50)) we get the following output:

$ dmd -profile=gc -run test.d ; cat profilegc.log
    capacity: 0
    reallocation occurred: 0
    reallocation occurred: 3
    reallocation occurred: 7
    reallocation occurred: 15
    reallocation occurred: 31
    reallocation occurred: 63
    bytes allocated, allocations, type, function, file:line
                400             100 int[] test.append test.d:20

As many allocations exactly are reported by the GC profiler, and on
the same appending line. As far as I can tell arr.reserve does its
preallocation job but the GC is still reporting allocations that do
not appear.

--
Jul 14 2016