-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Add a StringSegment type #22004
Comments
ASP.NET @Tratcher @pranavkm @rynowak @JunTaoLuo @javiercn @davidfowl |
We should add an implicit conversion to Span<char> as well. |
It's really unfortunate that there needs to be so many different types to essentially do the same thing. Is it really necessary?
Can you expand on these constraints? Are you talking about stack-only? |
I would also consider things like StringTokenizer https://github.com/aspnet/Common/blob/7c7031f145413221be316719683edb0bf1b9bf40/src/Microsoft.Extensions.Primitives/StringTokenizer.cs In general, any abstraction that allows classic string operations but without allocating. |
Your proposed API shape includes StringTokenizer: public StringTokenizer Split(char[] chars); Is that intentional? |
So I'm trying to get a picture: if I want to do slicing I'll always use Then, when I bring the How close am I? |
Cc @ stephentoub |
@stephentoub , rather. |
@KrzysztofCwalina, could we make Regardless of whether we end up using |
I think it boils down to whether we are ok with this segment type being subject to struct tearing. |
@KrzysztofCwalina, can you elaborate? Both StringSegment and ReadOnlyMemory are multi-field structs, both subject to tearing. Or are you saying you'd make StringSegment a class? |
Sorry, I was just first trying to decide between span like type vs memory like type. |
However should be conversion from |
Maybe, but |
That could work :) |
Side note, I'd guestimate that in building large solutions 30% - 40% of all MSBuild string allocations are short-lived and would greatly benefit from a StringSegment-like type. I'm prototyping some changes and have just directly grabbed ASP.NET's version of this to see how we can benefit from this. To full see the benefit here for us - I'd like to see allocation-free Path APIs along with this; both MSBuild and VS make large amount of short-lived strings via |
@JeremyKuhne for the Path-related request ^ |
Is this now covered by |
I believe so. |
Closing then |
Is there a diff that shows what APIs we ended up? I'm interested in making sure we have string-like APIs and equivalent APIs on Path. |
One demonstrative excersise would be to convert the internals of https://github.com/aspnet/Common/blob/dev/src/Microsoft.Extensions.Primitives/StringSegment.cs and see if all of the functionality is available. |
From a quick skim, I expect all of the needed functionality is there with the exception of:
|
ASP.NET Core has a type called StringSegment. It allows substring operations on string to not allocate but return segments using offset and length.
It's the spiritual equivalent of
ArraySegment<T>
for string. Note thatSpan<T>
will also allow slicing, but spans have different type system constraints that not all consumers can -- or want to -- opt into.Proposed API Shape
Starting point is the existing API shape from ASP.NET. We should, however, diff it against the latest surface of
string
.Other APIs
In order to make the API useful, we could also define methods on
String
that allow returning segments, e.g.The text was updated successfully, but these errors were encountered: