dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.22k stars 5.87k forks source link

Confusion about StringBuilder memory allocation #42301

Open samibinsami opened 3 weeks ago

samibinsami commented 3 weeks ago

Type of issue

Other (describe below)

Description

Referring to the article: https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-text-stringbuilder

In first section it says, A StringBuilder object maintains a buffer to accommodate expansions to the string. New data is appended to the buffer if room is available; otherwise, a new, larger buffer is allocated, data from the original buffer is copied to the new buffer, and the new data is then appended to the new buffer.

From above explanation, I can say that StringBuilder object's data resides in a single block of memory(or in other words called buffer because old and new data resides in a new single buffer).

But later on in the article, in section(Iterate StringBuilder characters), it says that, Performance is severely impacted because each character access walks the entire linked list of chunks to find the correct buffer to index into.

Now this text says that, StringBuilder is chunky, meaning the objects data resides in scattered chunked in memory connected via references, hence called linked list of chunks.

So I see there are 2 different explanation of how StringBuilder memory is declared.

Page URL

https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-text-stringbuilder#search-the-text-in-a-stringbuilder-object

Content source URL

https://github.com/dotnet/docs/blob/main/docs/fundamentals/runtime-libraries/system-text-stringbuilder.md

Document Version Independent Id

7d655701-437d-26bf-18d5-cb2d56e3754c

Article author

@gewarren

Metadata


Associated WorkItem - 308214

gewarren commented 2 weeks ago

Thanks @samibinsami, I agree that is confusing. I think the "chunked" description is more accurate based on https://github.com/dotnet/runtime/blob/9a31a5b47aa7484bec71ba28aa59712c7898b902/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs#L26-L28. @stephentoub Can you confirm, and I will update this doc?

stephentoub commented 2 weeks ago

Yes, it's (mostly) an implementation detail, but StringBuilder is implemented today as a linked list of buffers. New buffers are added rather than existing ones being replaced.

samibinsami commented 1 week ago

Thanks @stephentoub for resolving my confusion.

I assume the next step would be to update the document?

samibinsami commented 1 week ago

@stephentoub any updates to my previous comment would be helpful.

gewarren commented 1 week ago

any updates to my previous comment would be helpful.

Yes, the doc will be updated.