Skip to content

[API Proposal]: Get a Read-only view of a StringBuilder body and clear the StringBuilder. #97570

Closed

Description

@jkoritzinsky

Background and motivation

The Roslyn and runtime teams are looking at improving the experience of writing formatted text, primarily around source generators. An important feature for Roslyn is a mechanism to, without a copy, get a read-only view of any text that no one else can write to. The lack of an API to provide this has been blocking dotnet/roslyn#61326.

The Roslyn team has mentioned recently that they are okay with a non-optimal copy-based API for downlevel scenarios as long as there is a no-copy API on the horizon. This proposal provides such an API.

The following is updated from #97570 (comment)

API Proposal

namespace System.Text;

public class StringBuilder
{
    public StringBuilder MoveChunksToStringBuilder();
}

This API will create a new StringBuilder instance initialized to the same state as the original instance, and then reset the fields of the original instance back to the same state as a freshly created instance.

This clearing behavior is in contrast to the behavior when Clear is called, where the internal array is not thrown away.

API Usage

Roslyn would use it with an API similar to the following:

public abstract class SourceText
{
    public static SourceText DrainFrom(StringBuilder stringBuilder)
    {
        return new StringBuilderText(stringBuilder.MoveChunksToStringBuilder());
    }
}

(StringBuilderText implementation, not able to wrap user-provided StringBuilders until this StringBuilder API is added)

New StringBuilder versus array

An allocation is needed somewhere so that chunk enumeration can start multiple times after the original StringBuilder is cleared.

Allocating a new StringBuilder is cheaper than allocating an array of chunks, both because the array would be variable sized and because populating the array requires following the whole linked list chain. In comparison, the implementation of MoveChunksToStringBuilder needs only allocate a new StringBuilder and set a handful of fields directly in the old and new instances.

Allocating a new StringBuilder is also a more composable operation, opening the door to any use case where you need a cheap way to be sure that no one else can modify the contents of the StringBuilder that you hold, even if your use case goes beyond chunk enumeration (possibly involving your own mutations).

Naming

MoveChunks is preferred over Move or Drain because it is clearer that the chunks are leaving the current instance and landing as-is in the new instance without being compacted into a single chunk. MoveChunks is preferred over DrainChunks because Move represents the O(1) operation on the chunks that is happening, whereas Drain sounds more involved. (And in prior art, Drain is in fact more costly than Move; ImmutableArray<T>.Builder.DrainToImmutable freshly allocates and copies elements when the presizing doesn't match.)

Activity

  1. added
    api-suggestion
    on Jan 26, 2024
  2. ghost added
    needs-area-label
    on Jan 26, 2024
  3. ghost added
    untriaged
    on Jan 26, 2024
  4. added and removed
    untriaged
    needs-area-label
    on Jan 26, 2024
  5. added
    untriaged
    on Jan 26, 2024
  6. jkoritzinsky commented on Jan 26, 2024

    @jkoritzinsky
    MemberAuthor
  7. stephentoub commented on Jan 26, 2024

    @stephentoub
    Member

    Related to #87362

    (As with that one, this would necessitate moving ReadOnlySequence into corelib.)

  8. MichalPetryka commented on Jan 26, 2024

    @MichalPetryka
    Contributor

    I don't see how the Clear behaviour here makes sense, seems like you effectively want it to alloc a new StringBuilder instead.

  9. CyrusNajmabadi commented on Jan 26, 2024

    @CyrusNajmabadi
    Contributor

    I don't see how the Clear behaviour here makes sense, seems like you effectively want it to alloc a new StringBuilder instead.

    We want to transfer ownership of hte underlying data. That's only safe if after the operation the SB itself doesn't point at the data (hence, it is "cleared").

  10. lambdageek commented on Jan 26, 2024

    @lambdageek
    Member

    I don't see how the Clear behaviour here makes sense, seems like you effectively want it to alloc a new StringBuilder instead.

    We want to transfer ownership of hte underlying data. That's only safe if after the operation the SB itself doesn't point at the data (hence, it is "cleared").

    I'm still unclear on the scenario. Either there's a single reference to the string builder instance - in which case it's ok to have 2 separate operations - one that gives a view of the underlying chunks and a separate clear operation (different from the existing Clear which mutates the internal array).

    Or there are multiple references - in which case don't they already need some kind of coordination mechanism? Otherwise how could they possibly call Append twice and have any confidence that they're still writing to the same buffer.

    It seems the "and clear" part can be separate.

  11. jkoritzinsky commented on Mar 13, 2024

    @jkoritzinsky
    MemberAuthor

    I don't see how the Clear behaviour here makes sense, seems like you effectively want it to alloc a new StringBuilder instead.

    We want to transfer ownership of hte underlying data. That's only safe if after the operation the SB itself doesn't point at the data (hence, it is "cleared").

    I'm still unclear on the scenario. Either there's a single reference to the string builder instance - in which case it's ok to have 2 separate operations - one that gives a view of the underlying chunks and a separate clear operation (different from the existing Clear which mutates the internal array).

    This is the expected scenario. In the Roslyn source generator scenario, the user would build up the source in the StringBuilder and then pass the resulting sequence between phases (so the StringBuilder would be single-use or a pooled instance, never saved in a field for later usage based on previous state).

    Or there are multiple references - in which case don't they already need some kind of coordination mechanism? Otherwise how could they possibly call Append twice and have any confidence that they're still writing to the same buffer.

    Today, there's not a good mechanism for coordination when using the GetChunks method while also clearing the buffer, which is what this API effectively enables.

    It seems the "and clear" part can be separate.

    We could just introduce a ResetBuffers() that would release all the internal buffers instead of what "Clear" does and then Roslyn could provide the "create a ReadOnlySequence and reset" mechanism on top (possibly internally).

  12. reflectronic commented on Mar 14, 2024

    @reflectronic
    Contributor

    A similar API #72625 went from "ToImmutableAndClear" to "DrainToImmutable" in API review, perhaps this 'drain' naming pattern should be used here as well--DrainToReadOnlySequence?

  13. 7 remaining items

  14. added
    api-ready-for-review
    and removed
    needs-further-triage
    on May 1, 2026
  15. modified the milestones: Future, 11.0.0 on May 1, 2026
  16. bartonjs commented on May 5, 2026

    @bartonjs
    Member

    Video

    • After a 40 minute discussion on naming, we ended up with static MoveChunks.
    • We left out a MoveChunks(from, to) because it has questions about the behavior of to when non-empty (it can be added in the future)
    namespace System.Text;
    
    public partial class StringBuilder
    {
        public static StringBuilder MoveChunks(StringBuilder source);
    }
  17. added
    api-approved
    and removed
    api-ready-for-review
    on May 5, 2026
  18. added
    in-pr
    on May 5, 2026
  19. locked and limited conversation to collaborators on Jun 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api-approvedarea-System.Runtimein-pr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions