Thread overview
optimatization suggestion
Apr 01, 2017
Inquie
Apr 01, 2017
Stefan Koch
Apr 01, 2017
Stefan Koch
Apr 01, 2017
ketmar
Apr 02, 2017
H. S. Teoh
April 01, 2017
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)

April 01, 2017
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.

April 01, 2017
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.
April 02, 2017
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.
April 01, 2017
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