optimize the simple addition and substraction of small integers#730
optimize the simple addition and substraction of small integers#730
Conversation
Pull Request Test Coverage Report for Build 22676204120Details
💛 - Coveralls |
There was a problem hiding this comment.
Pull request overview
This PR adds fast paths to op_add and op_subtract in the CLVM interpreter to skip bignum arithmetic when all operands are small non-negative integers (stored as SmallAtom/NodeVisitor::U32). It also introduces two new helper methods new_u64 and new_i64 on the Allocator to create atoms from primitive Rust integer types directly.
Changes:
- Adds
Allocator::new_u64andAllocator::new_i64methods with minimal two's-complement big-endian byte encoding - Adds fast-path logic in
op_addandop_subtractthat detects all-SmallAtominputs and avoids bignum allocation - Adds comprehensive unit tests for
new_u64andnew_i64covering boundary values
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/allocator.rs |
Adds new_u64/new_i64 helper methods for direct integer-to-atom conversion, plus corresponding unit tests |
src/more_ops.rs |
Adds IIFE-based fast paths in op_add and op_subtract that accumulate in native integer types for all-SmallAtom inputs, falling back to the existing bignum slow path otherwise |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Are the savings worth the complexity here? |
We probably should upstream this to num_bigint |
I think it might be. As long as we're confident in the tests ensuring the behavior is unaffected. The common cases of small numbers are disproportionally expensive as it is. I think it might even make sense to do this to
I don't think this would fall in the scope of any bignum library, it would probably slow down the common case. |
…e we don't have to involve bignum
6f0a9bb to
923fc89
Compare
where we don't have to involve bignum
This improves performance slightly for small integer arithmetic. Specifically for
+and-.Note
Cursor Bugbot is generating a summary for commit 6f0a9bb. Configure here.