question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use ReadOnlySpan<char> equivalents for string methods?

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

In #39743 I found that there were performance gains available from using someString.AsSpan().IndexOfAny(...) in the code instead of someString.IndexOfAny(...).

This raises the possibility that there are other usages of string-based operations that could potentially receive a performance boost from using equivalent operations available for spans in the MemoryExtensions class (though the original motivation was to remove the static char arrays).

To pick a specific example, IndexOf(ReadOnlySpan<char> value, StringComparison comparisonType) could be used here if AsSpan() were added:

https://github.com/dotnet/aspnetcore/blob/570bf070e95a2bc559a4b616240574dee954232c/src/Http/Http/src/BindingAddress.cs#L130

Assuming a positive improvement of a large enough magnitude (open question: how big would that be?) is available from benchmarking candidate cases (which also raises the possibility of a runtime analyzer to find such candidates in the first place?), would a larger scale adoption of this pattern (and the subsequent sprinkling of AsSpan() through the code base) be welcomed in ASP.NET Core?

If so, I’d be happy to help out with evaluating/adopting them as determined by such an exercise.

This seems to be the sort of thing where lots of small changes could add up to nice overall benefit, but comes with the potential code style issue of doing so, and the maintenance overhead of continuing to use such a pattern in the code over time.

Describe the solution you’d like

  1. Benchmark candidate call sites of string method usage that have ReadOnlySpan<T> equivalents available for each project file’s target frameworks.
  2. Update worthy candidate call sites (determined by meeting a TBD threshold of improvement) to add AsSpan() (By area? Splitting it up would prevent a giant PR summoning lots of Code Owners for review, for example).
  3. Observe net result on ASP.NET Core benchmarks once all agreed candidates updated.

Additional context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
martincostellocommented, Jan 27, 2022

@wcontayon Happy for you to take a look if you’d like to 😃

1reaction
adityamandaleekacommented, Jan 26, 2022

Triage: this might be worth pursuing on per-request codepaths, and only if the change is simple/low-impact.

The BindingAddress example above is an example of a codepath that is only executed a few times (not per-request) so we wouldn’t want to make this change there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Span<char> and string equality
Working with . NET Core that supports implicit conversions between String and ReadOnlySpan<char> , you would have: ReadOnlySpan<char> myString ...
Read more >
How to Convert a String to a Span in C# - Code Maze
In this article, we'll explore the different methods for converting a string into a character span, benchmarking our methods.
Read more >
ReadOnlySpan<T> Struct (System)
Contains <T>(ReadOnlySpan<T>, T). Indicates whether a specified value is found in a read-only span. Values are compared using IEquatable{T}.Equals(T).
Read more >
Slicing managed arrays using Span<T> | by Antão Almada
Use Span<char> or ReadOnlySpan<char> for slicing strings. Use them also as argument types so that no conversion back to string is required.
Read more >
C# 11.0 new features: Span<char> pattern matching
A string is a sequence of char values, but it's not an array. So we couldn't use it via char[] , but we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found