Optimize Contract Compilation with LRU Caching#906
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Use the checkbox below for a quick retry:
Note
|
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | 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.
Comment @coderabbitai help to get the list of available commands and usage tips.
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
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_jsonis already aserde_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::Valuedirectly via a customHashimpl or hashing during serialization with aHasher-backedWriteadapter, avoiding the intermediateStringallocation. 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_jsonuses the re-export whilecrate::utils::compile_sierra_contractuses the direct module path.Both imports are valid, but for consistency, either import both from
crate::(via re-exports in lib.rs) or both fromcrate::utils::.
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:ahash::AHasherfor high-performance cache key hashingMutex<LruCache>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:
./scripts/format.sh./scripts/clippy_check.sh./scripts/check_unused_deps.sh./scripts/check_spelling.sh./website/README.mdSummary by CodeRabbit
Performance Improvements
Chores