feat: implement clarity Contract cache#7082
Draft
cylewitruk-stacks wants to merge 37 commits into
Draft
Conversation
There was a problem hiding this comment.
Pull request overview
Implements a size-weighted, read-through cache for parsed Clarity Contract instances to reduce repeated contract parsing/loading overhead while keeping cache correctness across reorgs and epoch transitions.
Changes:
- Add
ResidentBytestrait (and implementations) to estimate heap+stack footprint for cache weighting. - Introduce a
TinyUFO-backedContractCacheand wire it through Clarity DB/VM call paths (including cost-contract loads). - Add tests and a Criterion benchmark to validate/measure caching behavior and correctness guards (tip-only, rollback/reorg/epoch invalidation).
Reviewed changes
Copilot reviewed 22 out of 23 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
stackslib/src/clarity_vm/clarity.rs |
Plumbs cache through block/tx/read-only connections and adds integration tests for persistence/bypass/invalidation. |
stacks-common/src/util/macros.rs |
Extends guarded_string! types with heap_capacity() for sizing. |
clarity/src/vm/types/signatures.rs |
Adds ResidentBytes impl for FunctionSignature. |
clarity/src/vm/functions/mod.rs |
Switches contract lookup in contract-of to cached contract load. |
clarity/src/vm/functions/database.rs |
Uses cached contract loads during trait-based dispatch checks. |
clarity/src/vm/database/structures.rs |
Adds ResidentBytes impls for token/map/var metadata types. |
clarity/src/vm/database/mod.rs |
Exposes ContractCache/CachedContract and adds module. |
clarity/src/vm/database/key_value_wrapper.rs |
Adds is_retargeted() and a test helper to bypass caches on historical views. |
clarity/src/vm/database/contract_cache.rs |
New TinyUFO-backed weighted cache implementation with unit tests. |
clarity/src/vm/database/clarity_db.rs |
Adds optional cache attachment and cached contract/load-cost accessors with retarget bypass. |
clarity/src/vm/costs/mod.rs |
Loads cost-contract contexts via cached contract loads. |
clarity/src/vm/contracts.rs |
Adds ResidentBytes impl for Contract plus tests for sizing consistency. |
clarity/src/vm/contexts.rs |
Adds ResidentBytes impl for ContractContext; charges load cost before contract load using cached cost-size path. |
clarity/src/vm/callables.rs |
Adds ResidentBytes impl for DefinedFunction. |
clarity/Cargo.toml |
Adds TinyUFO dependency and a contract_cache benchmark target. |
clarity/benches/contract_cache.rs |
Adds Criterion benchmark comparing cached vs uncached contract execution. |
clarity-types/src/types/mod.rs |
Adds FunctionIdentifier::heap_capacity() for sizing. |
clarity-types/src/resident_bytes.rs |
New ResidentBytes trait and implementations for many Clarity types and std containers. |
clarity-types/src/lib.rs |
Exports the new resident_bytes module. |
changelog.d/README.md |
Tweaks example fenced block language (text). |
changelog.d/7049-clarity-type-size-approximation.added |
Adds changelog fragment for the ResidentBytes addition. |
Cargo.toml |
Adds workspace dependency entry for TinyUFO. |
Cargo.lock |
Locks TinyUFO and its transitive dependencies. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Coverage Report for CI Build 25800686541Coverage increased (+0.07%) to 85.786%Details
Uncovered Changes
Coverage Regressions5424 previously-covered lines in 104 files lost coverage.
Coverage Stats
💛 - Coveralls |
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Builds on #7049 (
ResidentBytesimpl) for calculating resident memory size. Currently in draft status until that PR merges.Adds a size-weighted read-through
TinyUFOcache (written by Cloudflare; usesS3-FIFOeviction +TinyLFUadmission) for parsed ClarityContractinstances:CACHE_WEIGHT_UNITof256, which theresident_bytes()size of aCachedContractis divided by (sinceTinyUFO's weight system usesu16), allowing for a maximum cacheable entry size of roughly16MiBeach.DEFAULT_CONTRACT_CACHE_SIZEis set to64MiB(64 * 1024 * 1024), which gives a cache-wide weight limit of262,144units.TinyUFOenforces this budget on every insertion, evicting entries by access frequency to stay within the limit.Applicable issues
Additional info (benefits, drawbacks, caveats)
NOTE: The cache is implemented as a node-level cache and as such is purely a node optimization; a next iteration which incorporates tx-level caching policy for targeting tenure budgets could implement its own tx-scoped cache on top of the node-level cache with stricter/deterministic policy.
Some benchmark results:
Checklist
changelog.d/README.md)