Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-10

Efficient String concatenation

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

This is a very impressive little blog about efficient string concatenation in Delphi. But it also applies to Free Pascal.

http://www.delphitools.info/2013/10/28/efficient-string-concatenation-in-delphi/

I created two versions of the small test project and compiled it with FPC as follows.

  $ fpc -O2 -al stringtest.pas
  $ fpc -O2 -al stringtest2.pas

Then compared the two *.s assembler files. As the blog mentions, the "stupid looking" version generates a heck of a lot less ASM code. Umm. :)

Regards,
- Graeme -

John Lee
to Graeme Geldenhuys

Did you try any other fpc compiler optimisation settings? No expert, but looks like it isn't too difficult for compiler to spot this.

What about llvm - (coming soon to fpc apparently) - would that fix it?

John

Graeme Geldenhuys wrote 28 lines
Hi, This is a very impressive little blog about efficient string concatenation in Delphi. But it also applies to Free Pascal. http://www.delphitools.info/2013/10/28/efficient-string-concatenation-in-delphi/ I created two versions of the small test project and compiled it with FPC as follows. $ fpc -O2 -al stringtest.pas $ fpc -O2 -al stringtest2.pas Then compared the two *.s assembler files. As the blog mentions, the "stupid looking" version generates a heck of a lot less ASM code. Umm. :) Regards, - Graeme - _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Jonas Maebe
to John Lee
John Lee wrote 8 lines
On 29 October 2013 13:22, Graeme Geldenhuys wrote: > This is a very impressive little blog about efficient string concatenation > in Delphi. But it also applies to Free Pascal. > http://www.delphitools.info/2013/10/28/efficient-string-concatenation-in-delphi/ Did you try any other fpc compiler optimisation settings? No expert, but looks like it isn't too difficult for compiler to spot this.

It may indeed be possible to handle this in the compiler.

What about llvm - (coming soon to fpc apparently) - would that fix it?

Not in the least. "soon" is also a gross overstatement.

Jonas

John Lee
to Jonas Maebe

jonas wrote
'
It may indeed be possible to handle this in the compiler.

What about llvm - (coming soon to fpc apparently) - would that fix it?

Not in the least. "soon" is also a gross overstatement. '
What's the size/difficulty of compiler spotting this? Suppose it needs to be able to 'look ahead' by few statements - which I'd thought it does already to some extent.

Think I saw email where you said 'few' months - that is soon in context of fpc and even more in the context of significant fpc changes, so not 'gross overstatement'!

John

Jonas Maebe wrote 29 lines
On 29/10/13 18:17, John Lee wrote: On 29 October 2013 13:22, Graeme Geldenhuys > wrote: > > This is a very impressive little blog about efficient string >> concatenation >> in Delphi. But it also applies to Free Pascal. >> http://www.delphitools.info/**2013/10/28/efficient-string-** >> concatenation-in-delphi/<http://www.delphitools.info/2013/10/28/efficient-string-concatenation-in-delphi/> >> > > Did you try any other fpc compiler optimisation settings? No expert, but > looks like it isn't too difficult for compiler to spot this. It may indeed be possible to handle this in the compiler. > What about llvm - (coming soon to fpc apparently) - would that fix it? Not in the least. "soon" is also a gross overstatement. Jonas ______________________________**_________________ fpc-pascal maillist - http://lists.freepascal.org/**mailman/listinfo/fpc-pascal<http://lists.freepascal.org/mailman/listinfo/fpc-pascal>
Sven Barth
to John Lee
John Lee wrote 11 lines
jonas wrote ' It may indeed be possible to handle this in the compiler. > What about llvm - (coming soon to fpc apparently) - would that fix it? Not in the least. "soon" is also a gross overstatement. ' What's the size/difficulty of compiler spotting this? Suppose it needs to be able to 'look ahead' by few statements - which I'd thought it does already to some extent.

It's not about parsing. The code will be parsed the same nevertheless.

It would be a optimization on the resulting node tree however after the function was parsed and before the code is generated.

Regards,
Sven

Graeme Geldenhuys
John Lee wrote
Did you try any other fpc compiler optimisation settings?

No, -O2 is the only compiler optimisation I enabled.

Regards,
Graeme

Antonio Fortuny
to John Lee: Did you try any other fpc compiler optimisation settings? No expert, but looks like it…

Hi All.

Based upon the code into the post, I've driven a test case: three platforms and Delphi 5, Delphi XE, Lazarus 1.0.12 (FPC 2.6.2) For a comparison purpose, the main specific code has been written in a classical way, using implicit string and the optimized code uses the function result as explicit intermediate string.
All tests have been run the same way: the program has been reloaded for everyy run either classic or optimized
times shown are in minutes(always zero):seconds.ms Two loops have been done: 10 meg loops and 100 meg. There are the results:

Platform Vista 32
Delphi 5
10,000,000 iterations classic 00:05.735 - optimized 00:04.374 100,000,000 iterations classic 00:57.881 - optimized 00:44.386 Lazarus 1.0.12 Win32
10,000,000 iterations classic 00:02.487 - optimized 00:01.498 100,000,000 iterations classic 00:25.121 - optimized 00:16.528 Delphi XE on Vista32
10,000,000 iterations classic 00:01.757 - optimized 00:01.233 100,000,000 iterations classic 00:18.524 - optimized 00:13.204

Platform W7 x64 (virtual machine into Esxi4)
Delphi 5
10,000,000 iterations classic 00:05.491 - optimized 00:04.773 100,000,000 iterations classic 00:57.143 - optimized 00:49.780 Lazarus 1.0.12 Win32
10,000,000 iterations classic 00:03.213 - optimized 00:02.465 100,000,000 iterations classic 00:33.290 - optimized 00:25.21 Lazarus 1.0.12 Win64
10,000,000 iterations classic 00:03.962 - optimized 00:03.136 100,000,000 iterations classic 00:42.495 - optimized 00:34.523 Delphi XE w32
10,000,000 iterations classic 00:02.886 - optimized 00:02.153 100,000,000 iterations classic 00:31.013 - optimized 00:24.102

Platform Linux x64 (virtual machine into Esxi4)
Lazarus 1.0.12
10,000,000 iterations classic 00:03.348 - optimized 00:02.727 100,000,000 iterations classic 00:37.081 - optimized 00:28.810

Enjoy.

Antonio