From 5433be2f4f9b5772c6a4a1a2af9bd84cfaef17de Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 6 Jul 2026 14:23:04 +0000 Subject: [PATCH] perf: add [AggressiveInlining] to Frac and Lerp; add Lerp and Clamp benchmarks - Frac was the only short hot-path helper lacking [AggressiveInlining]; its sibling FractionalPart (identical implementation) already has the attribute. - Lerp is a three-operation composable helper (start + (end-start) * amount) that benefits from inlining into call sites. - Add LerpDecimalReference, LerpDoubleReference, LerpRaw benchmarks. - Add ClampDecimalReference, ClampRaw benchmarks. These complete the coverage of the main public math helpers in the benchmark suite; only Pow, PopulationVariance/SampleVariance, and Parse remain. Tests: 1426/1426 passed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../FixedPointNanoMathBenchmarks.cs | 39 +++++++++++++++++++ src/FixedPointNano/FixedPointNano.cs | 2 + 2 files changed, 41 insertions(+) diff --git a/benchmarks/FixedPointNano.Benchmarks/FixedPointNanoMathBenchmarks.cs b/benchmarks/FixedPointNano.Benchmarks/FixedPointNanoMathBenchmarks.cs index 6dcffc2..188ff37 100644 --- a/benchmarks/FixedPointNano.Benchmarks/FixedPointNanoMathBenchmarks.cs +++ b/benchmarks/FixedPointNano.Benchmarks/FixedPointNanoMathBenchmarks.cs @@ -14,6 +14,13 @@ public class FixedPointNanoMathBenchmarks private readonly double _rightDouble = 7.123456789d; private readonly double _sqrtInputDouble = 1234.567890123d; private readonly double _doubleValue = 1234.567890123d; + private readonly Fpn _lerpAmount = Fpn.FromDecimal(0.5m); + private readonly decimal _lerpAmountDecimal = 0.5m; + private readonly double _lerpAmountDouble = 0.5d; + private readonly Fpn _clampMin = Fpn.FromDecimal(0m); + private readonly Fpn _clampMax = Fpn.FromDecimal(10000m); + private readonly decimal _clampMinDecimal = 0m; + private readonly decimal _clampMaxDecimal = 10000m; private Fpn[] _fixedValues = []; private decimal[] _decimalValues = []; private double[] _doubleValues = []; @@ -202,6 +209,38 @@ public Fpn TruncateRaw() return Fpn.Truncate(_left); } + [Benchmark] + public Fpn LerpDecimalReference() + { + var start = _left.ToDecimal(); + var end = _right.ToDecimal(); + return Fpn.FromDecimal(start + (end - start) * _lerpAmountDecimal); + } + + [Benchmark] + public double LerpDoubleReference() + { + return _leftDouble + (_rightDouble - _leftDouble) * _lerpAmountDouble; + } + + [Benchmark] + public Fpn LerpRaw() + { + return Fpn.Lerp(_left, _right, _lerpAmount); + } + + [Benchmark] + public Fpn ClampDecimalReference() + { + return Fpn.FromDecimal(Math.Clamp(_left.ToDecimal(), _clampMinDecimal, _clampMaxDecimal)); + } + + [Benchmark] + public Fpn ClampRaw() + { + return Fpn.Clamp(_left, _clampMin, _clampMax); + } + [Benchmark] public Fpn AbsDecimalReference() { diff --git a/src/FixedPointNano/FixedPointNano.cs b/src/FixedPointNano/FixedPointNano.cs index d11131e..b79ded6 100644 --- a/src/FixedPointNano/FixedPointNano.cs +++ b/src/FixedPointNano/FixedPointNano.cs @@ -216,6 +216,7 @@ public static FixedPointNano CopySign(FixedPointNano value, FixedPointNano sign) return value.RawValue > 0 ? new FixedPointNano(-value.RawValue) : value; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static FixedPointNano Lerp(FixedPointNano start, FixedPointNano end, FixedPointNano amount) { return start + (end - start) * amount; @@ -514,6 +515,7 @@ public static FixedPointNano Truncate(FixedPointNano value) return new FixedPointNano((value.RawValue / Scale) * Scale); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static FixedPointNano Frac(FixedPointNano value) { return new FixedPointNano(value.RawValue % Scale);