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

Streamline String.Substring #73882

Merged
merged 2 commits into from
Aug 13, 2022
Merged

Conversation

stephentoub
Copy link
Member

  • 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
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);
    }
}

- 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 stephentoub added this to the 7.0.0 milestone Aug 13, 2022
@ghost ghost assigned stephentoub Aug 13, 2022
@ghost
Copy link

ghost commented Aug 13, 2022

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

Issue Details
  • 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
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);
    }
}
Author: stephentoub
Assignees: -
Labels:

area-System.Runtime, tenet-performance

Milestone: 7.0.0

@jkotas jkotas merged commit a037f0c into dotnet:main Aug 13, 2022
@stephentoub stephentoub deleted the streamlinesubstring branch August 13, 2022 20:54
@kunalspathak
Copy link
Member

Improvements: dotnet/perf-autofiling-issues#7331

@nietras
Copy link
Contributor

nietras commented Sep 3, 2022

I guess #62577 was relevant after all. 😅

@stephentoub
Copy link
Member Author

Heh, yeah, I don't remember that one, but it looks like we did very similar things.

@ghost 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.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants