Skip to content

Optimize Contract Compilation with LRU Caching#906

Merged
3alpha merged 3 commits intomainfrom
cache-casm-compilation
Feb 10, 2026
Merged

Optimize Contract Compilation with LRU Caching#906
3alpha merged 3 commits intomainfrom
cache-casm-compilation

Conversation

@3alpha
Copy link
Copy Markdown
Collaborator

@3alpha 3alpha commented Feb 9, 2026

Optimize Contract Compilation with LRU Caching

Problem

Contract compilation is one of the most resource-intensive operations in Devnet. Profiling revealed that the same Sierra contract class is frequently compiled multiple times during normal blockchain interactions.

Solution

Implemented an LRU cache (max 10 entries) for compiled CASM contracts in compile_sierra_contract_json:

  • Uses ahash::AHasher for high-performance cache key hashing
  • Thread-safe via Mutex<LruCache>
  • Transparent to callers—no API changes required

This approach elegantly solves the performance issue without requiring invasive architectural changes to Devnet's transaction handling or fee estimation flows.

Usage related changes

Devnet speedup.

Development related changes

None.

Checklist:

  • Checked out the contribution guidelines
  • Applied formatting - ./scripts/format.sh
  • No linter errors - ./scripts/clippy_check.sh
  • No unused dependencies - ./scripts/check_unused_deps.sh
  • No spelling errors - ./scripts/check_spelling.sh
  • Performed code self-review
  • Rebased to the latest commit of the target branch (or merged it into my branch)
    • Once you make the PR reviewable, please avoid force-pushing
  • Updated the docs if needed - ./website/README.md
  • Linked the issues resolvable by this PR - linking info
  • Updated the tests if needed; all passing - execution info

Summary by CodeRabbit

  • Performance Improvements

    • Introduced smart contract compilation caching to reduce compilation overhead and improve deployment speed. Recently compiled contracts are now automatically cached and reused, accelerating repeated operations and enhancing overall system performance.
  • Chores

    • Added dependencies to support the new caching infrastructure.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Feb 9, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Note

.coderabbit.yaml has unrecognized properties

CodeRabbit is using all valid settings from your configuration. Unrecognized properties (listed below) have been ignored and may indicate typos or deprecated fields that can be removed.

⚠️ Parsing warnings (1)
Validation error: Unrecognized key(s) in object: 'auto_resolve_threads'
⚙️ Configuration instructions
  • Please see the configuration documentation for more information.
  • You can also validate your configuration using the online YAML validator.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Walkthrough

This pull request introduces caching for Sierra-to-CASM contract compilation. It adds three new dependencies (ahash, lru, parking_lot) to support fast hashing and cache management. The compilation logic in contract_class.rs is refactored to delegate to a new compile_sierra_contract_json function. A 10-entry LRU cache is implemented in utils.rs that stores compiled CASM contracts using hashed Sierra JSON input as the cache key, reducing redundant compilation work.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: implementing LRU caching to optimize contract compilation performance, which matches the core functionality changes in the PR.
Description check ✅ Passed The PR description follows the required template structure with clear Problem/Solution sections, Usage and Development changes, and a completed checklist with all items marked done.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch cache-casm-compilation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@3alpha
Copy link
Copy Markdown
Collaborator Author

3alpha commented Feb 9, 2026

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Feb 9, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@crates/starknet-devnet-types/src/utils.rs`:
- Around line 112-118: The current cache key uses a single u64 (`cache_key`
computed from `sierra_contract_json` via `AHasher`), which risks silent
collisions; change the key to a wider representation — e.g. compute a 128-bit
key (tuple (u64,u64) from two AHasher invocations or by seeding) and make the
cache an LruCache<(u64,u64), CasmContractClass> (or use LruCache<String,
CasmContractClass> to store the full `sierra_contract_json.to_string()`), update
usages of `cache_key` and the cache type accordingly, and ensure both hashing
and cache lookup use the same new key representation so hits are verified
against the wider hash.
🧹 Nitpick comments (2)
crates/starknet-devnet-types/src/utils.rs (1)

113-118: to_string() on every call serializes the full JSON just to compute a hash.

On cache hits this serialization cost is pure overhead. Since sierra_contract_json is already a serde_json::Value, you're allocating and formatting the entire JSON tree. For large Sierra contracts this can be non-trivial.

If performance is a concern, consider hashing the serde_json::Value directly via a custom Hash impl or hashing during serialization with a Hasher-backed Write adapter, avoiding the intermediate String allocation. That said, this is still far cheaper than recompilation, so it's a reasonable trade-off for now.

crates/starknet-devnet-types/src/rpc/contract_class.rs (1)

23-27: Import paths follow inconsistent patterns: crate::compile_sierra_contract_json uses the re-export while crate::utils::compile_sierra_contract uses the direct module path.

Both imports are valid, but for consistency, either import both from crate:: (via re-exports in lib.rs) or both from crate::utils::.

@3alpha 3alpha merged commit 3a83832 into main Feb 10, 2026
3 checks passed
@3alpha 3alpha deleted the cache-casm-compilation branch February 10, 2026 06:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant