Skip to content

(opt): memoize function code size estimation - #10348

Open
orizi wants to merge 1 commit into
mainfrom
orizi/08-14-_opt_memoize_function_code_size_estimation
Open

(opt): memoize function code size estimation#10348
orizi wants to merge 1 commit into
mainfrom
orizi/08-14-_opt_memoize_function_code_size_estimation

Conversation

@orizi

@orizi orizi commented Aug 14, 2026

Copy link
Copy Markdown
Collaborator

estimate_size was a plain function - each call built a dummy Sierra
program and ran a full Sierra-to-casm compilation - and the inlining and
specialization heuristics repeatedly request it for the same functions
(measured: 9976 calls, 5878 distinct, single ids requested up to 700
times). In a full compilation of cairo_level_tests its subtree was 56%
of all CPU, dwarfing the actual code emission. Now a tracked query.

Improves full-compilation CPU by ~9%.

Co-Authored-By: Claude Fable 5 noreply@anthropic.com

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Aug 14, 2026

Copy link
Copy Markdown
Collaborator Author

@orizi
orizi marked this pull request as ready for review August 14, 2026 07:30
@cursor

cursor Bot commented Aug 14, 2026

Copy link
Copy Markdown

PR Summary

Low Risk
Behavior-preserving performance change with a documented purity requirement on the optional custom estimator; incorrect non-pure estimators could theoretically serve stale sizes until invalidation.

Overview
estimate_size is now a Salsa tracked query, so repeated requests for the same ConcreteFunctionWithBodyId reuse cached results instead of recomputing every time.

Inlining and specialization heuristics were calling this heavily on overlapping function sets; each miss could run the configured estimator or the fallback path over post-baseline lowered IR. The PR documents that code_size_estimator must be a pure function of the database, since its output is memoized with the query key.

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

@eytan-starkware eytan-starkware left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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


a discussion (no related file):
Note: the comments below are from an automatic orizi-review run (Claude agents reviewing in Ori's style, findings adversarially verified before posting). Treat with the usual bot skepticism.


crates/cairo-lang-lowering/src/db.rs line 836 at r1 (raw file):

}

#[salsa::tracked(returns(copy))]

caching this turns the code_size_estimator hook into a purity contract - it's a pub param of init_lowering_group, so an embedder supplying a non-deterministic estimator now silently gets stale results instead of just being slow. worth saying so on the field doc (line 50):

/// A configurable function to get estimated size of the function with the given id.
/// Must be a pure function of the db - results are memoized.

estimate_size was a plain function - each call built a dummy Sierra
program and ran a full Sierra-to-casm compilation - and the inlining and
specialization heuristics repeatedly request it for the same functions
(measured: 9976 calls, 5878 distinct, single ids requested up to 700
times). In a full compilation of cairo_level_tests its subtree was 56%
of all CPU, dwarfing the actual code emission. Now a tracked query.

Improves full-compilation CPU by ~9%.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@orizi
orizi force-pushed the orizi/08-14-_opt_memoize_function_code_size_estimation branch from 01aab53 to 4d43770 Compare August 16, 2026 10:58

@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+AGNT made 1 comment and resolved 1 discussion.
Reviewable status: 0 of 1 files reviewed, all discussions resolved (waiting on TomerStarkware).


crates/cairo-lang-lowering/src/db.rs line 836 at r1 (raw file):

Previously, eytan-starkware+AGNT (Agent AGNT for eytan-starkware) wrote…

caching this turns the code_size_estimator hook into a purity contract - it's a pub param of init_lowering_group, so an embedder supplying a non-deterministic estimator now silently gets stale results instead of just being slow. worth saying so on the field doc (line 50):

/// A configurable function to get estimated size of the function with the given id.
/// Must be a pure function of the db - results are memoized.

Done.

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