Recent

Author Topic: Any TStringBuilder class implementation?  (Read 21608 times)

eny

  • Hero Member
  • *****
  • Posts: 1634
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Any TStringBuilder class implementation?
« Reply #16 on: August 19, 2014, 08:07:22 pm »
If this is true:
So that tells me it is yet one of those brain-dead Embarcadero ideas of simply copying .NET verbatim - thus no thought about the Object Pascal language itself.
....
I would honestly like to know what is the purpose of TStringBuilder, and what does it improve?
... then nothing, of course. Otherwise it wouldn't be brain-dead.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Any TStringBuilder class implementation?
« Reply #17 on: August 19, 2014, 08:43:14 pm »
I would honestly like to know what is the purpose of TStringBuilder, and what does it improve?

Jeroen Pluimers on stackoverflow writes
"TStringBuilder reallocates the underlying buffer with an over capacity scheme, which means that with large buffers and frequent appends, it can be a lot faster than TStringList...but to get the TStringBuilder content, you have to call the ToString method which can slow things down."
 
Some people find TStringBuilder methods easier to read and use than standard string-appending code, but in many cases you would be gaining readability at the expense of slightly reduced performance (assuming you find it more readable).

Someone else posted this:
"TStringBuilder was introduced solely to provide a source code compatible mechanism for applications to perform string handling in Delphi and Delphi.NET. You sacrifice some speed in Delphi for some potentially significant benefits in Delphi.NET
The StringBuilder concept in .NET addresses performance issues with the string implementation on that platform, issues that the Delphi (native code) platform simply does not have.
If you are not writing code that needs to be compiled for both native code and Delphi.NET then there is simply no reason to use TStringBuilder."
« Last Edit: August 19, 2014, 08:45:06 pm by howardpc »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Any TStringBuilder class implementation?
« Reply #18 on: August 19, 2014, 08:45:02 pm »
Originally it was of course just for working around the Java/.NET limitation that strings are immutable.

The only thing I can imagine for implementations without immutable strings is  that you program against an interface that you could replace with something else, to have better control over the algorithm to reallocate the buffer during repeated (and prolonged) concatenation. (read webidiots that append MB's of javascript gibberish)
« Last Edit: August 20, 2014, 10:26:14 am by marcov »

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: Any TStringBuilder class implementation?
« Reply #19 on: September 08, 2014, 09:56:02 pm »
Thanks for all the replies and links.  As I thought, the TStringBuilder concept seem pretty useless in Delphi/FPC.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

mtbf40

  • Newbie
  • Posts: 5
Re: Any TStringBuilder class implementation?
« Reply #20 on: September 13, 2014, 11:55:38 am »
@Graeme: which version you use for <tiOPF test suite>? tiOPF2 or tiOPF3

it's important for the use of TStringBuilder

Regards

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Any TStringBuilder class implementation?
« Reply #21 on: September 13, 2014, 12:12:07 pm »
version 2. See the tiopf documentation e.g.
http://wiki.lazarus.freepascal.org/tiOPF

Also, please start a new thread if you have a new question. Thank you.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

mtbf40

  • Newbie
  • Posts: 5
Re: Any TStringBuilder class implementation?
« Reply #22 on: September 13, 2014, 01:10:03 pm »
ok - who can read are clearly at an advantage!  %)  :o

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: Any TStringBuilder class implementation?
« Reply #23 on: September 13, 2014, 09:36:06 pm »
@Graeme: which version you use for <tiOPF test suite>? tiOPF2 or tiOPF3
Currently only tiOPF2 works with FPC. I'm in the process of looking at FPC 2.7.1 (trunk) to see if it could support tiOPF3.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

dmz73

  • Newbie
  • Posts: 1
Re: Any TStringBuilder class implementation?
« Reply #24 on: June 23, 2017, 02:51:16 am »
Originally it was of course just for working around the Java/.NET limitation that strings are immutable.

The only thing I can imagine for implementations without immutable strings is  that you program against an interface that you could replace with something else, to have better control over the algorithm to reallocate the buffer during repeated (and prolonged) concatenation. (read webidiots that append MB's of javascript gibberish)

The advantage of TStringBuilder is that is allocates additional memory for the string so it reduces the number of string copy operations when reallocating the string memory on append of new string. Speed difference becomes noticeable once memory manager can no longer expand the existing string without making a copy first.
It is difficult to create a simple test to show this issue with but I have seen it in XML parser when assembling XML text from nodes on large XML documents and also recently in jedicodeformat when processing a large .PAS file. In both cases replacing the simple "outputstring := outputstring + smallstring" with TStringBuilder increased the speed by several orders of magnitude.

I have implemented the methods of TStringBuilder class started by tangentstorm on github (https://github.com/tangentstorm/fpc-stringbuilder) in my fork here https://github.com/dmz73vu/fpc-stringbuilder.

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: Any TStringBuilder class implementation?
« Reply #25 on: June 23, 2017, 06:49:54 am »
Originally it was of course just for working around the Java/.NET limitation that strings are immutable.

The only thing I can imagine for implementations without immutable strings is  that you program against an interface that you could replace with something else, to have better control over the algorithm to reallocate the buffer during repeated (and prolonged) concatenation. (read webidiots that append MB's of javascript gibberish)

The advantage of TStringBuilder is that is allocates additional memory for the string so it reduces the number of string copy operations when reallocating the string memory on append of new string. Speed difference becomes noticeable once memory manager can no longer expand the existing string without making a copy first.
It is difficult to create a simple test to show this issue with but I have seen it in XML parser when assembling XML text from nodes on large XML documents and also recently in jedicodeformat when processing a large .PAS file. In both cases replacing the simple "outputstring := outputstring + smallstring" with TStringBuilder increased the speed by several orders of magnitude.

I have implemented the methods of TStringBuilder class started by tangentstorm on github (https://github.com/tangentstorm/fpc-stringbuilder) in my fork here https://github.com/dmz73vu/fpc-stringbuilder.

Glad someone finally understands the importance of TStringBuilder here.
Good job Man.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Any TStringBuilder class implementation?
« Reply #26 on: June 23, 2017, 10:44:51 am »
Glad someone finally understands the importance of TStringBuilder here.

He just gave a positive spin to what I already said in the last paragraph of the text I quoted?

Originally it was of course just for working around the Java/.NET limitation that strings are immutable.

The only thing I can imagine for implementations without immutable strings is  that you program against an interface that you could replace with something else, to have better control over the algorithm to reallocate the buffer during repeated (and prolonged) concatenation. (read webidiots that append MB's of javascript gibberish)

The .NET compatibility thing is stated in the embarcadero docs: http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.SysUtils.TStringBuilder
 
Also note that there is a Tstringbuilder in trunk since may 2016. (not merged to 3.0.x series)
« Last Edit: June 23, 2017, 10:53:41 am by marcov »

BeniBela

  • Hero Member
  • *****
  • Posts: 905
    • homepage
Re: Any TStringBuilder class implementation?
« Reply #27 on: June 23, 2017, 12:26:53 pm »
Recently I have been working on one, too: https://github.com/benibela/bbutils/blob/master/bbutils.pas#L469-L484

Trying to have zero overhead. It is a record rather than a class, so the builder instance  needs no allocation, and it writes to an existing (pointer to a) string rather than creating a new string, so there is no additional reference counting.

I am just not sure how to handle codepoint aware strings. Now I say everything must be utf-8, including acp, so it can be concatenated quickly with move. If I let it check if the encoding matches, it is unnecessary slow with Lazarus. If the encoding does not match, it should not write utf-8 in an ansistring anyways, but no one uses utf8string.

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Any TStringBuilder class implementation?
« Reply #28 on: June 23, 2017, 02:20:38 pm »
Since I haven't found any decent StringBuilder implementation so far, and no one posted one in this topic, I decided to implement it and also post it here
So here it is... (see attachment)
A couple of comments
1. This line generates a runtime error when compiled with a range check (StringBuilderUnit, line 83)
Code: Pascal  [Select][+][-]
  1.     Move(positionInString^, Tail^.Data[Tail^.Count], bytesToWriteInCurrentBlock);
2. I deleted your unit and added it:
Code: Pascal  [Select][+][-]
  1. type
  2.   TStringBuilder = class(TStringList)
  3.   public
  4.     constructor Create;
  5.   end;
  6.  
  7. constructor TSimpleStringBuilder.Create;
  8. begin
  9.   inherited;
  10.   LineBreak := '';
  11.   Capacity := CountOfTestString;
  12. end;
  13.  
By the speed there were almost the same values, but the amount of new code was greatly reduced :).

 

TinyPortal © 2005-2018