feat: new ResidentBytes trait for types which can approximate their resident memory size (stack+heap)#7049
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new ResidentBytes trait (stack + heap “resident size” estimate) and implements it across Clarity contract/context-related types to enable future memory-bounded caching.
Changes:
- Introduces
clarity-types::resident_bytes::ResidentByteswith heuristic implementations for common container types and Clarity core types. - Implements
ResidentBytesfor key VM structures (Contract,ContractContext, function signatures/callables, token/map/var metadata). - Adds targeted unit tests validating that resident byte accounting covers all
ContractContextfields and grows with richer contracts.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
stacks-common/src/util/macros.rs |
Extends guarded_string! types with heap_capacity() to support resident-size estimation. |
clarity/src/vm/types/signatures.rs |
Implements ResidentBytes for FunctionSignature. |
clarity/src/vm/database/structures.rs |
Implements ResidentBytes for contract metadata structs (FT/NFT/map/var metadata). |
clarity/src/vm/contracts.rs |
Implements ResidentBytes for Contract and adds contract-level resident-bytes tests. |
clarity/src/vm/contexts.rs |
Implements ResidentBytes for ContractContext. |
clarity/src/vm/callables.rs |
Implements ResidentBytes for DefinedFunction. |
clarity-types/src/types/mod.rs |
Adds heap_capacity() to FunctionIdentifier for resident-size estimation. |
clarity-types/src/resident_bytes.rs |
New module defining ResidentBytes, container heuristics, Clarity type impls, and extensive tests. |
clarity-types/src/lib.rs |
Exports the new resident_bytes module. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Coverage Report for CI Build 25789425370Coverage increased (+0.02%) to 85.734%Details
Uncovered Changes
Coverage Regressions5397 previously-covered lines in 96 files lost coverage.
Coverage Stats
💛 - Coveralls |
federico-stacks
left a comment
There was a problem hiding this comment.
Implementation looks good. I’ve only added a few minor comments with possible improvements.
Also, regarding the Coveralls report: we could improve coverage for structure.rs and signature.rs modules
Description
Introduces a new
ResidentBytestrait which is implemented for all types used withinContract/ContractContextand yields either exact or close approximations of their stack [+heap] size ("resident size in bytes"), which is used by #7082 as part of size-informed cache admittance/eviction policy (limiting its in-memory size).This is a precursor to Clarity contract AST (
Contract) caching, where it's important that we are able to restrict the cache's overall actual memory usage. This PR provides the trait and necessary implementations, but does not wire it up.Includes heuristics for
BTreeMap/BTreeSet/HashMap/HashSet, based on currentstdimpls, to conservatively approximate their overhead.Applicable issues
Checklist