Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve Math.BigMul to fix Decimal compare perf regression #115182

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

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

Conversation

Daniel-Svensson
Copy link
Contributor

@Daniel-Svensson Daniel-Svensson commented Apr 30, 2025

Improve the worst case performance of Math.BigMul which will hopefully resolve #112432 (regression i Decimal comparision)
This also allows Math.BigMul to do partial constant propagation (of the low part)

The regression seems to be caused by extra stack spilling caused by usage of MultiplyNoFlags

This PR applies the workaround suggested by @tevador in #11782 (comment) (se link for benchmarks) to Math.BigMul, which removes the extra push/pop which will hopefully restore performance.

See sharplab.io for codegen (Change BigMul2 to BigMul for code in main)

New
image

Main
image

Benchmark

  1. Se in Bmi2.MultiplyNoFlags issues #11782 (comment)

  2. Benchmark for orderby with and without this pr (main is commit where decimal code was merged)

BenchmarkDotNet v0.14.1-nightly.20250107.205, Windows 11 (10.0.26100.3915)
AMD Ryzen 7 5800X 3.80GHz, 1 CPU, 16 logical and 8 physical cores
.NET SDK 10.0.100-alpha.1.25077.2
  [Host]     : .NET 10.0.0 (10.0.25.7313), X64 RyuJIT AVX2
  Job-NQDAQW : .NET 10.0.0 (42.42.42.42424), X64 RyuJIT AVX2
  Job-CICNHA : .NET 10.0.0 (42.42.42.42424), X64 RyuJIT AVX2

PowerPlanMode=00000000-0000-0000-0000-000000000000  IterationTime=250ms  MaxIterationCount=20  
MinIterationCount=15  WarmupCount=1  

Method Job Toolchain Mean Error StdDev Median Min Max Ratio RatioSD Gen0 Allocated Alloc Ratio
Order00LinqQueryX Job-NQDAQW \main\10.0.0\corerun.exe 39.28 ms 1.002 ms 1.153 ms 39.75 ms 36.01 ms 40.28 ms 1.06 0.04 3500.0000 56.65 MB 1.00
Order00LinqQueryX Job-CICNHA \pr\10.0.0\corerun.exe 37.25 ms 0.798 ms 0.919 ms 37.56 ms 34.65 ms 38.16 ms 1.00 0.04 3428.5714 56.65 MB 1.00
Order00LinqMethodX Job-NQDAQW \main\10.0.0\corerun.exe 39.09 ms 0.766 ms 0.882 ms 39.30 ms 36.25 ms 39.95 ms 1.06 0.04 3428.5714 56.65 MB 1.00
Order00LinqMethodX Job-CICNHA \pr\10.0.0\corerun.exe 36.96 ms 1.012 ms 1.082 ms 37.31 ms 33.85 ms 38.07 ms 1.00 0.04 3428.5714 56.65 MB 1.00
Order00ManualX Job-NQDAQW \main\10.0.0\corerun.exe 77.68 ms 0.331 ms 0.309 ms 77.69 ms 77.21 ms 78.11 ms 1.01 0.01 750.0000 15.26 MB 1.00
Order00ManualX Job-CICNHA \pr\10.0.0\corerun.exe 76.65 ms 0.344 ms 0.305 ms 76.68 ms 75.90 ms 77.09 ms 1.00 0.01 750.0000 15.26 MB 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 Apr 30, 2025
@Daniel-Svensson Daniel-Svensson marked this pull request as ready for review April 30, 2025 07:14
@Copilot Copilot AI review requested due to automatic review settings April 30, 2025 07:14
@Daniel-Svensson Daniel-Svensson changed the title Improve BigMul to fix VarDecCmpSub regression #11243 Improve BigMul to fix VarDecCmpSub regression #112432 Apr 30, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR aims to fix a regression in Decimal comparisons by improving the worst-case performance of Math.BigMul through reducing extra stack spilling.

  • Removed unused TARGET_32BIT branch code in BigMul(uint, uint).
  • Added conditional logic for non-ARM64 platforms and refactored BigMul methods to remove unsafe code while leveraging BMI2 support.
  • Updated comments to reflect the current implementation and performance considerations.

@Daniel-Svensson Daniel-Svensson changed the title Improve BigMul to fix VarDecCmpSub regression #112432 Improve BigMul to fix Decimal compare regression #112432 Apr 30, 2025
@Daniel-Svensson Daniel-Svensson changed the title Improve BigMul to fix Decimal compare regression #112432 Improve Math.BigMul to fix Decimal compare perf regression May 3, 2025
@Daniel-Svensson
Copy link
Contributor Author

Daniel-Svensson commented May 6, 2025

I've updated the benchmark since it seems like i had mixed up the corelib.dll for the pr and my "old" branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Diagnostics-coreclr 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.

[Perf] Windows/x64: Decimal Regressions on 2/3/2025 6:32:46 PM +00:00
1 participant