Skip to content

Conversation

@orizi
Copy link
Collaborator

@orizi orizi commented Jan 24, 2026

Summary

Refactored the CasmBuilder to improve handling of cell expressions by:

  1. Splitting value retrieval into get_unadjusted and get_adjusted methods
  2. Removing the adjust_ap boolean parameter in favor of explicit method names
  3. Fixing potential overflow issues in offset calculations by using to_i16() and checked_add
  4. Optimizing cell reference handling by applying AP changes more consistently

Type of change

Please check one:

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

Why is this change needed?

The current implementation of CasmBuilder has inconsistent handling of AP adjustments and cell references. The boolean adjust_ap parameter creates confusion about when adjustments should be applied. Additionally, the offset calculations for cell references could potentially overflow without proper checking.


What was the behavior or documentation before?

The code used a boolean parameter adjust_ap to determine whether to apply AP changes, leading to potential inconsistencies. Cell reference handling was also less efficient, with redundant cloning operations and unsafe offset calculations.


What is the behavior or documentation after?

The code now has explicit methods for adjusted and unadjusted values, making the intent clearer. Offset calculations use proper checked arithmetic to prevent overflows, and AP changes are applied more consistently throughout the codebase.


Additional context

This refactoring improves code maintainability by making the API more explicit about when AP adjustments are applied. It also reduces unnecessary cloning of cell expressions and adds proper bounds checking for offsets.

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Collaborator

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

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

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


crates/cairo-lang-casm/src/builder.rs line 280 at r1 (raw file):

    pub fn assert_vars_eq(&mut self, dst: Var, res: Var, kind: AssertEqKind) {
        let a = self.as_adjasted_cell_ref(dst);
        let b = self.get_adjusted(res);

shouldn't this be unadjusted?

@orizi orizi changed the base branch from orizi/01-23-improve_sierra_debug_info_serde to graphite-base/9526 January 25, 2026 12:23
@orizi orizi force-pushed the graphite-base/9526 branch from 8573a25 to cb68427 Compare January 25, 2026 12:46
@orizi orizi force-pushed the orizi/01-24-performance_sierra-to-casm_removed_cellexpression_unrequired_clone branch from 7f15bec to ecc7d3c Compare January 25, 2026 12:46
Copy link
Collaborator Author

@orizi orizi left a comment

Choose a reason for hiding this comment

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

@orizi made 1 comment.
Reviewable status: 1 of 2 files reviewed, 1 unresolved discussion (waiting on @eytan-starkware and @TomerStarkware).


crates/cairo-lang-casm/src/builder.rs line 280 at r1 (raw file):

Previously, TomerStarkware wrote…

shouldn't this be unadjusted?

if i do - we require to 'readjust' as well. attepted.

@orizi orizi changed the base branch from graphite-base/9526 to orizi/01-23-improve_sierra_debug_info_serde January 25, 2026 12:46
@orizi orizi force-pushed the orizi/01-23-improve_sierra_debug_info_serde branch from cb68427 to 5681777 Compare January 25, 2026 12:56
@orizi orizi force-pushed the orizi/01-24-performance_sierra-to-casm_removed_cellexpression_unrequired_clone branch from ecc7d3c to 73878a2 Compare January 25, 2026 12:56
Copy link
Collaborator

@TomerStarkware TomerStarkware left a comment

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, made 1 comment, and resolved 1 discussion.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @eytan-starkware).

@orizi orizi changed the base branch from orizi/01-23-improve_sierra_debug_info_serde to graphite-base/9526 January 25, 2026 16:26
@orizi orizi force-pushed the graphite-base/9526 branch from 5681777 to aa81a46 Compare January 25, 2026 16:26
@orizi orizi force-pushed the orizi/01-24-performance_sierra-to-casm_removed_cellexpression_unrequired_clone branch from 73878a2 to 377f341 Compare January 25, 2026 16:27
@orizi orizi changed the base branch from graphite-base/9526 to main January 25, 2026 16:27
@orizi orizi force-pushed the orizi/01-24-performance_sierra-to-casm_removed_cellexpression_unrequired_clone branch 2 times, most recently from aad259a to 6c9ba5c Compare January 25, 2026 17:47
@orizi orizi force-pushed the orizi/01-24-performance_sierra-to-casm_removed_cellexpression_unrequired_clone branch from 6c9ba5c to cbffbe8 Compare January 26, 2026 17:10
Done by refactoring the basic callbacks.

SIERRA_UPDATE_PATCH_CHANGE_TAG=No interface changes.
@orizi orizi force-pushed the orizi/01-24-performance_sierra-to-casm_removed_cellexpression_unrequired_clone branch from cbffbe8 to 0cc98d1 Compare January 27, 2026 14:47
Copy link
Collaborator

@TomerStarkware TomerStarkware left a comment

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 Jan 27, 2026
Merged via the queue into main with commit 37a1fca Jan 27, 2026
55 checks passed
@orizi orizi deleted the orizi/01-24-performance_sierra-to-casm_removed_cellexpression_unrequired_clone branch January 27, 2026 16:07
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