Skip to content

Conversation

@headius
Copy link
Member

@headius headius commented Dec 13, 2024

The call to Math.multiplyExact works very well when the result does not overflow, skipping range checks and using the host CPU's support for overflow checks. However when we are dancing on the range boundary and multiplyExact fails repeatedly, the cost of raising all those exceptions massively outweighs the gains.

This is the primary cause of performance issues reported in #8516.

This patch reverts to a version of the old range-checking logic. Performance on the bug report's benchmark is an order of magnitude faster for the two cases that seemed to overflow a lot, and the other cases do not seem to degrade.

Fixes #8516

@headius headius added this to the JRuby 9.4.10.0 milestone Dec 13, 2024
@headius
Copy link
Member Author

headius commented Dec 13, 2024

Note that JRuby 10 could use a different patch proposed in #8516 that uses Math.multiplyHigh as an overflow check before doing the exact operation. That may be no faster than this, but it has the potential to reduce to two intrinsic Math calls rather than always using our hand-written logic.

@headius
Copy link
Member Author

headius commented Dec 19, 2024

Ok clearly I botched this. Will circle back.

@headius headius closed this Dec 19, 2024
@headius headius reopened this Dec 19, 2024
The call to Math.multiplyExact works very well when the result
does not overflow, skipping range checks and using the host CPU's
support for overflow checks. However when we are dancing on the
range boundary and multiplyExact fails repeatedly, the cost of
raising all those exceptions massively outweighs the gains.

This is the primary cause of performance issues reported in
jruby#8516.

This patch reverts to a version of the old range-checking logic.
Performance on the bug report's benchmark is an order of magnitude
faster for the two cases that seemed to overflow a lot, and the
other cases do not seem to degrade.

Fixes jruby#8516
@headius headius force-pushed the avoid_multiplyexact_exception branch from 663b0ac to b6f3b05 Compare January 10, 2025 21:51
@headius
Copy link
Member Author

headius commented Jan 10, 2025

New patch looks clean enough. I'm only doing this one targeted fix since we have alternatives in JRuby 10 using Java 21.

@headius headius merged commit 42197f4 into jruby:master Jan 10, 2025
93 of 94 checks passed
@headius headius deleted the avoid_multiplyexact_exception branch January 10, 2025 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Strange performance difference?

1 participant