Skip to content

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.

@ghost ghost added the area-System.Numerics label Feb 1, 2025
@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

@tannergooding tannergooding merged commit 4bd597f into dotnet:main Feb 2, 2025
142 of 144 checks passed
grendello added a commit to grendello/runtime that referenced this pull request Feb 3, 2025
* main:
  System.Net.Http.WinHttpHandler.StartRequestAsync assertion failed (dotnet#109799)
  Keep test PDB in helix payload for native AOT (dotnet#111949)
  Build the RID-specific System.IO.Ports packages in the VMR (dotnet#112054)
  Always inline number conversions (dotnet#112061)
  Use Contains{Any} in Regex source generator (dotnet#112065)
  Update dependencies from https://github.com/dotnet/arcade build 20250130.5 (dotnet#112013)
  JIT: Transform single-reg args to FIELD_LIST in physical promotion (dotnet#111590)
  Ensure that math calls into the CRT are tracked as needing vzeroupper (dotnet#112011)
  Use double.ConvertToIntegerNative where safe to do in System.Random (dotnet#112046)
  JIT: Compute `fgCalledCount` after synthesis (dotnet#112041)
  Simplify boolean logic in `TimeZoneInfo` (dotnet#112062)
  JIT: Update type when return temp is freshly created (dotnet#111948)
  Remove unused build controls and simplify DotNetBuild.props (dotnet#111986)
  Fix case-insensitive JSON deserialization of enum member names (dotnet#112028)
  WasmAppBuilder: Remove double computation of a value (dotnet#112047)
  Disable LTCG for brotli and zlibng. (dotnet#111805)
  JIT: Improve x86 unsigned to floating cast codegen (dotnet#111595)
  simplify x86 special intrinsic imports (dotnet#111836)
  JIT: Try to retain entry weight during profile synthesis (dotnet#111971)
  Fix explicit offset of ByRefLike fields. (dotnet#111584)
@github-actions github-actions bot locked and limited conversation to collaborators Mar 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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