digitalmars.D - optimatization suggestion
- Inquie (19/19) Apr 01 2017 I use a lot of code that has string appending and it seems like
- Stefan Koch (2/21) Apr 01 2017 Cannot be done reliably without proper data-flow analysis.
- Stefan Koch (5/37) Apr 01 2017 Besides, it would generate almost the same code under the hood.
- ketmar (7/14) Apr 01 2017 the problem with "smal optimizations" is that they adds complexity to th...
- H. S. Teoh via Digitalmars-d (23/42) Apr 01 2017 You could have just written instead:
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
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
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: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.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
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
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