Re: Where is stringbuilder?



On Wed, 18 Oct 2006 12:14:46 -0500, Rob Kennedy <me3@xxxxxxxxxxx>
wrote:

<snip>

One of the reasons for StringBuilder is that in .Net (and Java), strings
are immutable -- once you create one, you cannot change its contents. In
Win32 Delphi, this statement is easy:

BigStr[L9 + Start] := SmallStr[L9];

In .Net, implementing that statement requires more work from the
compiler. It turns into something like this:

BigStr := Copy(BigStr, 1, L9 + Start - 1)
+ SmallStr[L9]
+ Copy(BigStr, L9 + Start + 1, MaxInt);

The old string gets discarded and a new string gets allocated because we
cannot change the contents of the old string, even when the reference
count is 1.

Fascinating,
so VB.NET does not support the equivalent of Mid$( S$, 2, 1 ) = "x"

What an odd idea
A string is just a block of memory

So in .Net, we need Stringbuilder because the old way of allocating a
long string and then filling it in later won't work -- it will result in
just as many memory allocations as before. StringBuilder purposely uses
something other than a string to holds its intermediate value, thus
avoiding the problem of immutable strings.

Hmm... I've generally found that Strings are about the most useful
and versatile way of allocating and manipulating blocks of memory

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

As it turns out, the Concat function is compiler magic. It does nothing
more than invoke the same routines that the "+" operator uses.

Drat, I expected to find the source for it.

How peculiar that Strings chars cannot be modified in .NET

Thanks for the info
.



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: 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)
  • Re: Fast string operations
    ... worried about string performance. ... > is why people use unsafe code now and then to use pointer arithmetic to loop ... > I'm aware that looping has to occur one way or the other, but with Trim, ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)