Skip to content

Bypass the dispatch chain for inline-value comparison#70

Open
DRMacIver wants to merge 3 commits into
cmpute:masterfrom
DRMacIver:opt/p7-cmp-dispatch-bypass
Open

Bypass the dispatch chain for inline-value comparison#70
DRMacIver wants to merge 3 commits into
cmpute:masterfrom
DRMacIver:opt/p7-cmp-dispatch-bypass

Conversation

@DRMacIver

@DRMacIver DRMacIver commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Optimisations for order comparisons that bypass expensive operations on the fast path. Gives a 22-26% speedup on some of my benchmarks.

Benchmarks (layout-sampled, K=11)

Wall-clock, dashu-only, vs the merge base, measured under 11 randomised function layouts (ld64 -order_file) and reported as the median — to average out the ±10% code-placement noise that otherwise swamps these small-value micro-benchmarks. The untouched ubig_mul control reads 0.0% ± 0.0%, confirming a clean run.

Benchmark Δ
ubig_cmp (all classes) −22% to −25%
ubig_min (one/two-word) −24% to −26%
ubig_mul (control) 0.0%

DRMacIver and others added 2 commits June 1, 2026 11:53
UBig/IBig ordering went through as_sign_typed -> TypedReprRef::cmp, which
materialises slice references even for inline (<= DoubleWord) values.

Add Repr::magnitude_cmp (compares inline values as a single DoubleWord,
falls through to length-then-words for heap) and Repr::signed_cmp (adds
sign handling on top, using the canonical-positive-zero invariant).
UBig::cmp now calls magnitude_cmp directly (capacities are always
positive) and IBig::cmp calls signed_cmp. cmp_in_place gains #[inline].

The existing cmp / num_order tests exercise every branch (mixed
inline/heap operands and both signs).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Directly exercises magnitude_cmp / signed_cmp on equal-scale-different-value
pairs and the inline/heap boundary, for both UBig and IBig, rather than
relying on incidental coverage from the existing cmp tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@DRMacIver DRMacIver marked this pull request as ready for review June 2, 2026 08:24
@cmpute

cmpute commented Jun 3, 2026

Copy link
Copy Markdown
Owner

I think this problem needs more investigation. I have written a whole bunch of cmp()s for TypedReprRef instances. We should investigate and try optimizing that instead of just skip the repr's cmp(), and thus it can benefit every comparisons involved in the dashu-int crate.

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.

2 participants