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

Always inline number conversions #112061

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

hez2010
Copy link
Contributor

@hez2010 hez2010 commented Feb 1, 2025

We applied AggressiveInlining for most TryConvert*, except several leftovers.

Apply it for them as well.

Otherwise they will not be inlined: https://godbolt.org/z/h5dz6sKa5

Searched with bool\s+.*TryConvert.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Feb 1, 2025
Copy link
Contributor

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

@hez2010
Copy link
Contributor Author

hez2010 commented Feb 1, 2025

@MihuBot

@hez2010
Copy link
Contributor Author

hez2010 commented Feb 1, 2025

@EgorBot -intel

using System.Numerics;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

// Actual runner is optional, but if it exists, it has to be like this:
BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);

public class Bench
{
    [Benchmark]
    public double GetDouble() => CastNumber<double>(42);

    static T CastNumber<T>(int v) where T : INumberBase<T>
    {
        return T.CreateTruncating(v);
    }
}

@hez2010
Copy link
Contributor Author

hez2010 commented Feb 1, 2025

Benchmark on linux_azure_genoa:

Method Toolchain Mean Error Ratio
GetDouble Main 1.0728 ns 0.0001 ns 1.000
GetDouble PR 0.0000 ns 0.0000 ns 0.000

Benchmark on linux_azure_cascadelake:

Method Toolchain Mean Error Ratio
GetDouble Main 0.8452 ns 0.0005 ns 1.00
GetDouble PR 0.0179 ns 0.0001 ns 0.02

@tannergooding
Copy link
Member

@hez2010 you should run a benchmark that isn't constant foldable as well; so more accurate numbers can be given.

@hez2010
Copy link
Contributor Author

hez2010 commented Feb 1, 2025

@EgorBot -intel

using System.Numerics;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

// Actual runner is optional, but if it exists, it has to be like this:
BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);

public class Bench
{
    private int v = 42;

    [Benchmark]
    public double GetDouble() => CastNumber<double>(this.v);

    static T CastNumber<T>(int v) where T : INumberBase<T>
    {
        return T.CreateTruncating(v);
    }
}

@hez2010
Copy link
Contributor Author

hez2010 commented Feb 1, 2025

Saved the int to a class field to prevent constant folding and make it always load from the address.

Method Toolchain Mean Error Ratio
GetDouble Main 1.1684 ns 0.0002 ns 1.000
GetDouble PR 0.0000 ns 0.0000 ns 0.000

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