-
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
Always inline number conversions #112061
base: main
Are you sure you want to change the base?
Always inline number conversions #112061
Conversation
Tagging subscribers to this area: @dotnet/area-system-numerics |
@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);
}
} |
Benchmark on linux_azure_genoa:
Benchmark on linux_azure_cascadelake:
|
@hez2010 you should run a benchmark that isn't constant foldable as well; so more accurate numbers can be given. |
@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);
}
} |
Saved the int to a class field to prevent constant folding and make it always load from the address.
|
We applied
AggressiveInlining
for mostTryConvert*
, 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
.