Open AlexRadch opened 1 year ago
Tagging subscribers to this area: @dotnet/area-system-memory See info in area-owners.md if you want to be subscribed.
Author: | AlexRadch |
---|---|
Assignees: | - |
Labels: | `api-suggestion`, `area-System.Memory`, `untriaged` |
Milestone: | - |
Tagging subscribers to this area: @dotnet/area-system-runtime See info in area-owners.md if you want to be subscribed.
Author: | AlexRadch |
---|---|
Assignees: | - |
Labels: | `api-suggestion`, `area-System.Runtime`, `untriaged` |
Milestone: | - |
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
?
If we do this on
StringBuilder
itself, it would require foldingReadOnlySequence
into CoreLib.
Is System.Memory.dll
not included in CoreLib?
Is this achievable with the public API surface of
StringBuilder
, usingGetChunks
?
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.
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.
Is this achievable with the public API surface of
StringBuilder
, usingGetChunks
?
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.
If we do this on
StringBuilder
itself, it would require foldingReadOnlySequence
into CoreLib. Is this achievable with the public API surface ofStringBuilder
, usingGetChunks
?
I rewrote [API Proposal] based on your comments. Thank you!
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 createstring
content quickly and conveniently. Therefore, there is a desire to work with the contents of theStringBuilder
class as quickly and conveniently as with the contents of theReadOnlySequence<char>
structure.There are 3 ways to deal with the contents of a
StringBuilder
as ways as with theReadOnlySequence<char>
structure:Create a string and use it. This is not optimal.
Use the
StringBuilder.GetChunks()
method and implement the required methods yourself, repeating the methods already written for theReadOnlySequence<char>
structure. This is probably the fastest in terms of code performance, but very costly to implement and test.Convert the
StringBuilder.ChunkEnumerator
structure to aReadOnlySequence<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 theStringBuilder
class. This conversion can be done more efficiently by accessing the internal fields of theStringBuilder
class.I propose to add to the
StringBuilder
class (as an option in the extension methods) methods to get its contents as aReadOnlySequence<char>
structure.API Proposal
API Usage
Wherever the
ReadOnlySequence<T>
class is used:Alternative Designs
Repeating the methods already written for the
ReadOnlySequence<char>
structure for theStringBuilder
class.Risks
The contents of the
ReadOnlySequence<char>
structure can be changed by changing the contents of the originalStringBuilder
instance.