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

Avoid an allocation in BigInteger.Parse("-2147483648") #104666

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kzrnm
Copy link
Contributor

@kzrnm kzrnm commented Jul 10, 2024

Possibly related to #97589 (comment)

Before the change, this = s_bnMinInt; was unreachable.

Benchmark

[DisassemblyDiagnoser]
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByMethod)]
public class ParseSmall
{
    [Params(
        "-2147483648",
        "-2147483647",
        "2147483647",
        "2147483648"
    )]
    public string N;

    [Benchmark] public BigInteger Parse() => BigInteger.Parse(N);
}

BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3810/23H2/2023Update/SunValley3)
13th Gen Intel Core i5-13500, 1 CPU, 20 logical and 14 physical cores
.NET SDK 9.0.100-preview.5.24307.3
  [Host]  : .NET 9.0.0 (9.0.24.30607), X64 RyuJIT AVX2
  LongRun : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX2

Job=LongRun  IterationCount=100  LaunchCount=3  
WarmupCount=15  

Method Toolchain N Mean Error StdDev Median Ratio RatioSD Code Size Gen0 Allocated Alloc Ratio
Parse \main\corerun.exe -2147483647 54.29 ns 0.118 ns 0.599 ns 54.10 ns 1.00 0.00 1,226 B - - NA
Parse \pr\corerun.exe -2147483647 53.41 ns 0.053 ns 0.270 ns 53.39 ns 0.98 0.01 1,226 B - - NA
Parse \main\corerun.exe -2147483648 58.50 ns 0.144 ns 0.740 ns 58.67 ns 1.00 0.00 1,226 B 0.0025 32 B 1.00
Parse \pr\corerun.exe -2147483648 55.90 ns 0.067 ns 0.346 ns 55.84 ns 0.96 0.02 1,226 B - - 0.00
Parse \main\corerun.exe 2147483647 52.68 ns 0.048 ns 0.242 ns 52.66 ns 1.00 0.00 1,226 B - - NA
Parse \pr\corerun.exe 2147483647 52.39 ns 0.070 ns 0.358 ns 52.32 ns 0.99 0.01 1,226 B - - NA
Parse \main\corerun.exe 2147483648 58.38 ns 0.187 ns 0.954 ns 58.12 ns 1.00 0.00 1,226 B 0.0025 32 B 1.00
Parse \pr\corerun.exe 2147483648 57.65 ns 0.097 ns 0.491 ns 57.59 ns 0.99 0.02 1,226 B 0.0025 32 B 1.00

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Numerics community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants