Re: Where is stringbuilder?



On Wed, 18 Oct 2006 01:58:03 +0100, nowhere@xxxxxxxxxxxxxx wrote:
<snip>

That's exactly what Stringbuilder is.

When a string is added to another, such as s := s + t, instead of
simply tacking t on the end of s, a whole new block of memory is
allocated that can accommodate the new string. (the original s might
have something immediately after it, so concatenation in place might
not be possible).
The memory used by the original s is then abandoned, until the .NET
Garbage Collector reclaims it.

If you carry out a succession of string concatenations, you end up
leaving behind lots of little blocks of memory.

A Stringbuilder pre-allocates a larger block so that concatenations
can be done in-place. This has the effect of reducing the number of
memory allocations required, and the amount of work needed by the
Garbage Collector.

Win32 doesn't have a Garbage Collector, so Stringbuilders are
irrelevant.
I would guess that if you created a Win32 application in D2005/D2006,
you wouldn't have Stringbuilders either. (Or if you do, they're
probably just strings in reality).

Win32 or rather Delphi does not have a garbage collecter, but the
problem of 'Swiss Cheese' memory certainly exists.

If one /knows/ that one will be concatenation a whole load of strings,
or more precisely 'growing' just one string, then it makes sense to
create a longer string then push the smaller strings into their
destination positions.

This is what I use :-

{ ###########################################################
MidSet( L, 4, S )
}
Procedure MidSet( Var BigStr:String; Const HStart:Integer ; Const
SmallStr : String ) ;
Var
L9, BL, SL, L, Start : Integer ;
Begin
BL := Length( BigStr ) ;
SL := Length( SmallStr ) ;
Start := HStart - 1 ; { adjust start }

L := Min( SL, BL - Start ) ;
For L9 := 1 To L Do
BigStr[ L9 + Start ] := SmallStr[ L9 ] ;

End; {MidSet}

Alternatively one could use a Stream of some sort.

Personally I don't think that this is worth worrying about unless one
is doing a heck of a lot of concatenations on the same String

There is the old Concat Function, but it is not much use for this sort
of thing.


.



Relevant Pages

  • Re: Fast string operations
    ... Looping: I thought looping over arrays in managed code was "slow" ... array handling and such. ... The problem with TrimHelper is that it always returns a new string instance. ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Discovering variable types...
    ... >- but I suppose MS expect us to use wrappers ... memory allocations for your variables from disk as well. ... >They most certainly are of fixed size, changing the size of a String ... >>me to keep buffer size and current postion right in the memory block. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Compiler String Efficiency
    ... I drew this conclusion from looking at several string concatenation ... This set me to wondering how VB was handling the memory allocation -- ... If a set amount of memory is being set aside, ...
    (microsoft.public.vb.winapi)
  • Re: Optimize
    ... if you use it like me to get 'the next string ptr' in addition. ... | |mov ebx eax;dw aligned strings are faster ... | Would it be enough to touch each 16th bytes in the 64K memory area ... Cache-line size is fixed, my AMD got 64 bytes per line. ...
    (alt.lang.asm)
  • Memory Question
    ... I have wrote a helper to my program which monitors object count, memory utilization and a threshold of 10% to determine how many objects are sticking around and how many are being garbage collected. ... occured right before the original String count). ... def print_threshold_breakers hsh1, hsh2, threshold ... putsf 'Building mem usage', mem_usage ...
    (comp.lang.ruby)

Loading