Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API Proposal]: Add access to StringBuilder content by using ReadOnlySequence<char> #87362

Open
AlexRadch opened this issue Jun 10, 2023 · 7 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration
Milestone

Comments

@AlexRadch
Copy link
Contributor

AlexRadch commented Jun 10, 2023

Background and motivation

There are many methods for working with the contents by using the ReadOnlySequence<char> structure:

There are no such methods for the StringBuilder class, and it is often used as a buffer to create string content quickly and conveniently. Therefore, there is a desire to work with the contents of the StringBuilder class as quickly and conveniently as with the contents of the ReadOnlySequence<char> structure.

There are 3 ways to deal with the contents of a StringBuilder as ways as with the ReadOnlySequence<char> structure:

  1. Create a string and use it. This is not optimal.

  2. Use the StringBuilder.GetChunks() method and implement the required methods yourself, repeating the methods already written for the ReadOnlySequence<char> structure. This is probably the fastest in terms of code performance, but very costly to implement and test.

  3. Convert the StringBuilder.ChunkEnumerator structure to a ReadOnlySequence<char> structure and use it further in the already written methods for working with buffers and memory. Implementing such a conversion in third-party libraries will not be the most efficient without access to the internal fields of the StringBuilder class. This conversion can be done more efficiently by accessing the internal fields of the StringBuilder class.

I propose to add to the StringBuilder class (as an option in the extension methods) methods to get its contents as a ReadOnlySequence<char> structure.

API Proposal

namespace System.Text;

public class StringBuilder
{
    public ChunkReverseEnumerator GetReverseChunks(); // will allow us to create performance and memory-efficient code
}

// Assembly: System.Memory.dll
namespace System.Buffers;

public static class BuffersExtensions
{
    public static ReadOnlySequence<char> AsSequence(this StringBuilder builder); // OR GetSequence(); OR ToSequence();
    public static ReadOnlySequence<char> AsSequence(this StringBuilder builder, int startIndex, int length);
}

API Usage

Wherever the ReadOnlySequence<T> class is used:

Alternative Designs

Repeating the methods already written for the ReadOnlySequence<char> structure for the StringBuilder class.

Risks

The contents of the ReadOnlySequence<char> structure can be changed by changing the contents of the original StringBuilder instance.

@AlexRadch AlexRadch added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jun 10, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jun 10, 2023
@ghost
Copy link

ghost commented Jun 10, 2023

Tagging subscribers to this area: @dotnet/area-system-memory
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

There are many methods for working with the contents by using the ReadOnlySequence<char> structure:

There are no such methods for the StringBuilder class, and it is often used as a buffer to create string content quickly and conveniently. Therefore, there is a desire to work with the contents of the StringBuilder class as quickly and conveniently as with the contents of the ReadOnlySequence<char> structure.

There are 3 ways to deal with the contents of a StringBuilder as ways as with the ReadOnlySequence<char> structure:

  1. Create a string and use it. This is not optimal.

  2. Use the StringBuilder.GetChunks() method and implement the required methods yourself, repeating the methods already written for the ReadOnlySequence<char> structure. This is probably the fastest in terms of code performance, but very costly to implement and test.

  3. Convert the StringBuilder.ChunkEnumerator structure to a ReadOnlySequence<char> structure and use it further in the already written methods for working with buffers and memory. Implementing such a conversion in third-party libraries will not be the most efficient without access to the internal fields of the StringBuilder class. This conversion can be done more efficiently by accessing the internal fields of the StringBuilder class.

I propose to add to the StringBuilder class (as an option in the extension methods) methods to get its contents as a ReadOnlySequence<char> structure.

API Proposal

namespace System.Text;

public class StringBuilder: IEnumerable<T>
{
    public ReadOnlySequence<char> AsSequence(); // OR GetSequence();
    public ReadOnlySequence<char> AsSequence(int startIndex, int length);
}

// OR

public static class StringBuilderExtensions
{
    public static ReadOnlySequence<char> AsSequence(this StringBuilder builder);
    public static ReadOnlySequence<char> AsSequence(this StringBuilder builder, int startIndex, int length);
}

API Usage

Wherever the ReadOnlySequence<T> class is used:

Alternative Designs

Repeating the methods already written for the ReadOnlySequence<char> structure for the StringBuilder class.

Risks

The contents of the ReadOnlySequence<char> structure can be changed by changing the contents of the original StringBuilder instance.

Author: AlexRadch
Assignees: -
Labels:

api-suggestion, area-System.Memory, untriaged

Milestone: -

@ghost
Copy link

ghost commented Jun 10, 2023

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

There are many methods for working with the contents by using the ReadOnlySequence<char> structure:

There are no such methods for the StringBuilder class, and it is often used as a buffer to create string content quickly and conveniently. Therefore, there is a desire to work with the contents of the StringBuilder class as quickly and conveniently as with the contents of the ReadOnlySequence<char> structure.

There are 3 ways to deal with the contents of a StringBuilder as ways as with the ReadOnlySequence<char> structure:

  1. Create a string and use it. This is not optimal.

  2. Use the StringBuilder.GetChunks() method and implement the required methods yourself, repeating the methods already written for the ReadOnlySequence<char> structure. This is probably the fastest in terms of code performance, but very costly to implement and test.

  3. Convert the StringBuilder.ChunkEnumerator structure to a ReadOnlySequence<char> structure and use it further in the already written methods for working with buffers and memory. Implementing such a conversion in third-party libraries will not be the most efficient without access to the internal fields of the StringBuilder class. This conversion can be done more efficiently by accessing the internal fields of the StringBuilder class.

I propose to add to the StringBuilder class (as an option in the extension methods) methods to get its contents as a ReadOnlySequence<char> structure.

API Proposal

namespace System.Text;

public class StringBuilder
{
    public ReadOnlySequence<char> AsSequence(); // OR GetSequence(); OR ToSequence();
    public ReadOnlySequence<char> AsSequence(int startIndex, int length);
}

// OR

public static class StringBuilderExtensions
{
    public static ReadOnlySequence<char> AsSequence(this StringBuilder builder);
    public static ReadOnlySequence<char> AsSequence(this StringBuilder builder, int startIndex, int length);
}

API Usage

Wherever the ReadOnlySequence<T> class is used:

Alternative Designs

Repeating the methods already written for the ReadOnlySequence<char> structure for the StringBuilder class.

Risks

The contents of the ReadOnlySequence<char> structure can be changed by changing the contents of the original StringBuilder instance.

Author: AlexRadch
Assignees: -
Labels:

api-suggestion, area-System.Runtime, untriaged

Milestone: -

@huoyaoyuan
Copy link
Member

If we do this on StringBuilder itself, it would require folding ReadOnlySequence into CoreLib.
Is this achievable with public api surface of StringBuilder, using GetChunks?

@AlexRadch
Copy link
Contributor Author

If we do this on StringBuilder itself, it would require folding ReadOnlySequence into CoreLib.

Is System.Memory.dll not included in CoreLib?

Is this achievable with the public API surface of StringBuilder, using GetChunks?

GetChunks can be used, but such code will be less performance and consume more memory than code that has access to the StringBuilder private fields.

@huoyaoyuan
Copy link
Member

Is System.Memory.dll not included in CoreLib?

CoreLib is referenced by everything, and can't reference anything else. It is a single assembly file and doesn't "include" anything. Don't confuse with shared framework.

@AlexRadch
Copy link
Contributor Author

Is this achievable with the public API surface of StringBuilder, using GetChunks?

If add a new GetChunks method that will return chunks in reverse order, then such code can be created in any place and it will be performance and memory effective.

@AlexRadch
Copy link
Contributor Author

If we do this on StringBuilder itself, it would require folding ReadOnlySequence into CoreLib. Is this achievable with the public API surface of StringBuilder, using GetChunks?

I rewrote [API Proposal] based on your comments. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration
Projects
None yet
Development

No branches or pull requests

5 participants