www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Triple, quadruple concatenation?

reply Sterling Christensen <Sterling_member pathlink.com> writes:
I guess this is a feature request.

Say you've got:
char[] a, b, c, d;

I think that this:
a = b ~ c ~ d;

..should cancatenate in one step, kinda like this:
a.length = b.length + c.length + d.length;
a[0                   .. b.length] = b;
a[b.length            .. c.length] = c;
a[b.length + c.length .. d.length] = d;

..instead of working like this:
a = (b ~ c) ~ d;

It would be faster because it involves less copying and allocation, right?

Does the D language spec allow concatenation to be implemented that way? or does
it require something like a strict left to right order of operations, one
concatenation operator at a time? How do dmd and gdc do it?

-Sterling
Apr 19 2005
next sibling parent Derek Parnell <derek psych.ward> writes:
On Tue, 19 Apr 2005 23:32:44 +0000 (UTC), Sterling Christensen wrote:

 I guess this is a feature request.
 
 Say you've got:
 char[] a, b, c, d;
 
 I think that this:
 a = b ~ c ~ d;
 
 ..should cancatenate in one step, kinda like this:
 a.length = b.length + c.length + d.length;
 a[0                   .. b.length] = b;
 a[b.length            .. c.length] = c;
 a[b.length + c.length .. d.length] = d;
 
 ..instead of working like this:
 a = (b ~ c) ~ d;
 
 It would be faster because it involves less copying and allocation, right?
 
 Does the D language spec allow concatenation to be implemented that way? or
does
 it require something like a strict left to right order of operations, one
 concatenation operator at a time? How do dmd and gdc do it?
 

this optimization and it did shave off a lot wasted time. A common idiom is inserting an item ... A = A[0 .. x] ~ newitem ~ A[x .. $]; which greatly benefits from this optimization. -- Derek Melbourne, Australia 20/04/2005 9:44:52 AM
Apr 19 2005
prev sibling parent "TechnoZeus" <TechnoZeus PeoplePC.com> writes:
I an into trouble trying to do things like that and wasn't even sure why.
Should work... would be more intuitive.

TZ

"Sterling Christensen" <Sterling_member pathlink.com> wrote in message
news:d444es$1qob$1 digitaldaemon.com...
 I guess this is a feature request.

 Say you've got:
 char[] a, b, c, d;

 I think that this:
 a = b ~ c ~ d;

 ..should cancatenate in one step, kinda like this:
 a.length = b.length + c.length + d.length;
 a[0                   .. b.length] = b;
 a[b.length            .. c.length] = c;
 a[b.length + c.length .. d.length] = d;

 ..instead of working like this:
 a = (b ~ c) ~ d;

 It would be faster because it involves less copying and allocation, right?

 Does the D language spec allow concatenation to be implemented that way? or
does
 it require something like a strict left to right order of operations, one
 concatenation operator at a time? How do dmd and gdc do it?

 -Sterling

Apr 20 2005