opConcatAll?

Steven Schveighoffer schveiguy at gmail.com
Thu Jul 2 15:47:39 UTC 2020


If I have an array:

int[] arr = [1, 2, 3];

And I use it in a concatenation chain:

auto newArr = [0] ~ arr ~ [4, 5, 6];

The compiler calls a single function to allocate these together 
(_d_arraycatnTX).

However, if I define a struct instead:

struct S
{
    int [] x;
    S opBinary(string s : "~")(S other) {return S(x ~ other.x); }
}

Now, if I do:

S arr = S([1, 2, 3]);

auto newArr = S([0]) ~ arr ~ S([4, 5, 6]);

I get one call PER operator, in other words, it gets translated to:

S([0])opBinary!"~"(arr).opBinary!"~"(S([4, 5, 6]));

Which means one separate allocation for each field. If you have a lot of 
these all put together, it could add up to a lot of allocations, with 
most of the allocations as temporaries.

What about an opConcatAll (or other possible name), which can accept all 
the following concatenations as parameters?

in other words, given:

a ~ b ~ c ~ d

will translate to

a.opConcatAll(b, c, d)

if possible.


Would that make sense?

-Steve


More information about the Digitalmars-d mailing list