Skip to content

Conversation

@orizi
Copy link
Collaborator

@orizi orizi commented Jan 25, 2026

Summary

Optimized the rectify method in PreCost implementation by replacing a map-and-max operation with a filter-map approach. Instead of iterating through all values and replacing negative values with zero, the new implementation simply filters out negative values entirely, keeping only non-negative entries in the resulting cost map.


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 current implementation of rectify in PreCost iterates through all values and replaces negative values with zero. This is inefficient since we're keeping entries with zero values in the map. The new implementation filters out negative values entirely, which reduces the size of the resulting map and improves performance.


What was the behavior or documentation before?

Previously, the rectify method would iterate through all entries in the cost map and replace any negative values with zero, keeping all entries in the map.


What is the behavior or documentation after?

Now, the rectify method filters out any entries with negative values, keeping only entries with non-negative values in the resulting map. This reduces the map size and improves performance.


Additional context

This change maintains the same functional behavior while making the code more efficient. The Sierra gas computation will produce the same results, but with potentially fewer entries in the cost maps.

Copy link
Collaborator Author

orizi commented Jan 25, 2026

@reviewable-StarkWare
Copy link

This change is Reviewable

@orizi orizi marked this pull request as ready for review January 25, 2026 17:49
Copy link
Contributor

@eytan-starkware eytan-starkware left a comment

Choose a reason for hiding this comment

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

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


crates/cairo-lang-sierra-gas/src/compute_costs.rs line 110 at r1 (raw file):

        // Keys are unique since we're iterating over value's keys.
        PreCost(CostTokenMap::unchecked_from_iter(
            value.0.iter().filter_map(|(k, v)| (*v >= 0).then_some((*k, *v))),

Why is it ok to have a non-full map here? Shouldn't rectify have a zero default for anything negative?

@orizi orizi force-pushed the orizi/01-25-performance_sierra-gas_smaller_equivalent_costs_map branch from e6b5dfc to e612c14 Compare January 27, 2026 14:47
@orizi orizi force-pushed the orizi/01-24-performance_sierra-gas_added_unchecked_initialization_improving_runtime_marginally branch from 7cc82db to d56b482 Compare January 27, 2026 14:47
SIERRA_UPDATE_PATCH_CHANGE_TAG=No interface change.
@orizi orizi force-pushed the orizi/01-25-performance_sierra-gas_smaller_equivalent_costs_map branch from e612c14 to d0f1a87 Compare January 27, 2026 16:00
@orizi orizi force-pushed the orizi/01-24-performance_sierra-gas_added_unchecked_initialization_improving_runtime_marginally branch from d56b482 to b7b22af Compare January 27, 2026 16:00
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: 0 of 1 files reviewed, 1 unresolved discussion (waiting on @eytan-starkware and @TomerStarkware).


crates/cairo-lang-sierra-gas/src/compute_costs.rs line 110 at r1 (raw file):

Previously, eytan-starkware wrote…

Why is it ok to have a non-full map here? Shouldn't rectify have a zero default for anything negative?

the map already is a result of a sub operation - 0 values have been removed. no reason to keep values as 0.

Copy link
Contributor

@eytan-starkware eytan-starkware left a comment

Choose a reason for hiding this comment

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

:lgtm:

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

@orizi orizi closed this Jan 28, 2026
@orizi orizi deleted the orizi/01-25-performance_sierra-gas_smaller_equivalent_costs_map branch January 28, 2026 12:12
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