Re: StringBuilder Extension: IsQuotedBy

Jon Skeet <[email protected]> Fri, 15 Feb 2008 08:38:37 +0000
Newsgroups gmane.comp.windows.devel.dotnet.clr
Message-ID <[email protected]>
Frans Bouma wrote:
>> You may say, this is for edge cases. Yes and no ... If object
>> allocation was free, there would be no StringBuilder in the first
>> place and it would not have a constructor asking for its initial size,
>> etc.
>
>         That initial size is to prevent memory fragmentation during memcpy
> actions when the buffer needs to be resized. I don't see a relevance with
> object creation speed and that parameter. I think the main reason the string
> builder is there is to avoid having lots of objects to collect. I file that
> kind of action under 'object destruction', not 'creation', though if you see
> that as one action (as creating an object means it also has to be collected at
> some point), you have a point.

Not just objects to collect - but data to copy.

Suppose object creation and memory allocation were both completely free
- but that you were concatenating a million single character strings
together, one at a time. You'd still end up copying 1000 billion bytes.
I suspect that allocation (a pointer bump) is cheaper than the copying
by the time the strings get reasonably large.

Likewise, giving StringBuilder an appropriate initial capacity doesn't
just avoid memory fragmentation but also extra copying.

Jon

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com