www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - optimatization suggestion

reply Inquie <Inquie data.com> writes:
I use a lot of code that has string appending and it seems like 
it could be optimized;

wchar[] x;

x ~= "This is one line";
x ~= "This is another line";

could become

x ~= "This is one lineThis is another line";

The rewrite rule is very simple:

if char type array is being appended to with literal string and 
next line also has appender with literal string, then combine in 
to one appender.

More complex cases could be handled such as

x ~= "This is one"~x~" line";
x ~= "This is another line";

which turns in to

x ~= "This is one"~x~" lineThis is another line";

Since these are done in ctfe, it may improve the speed 
significantly in some cases!? (usually the splitting up is for 
readability and to allow for easy modification)
Apr 01 2017
next sibling parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Saturday, 1 April 2017 at 17:15:54 UTC, Inquie wrote:
 I use a lot of code that has string appending and it seems like 
 it could be optimized;

 wchar[] x;

 x ~= "This is one line";
 x ~= "This is another line";

 could become

 x ~= "This is one lineThis is another line";

 The rewrite rule is very simple:

 if char type array is being appended to with literal string and 
 next line also has appender with literal string, then combine 
 in to one appender.

 More complex cases could be handled such as

 x ~= "This is one"~x~" line";
 x ~= "This is another line";

 which turns in to

 x ~= "This is one"~x~" lineThis is another line";

 Since these are done in ctfe, it may improve the speed 
 significantly in some cases!? (usually the splitting up is for 
 readability and to allow for easy modification)
Cannot be done reliably without proper data-flow analysis.
Apr 01 2017
parent Stefan Koch <uplink.coder googlemail.com> writes:
On Saturday, 1 April 2017 at 22:38:58 UTC, Stefan Koch wrote:
 On Saturday, 1 April 2017 at 17:15:54 UTC, Inquie wrote:
 I use a lot of code that has string appending and it seems 
 like it could be optimized;

 wchar[] x;

 x ~= "This is one line";
 x ~= "This is another line";

 could become

 x ~= "This is one lineThis is another line";

 The rewrite rule is very simple:

 if char type array is being appended to with literal string 
 and next line also has appender with literal string, then 
 combine in to one appender.

 More complex cases could be handled such as

 x ~= "This is one"~x~" line";
 x ~= "This is another line";

 which turns in to

 x ~= "This is one"~x~" lineThis is another line";

 Since these are done in ctfe, it may improve the speed 
 significantly in some cases!? (usually the splitting up is for 
 readability and to allow for easy modification)
Cannot be done reliably without proper data-flow analysis.
Besides, it would generate almost the same code under the hood. Unless we do something really clever, it's not going to safe much time. Significant improvements are not expected.
Apr 01 2017
prev sibling next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
Inquie wrote:

 I use a lot of code that has string appending and it seems like it could 
 be optimized;

 wchar[] x;

 x ~= "This is one line";
 x ~= "This is another line";

 could become

 x ~= "This is one lineThis is another line";
the problem with "smal optimizations" is that they adds complexity to the compiler for a very small win in a very specific cases. here, the overall win is almost nonexistant, but compiler devs should add another specific data flow analysis stage. it may *look* like a trivial piece of code, but it actually not so simple, and such "trivial" hacks tend to accumulate, burdening the code base.
Apr 01 2017
prev sibling parent "H. S. Teoh via Digitalmars-d" <digitalmars-d puremagic.com> writes:
On Sat, Apr 01, 2017 at 05:15:54PM +0000, Inquie via Digitalmars-d wrote:
 I use a lot of code that has string appending and it seems like it could be
 optimized;
 
 wchar[] x;
 
 x ~= "This is one line";
 x ~= "This is another line";
You could have just written instead: wchar[] x; x ~= "This is one line" ~ "This is another line"; ? [...]
 More complex cases could be handled such as
 
 x ~= "This is one"~x~" line";
 x ~= "This is another line";
 
 which turns in to
 
 x ~= "This is one"~x~" lineThis is another line";
 
 Since these are done in ctfe, it may improve the speed significantly
 in some cases!? (usually the splitting up is for readability and to
 allow for easy modification)
If you want something done in CTFE, do this: string makeLongString() { wchar[] x; x ~= "blahblah"; x ~= "blehbleh"; x ~= "abc " ~ x ~ " def"; return x; } void main() { enum e = makeLongString(); // enum forces CTFE string x = e; // voila, everything done at compile-time } T -- "Real programmers can write assembly code in any language. :-)" -- Larry Wall
Apr 01 2017