www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22271] New: Appending to an empty array reallocate

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

          Issue ID: 22271
           Summary: Appending to an empty array reallocate
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: druntime
          Assignee: nobody puremagic.com
          Reporter: deadalnix gmail.com

See sample code:

string x = "foobar";
string y;
y ~= x;

import std.stdio;
writefln("%x %x", x.ptr, y.ptr);

We can see here that a new allocation is done and the content of x copied in
it. However, this reallocation is superfluous and a waste of perfectly good CPU
cycles and memory :)

--
Sep 03 2021