perf: optimize NonZero negation using bounded integers#9484
Open
l3ft0ver wants to merge 1 commit intostarkware-libs:mainfrom
Open
perf: optimize NonZero negation using bounded integers#9484l3ft0ver wants to merge 1 commit intostarkware-libs:mainfrom
l3ft0ver wants to merge 1 commit intostarkware-libs:mainfrom
Conversation
orizi
requested changes
Jan 16, 2026
Collaborator
orizi
left a comment
There was a problem hiding this comment.
@orizi made 1 comment.
Reviewable status: 0 of 1 files reviewed, 1 unresolved discussion (waiting on @l3ft0ver).
corelib/src/zeroable.cairo line 98 at r1 (raw file):
// This uses NegateHelper directly on bounded integers, which is more efficient // than the generic implementation that goes through Neg<T> #[feature("bounded-int-utils")]
this is much much worse solution from the original implementation with the todo.
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.
Summary
Optimizes negation for
NonZero<T>signed integer types (i8, i16, i32, i64, i128) by using bounded integers directly viaNegateHelper, avoiding the generic path throughNeg<T>. Addresses the TODO comment on line 88.Type of change
Please check one:
Why is this change needed?
The current implementation performs unnecessary conversions: unwraps
NonZero<T>toT, callsNeg<T>, then converts back. The TODO comment explicitly requests optimization using bounded integers.What was the behavior or documentation before?
Generic implementation:
NonZero<T>→T→T(viaNeg<T>) →NonZero<T>, going through the genericNeg<T>trait.What is the behavior or documentation after?
For signed integer types, uses bounded integers directly:
NonZero<T>→T→ bounded int →NegateHelper::negate()→T→NonZero<T>. Avoids intermediateNeg<T>call. Generic implementation remains forfelt252.Related issue or discussion (if any)
Addresses TODO comment on line 88:
// TODO(orizi): Optimize using bounded integers.Additional context
Gated behind
bounded-int-utilsfeature flag, consistent with other bounded integer optimizations. Maintains same behavior and error handling, including minimum value edge cases.