-
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
Streamline String.Substring #73882
Merged
Merged
Streamline String.Substring #73882
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
stephentoub
commented
Aug 13, 2022
- Split the one-arg Substring from the two-arg Substring to avoid unnecessary checks in the former
- Employ the same argument validation checks as Span, and then delegate to a helper that does more detailed checking to throw the right exception
- Avoid duplicative checks in the body
- Reorder checks in one-arg overload to do success paths before error paths where possible
Method | Job | Toolchain | StartIndex | Mean | Error | StdDev | Ratio | RatioSD |
---|---|---|---|---|---|---|---|---|
OneArg | Job-LMVHFB | \main\corerun.exe | 0 | 2.106 ns | 0.0840 ns | 0.0825 ns | 1.00 | 0.00 |
OneArg | Job-BIJWCO | \pr\corerun.exe | 0 | 1.418 ns | 0.0337 ns | 0.0315 ns | 0.67 | 0.02 |
TwoArgs | Job-LMVHFB | \main\corerun.exe | 0 | 1.927 ns | 0.0137 ns | 0.0114 ns | 1.00 | 0.00 |
TwoArgs | Job-BIJWCO | \pr\corerun.exe | 0 | 1.750 ns | 0.0300 ns | 0.0266 ns | 0.91 | 0.02 |
OneArg | Job-LMVHFB | \main\corerun.exe | 6 | 10.272 ns | 0.0776 ns | 0.0688 ns | 1.00 | 0.00 |
OneArg | Job-BIJWCO | \pr\corerun.exe | 6 | 9.869 ns | 0.0226 ns | 0.0189 ns | 0.96 | 0.01 |
TwoArgs | Job-LMVHFB | \main\corerun.exe | 6 | 10.231 ns | 0.0793 ns | 0.0703 ns | 1.00 | 0.00 |
TwoArgs | Job-BIJWCO | \pr\corerun.exe | 6 | 10.185 ns | 0.2332 ns | 0.1947 ns | 0.99 | 0.02 |
OneArg | Job-LMVHFB | \main\corerun.exe | 11 | 2.040 ns | 0.0252 ns | 0.0197 ns | 1.00 | 0.00 |
OneArg | Job-BIJWCO | \pr\corerun.exe | 11 | 1.838 ns | 0.0572 ns | 0.0507 ns | 0.90 | 0.03 |
TwoArgs | Job-LMVHFB | \main\corerun.exe | 11 | 2.323 ns | 0.0313 ns | 0.0293 ns | 1.00 | 0.00 |
TwoArgs | Job-BIJWCO | \pr\corerun.exe | 11 | 1.640 ns | 0.0872 ns | 0.1071 ns | 0.71 | 0.05 |
- Split the one-arg Substring from the two-arg Substring to avoid unnecessary checks in the former - Employ the same argument validation checks as Span, and then delegate to a helper that does more detailed checking to throw the right exception - Avoid duplicative checks in the body - Reorder checks in one-arg overload to do success paths before error paths where possible
stephentoub
added
area-System.Runtime
tenet-performance
Performance related issue
labels
Aug 13, 2022
ghost
assigned stephentoub
Aug 13, 2022
Tagging subscribers to this area: @dotnet/area-system-runtime Issue Details
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
public partial class Program
{
static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
private string _str = "hello world";
[Params(0, 6, 11)]
public int StartIndex { get; set; }
[Benchmark]
public string OneArg() => _str.Substring(StartIndex);
[Benchmark]
public string TwoArgs()
{
string s = _str;
int startIndex = StartIndex;
return s.Substring(startIndex, s.Length - startIndex);
}
}
|
danmoseley
approved these changes
Aug 13, 2022
tannergooding
approved these changes
Aug 13, 2022
This was referenced Aug 15, 2022
Improvements: dotnet/perf-autofiling-issues#7331 |
I guess #62577 was relevant after all. 😅 |
Heh, yeah, I don't remember that one, but it looks like we did very similar things. |
ghost
locked as resolved and limited conversation to collaborators
Oct 4, 2022
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.