Skip redundant buffer finalisation on heap add/sub - #68
Closed
DRMacIver wants to merge 2 commits into
Closed
Conversation
Repr::from_buffer does pop_zeros + shrink_to_fit + match-on-len. On the in-place Add/Sub paths that start from a Large (heap) operand and stay heap-resident, the result shape is known from the operation, so that finalisation is redundant. Add a Repr::from_buffer_normalized helper (asserts the canonical-heap preconditions in debug, transmutes directly in release) and route the heap-stay-heap add/sub helpers through it, falling back to full from_buffer only where a borrow can zero the top word. Adds test_not_ibig_large, which drives the heap +1/-1 helpers (add_large_one / sub_large_one, reached via two's-complement `!` on multi-word magnitudes) including the top-word-zeroing fallback. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
DRMacIver
marked this pull request as ready for review
June 2, 2026 09:04
Owner
|
Thanks for proposing this, I think it's a little over optimized. The difference will be at the level of only one or two comparisons. Closing for now. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When adding heap-allocated integers, we can often skip reshaping the final buffer when the result shape is already known from the operation, which saves some time.
This is probably a bit marginal to be worth it TBH. It looked bigger in my initial benchmarking, and when I fixed the benchmarking most of the wins went away. Up to you whether you want to keep this - it's in the region of a 1% improvement on some operations, but it's quite fiddly for that small gain.
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 untouchedubig_mulcontrol reads 0.0% ± 0.0%, confirming a clean run.Modest but real on the heap add/sub path it targets; inline sizes and the
mul/cmpcontrols are unchanged.ubig_sub1000-bitubig_add/ubig_sub10000-bitubig_add1000-bitubig_mul(control)