Sitelet https://github.com/starkware-libs/cairo/pull/10036
Skip to content

perf(corelib): make Take::advance_by arithmetic panic-free - #10036

Merged
orizi merged 1 commit into
mainfrom
orizi/06-04-perf_corelib_reset_take_advance_by_counter_when_budget_is_exhausted
Jun 7, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/06-04-perf_corelib_reset_take_advance_by_counter_when_budget_is_exhausted

Conversation

@orizi

@orizi orizi commented Jun 4, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Refactors the advance_by implementation for the Take iterator adapter to use checked_add and checked_sub arithmetic operations instead of direct addition and subtraction, and restructures the control flow to set self.n = 0 eagerly before calling the inner iterator's advance_by.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

Why is this change needed?

The previous implementation used unchecked arithmetic when restoring self.n after a partial advance and when computing the number of taken/untaken elements. This could lead to incorrect behavior in edge cases with improperly implemented inner iterators that return unexpected remainder values. The old code also used an unwrap-equivalent pattern that generated unnecessary code paths.


What was the behavior or documentation before?

advance_by used direct arithmetic (self.n += rem_nz.into(), available - self.n) without overflow or underflow protection. The self.n = 0 assignment was deferred inside the match arm rather than set upfront.


What is the behavior or documentation after?

self.n is set to 0 eagerly before calling the inner iterator's advance_by. All arithmetic uses checked_add and checked_sub, with early returns on unexpected values from misbehaving inner iterators. Comments clarify why overflow cannot occur in the well-behaved case and why the fallback Ok(()) paths exist as safeguards.


Related issue or discussion (if any)


Additional context

The behavioral guarantees for correctly implemented iterators are unchanged. The checked arithmetic paths serve as defensive guards against iterator implementations that violate the contract of advance_by.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@orizi
orizi marked this pull request as ready for review June 4, 2026 10:05
@cursor

cursor Bot commented Jun 4, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Iterator adapter internals only; behavior for conforming inner iterators is intended to stay the same, with stricter guards for contract violations.

Overview
Refactors Take::advance_by so budget accounting uses checked_add / checked_sub instead of unchecked math, and clears self.n to 0 before advancing the inner iterator when the request exceeds the remaining take count (so later next / count do not touch an exhausted inner).

On partial inner failure, the take budget is restored via checked refund logic; impossible arithmetic from a misbehaving inner iterator returns a fixed INNER_ERR_MARKER instead of the old try_into → Ok(()) fallback. Inline comments document the well-behaved paths.

Reviewed by Cursor Bugbot for commit ce80687. Bugbot is set up for automated code reviews on this repo. Configure here.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5d97fa113a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread corelib/src/iter/adapters/take.cairo Outdated
@orizi
orizi force-pushed the orizi/06-04-perf_corelib_reset_take_advance_by_counter_when_budget_is_exhausted branch from 5d97fa1 to 57f25e9 Compare June 4, 2026 14:44

@TomerStarkware TomerStarkware left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

@TomerStarkware reviewed 1 file and all commit messages, and made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on eytan-starkware and orizi).

@orizi
orizi force-pushed the orizi/06-04-perf_corelib_reset_take_advance_by_counter_when_budget_is_exhausted branch from 57f25e9 to ebc293e Compare June 7, 2026 09:01

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi resolved 1 discussion.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on eytan-starkware).

  Replace the panicking `-`/`+=` in advance_by with checked_sub/checked_add so
  the overflow->panic branches aren't emitted into the generated Sierra (the
  subtractions can't underflow and the addition can't overflow for a correct
  inner iterator). Measured ~3 fewer Sierra statements on a generic caller.
  checked_* is used rather than saturating_* (the latter compiles larger).

  Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@orizi
orizi force-pushed the orizi/06-04-perf_corelib_reset_take_advance_by_counter_when_budget_is_exhausted branch from ebc293e to ce80687 Compare June 7, 2026 09:09

@TomerStarkware TomerStarkware left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

@TomerStarkware reviewed 1 file and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on eytan-starkware).

@orizi
orizi added this pull request to the merge queue Jun 7, 2026
Merged via the queue into main with commit 835db85 Jun 7, 2026
54 checks passed
@orizi
orizi deleted the orizi/06-04-perf_corelib_reset_take_advance_by_counter_when_budget_is_exhausted branch June 7, 2026 11:24
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.

3 participants