Skip to content

Perf/clarity contract cache#2

Closed
cylewitruk-stacks wants to merge 7 commits into
perf/clarity-resident-bytesfrom
perf/clarity-contract-cache
Closed

Perf/clarity contract cache#2
cylewitruk-stacks wants to merge 7 commits into
perf/clarity-resident-bytesfrom
perf/clarity-contract-cache

Conversation

@cylewitruk-stacks
Copy link
Copy Markdown
Owner

Description

Builds on stacks-network#7049 (ResidentBytes impl) for calculating resident memory size.

Adds a size-weighted read-through TinyUFO cache (written by Cloudflare; uses S3-FIFO eviction + TinyLFU admission) for parsed Clarity Contract instances:

  • The cache uses a CACHE_WEIGHT_UNIT of 256, which the resident_bytes() size of a CachedContract is divided by (since TinyUFO's weight system uses u16), allowing for a maximum cacheable entry size of roughly 16MiB each.
  • The DEFAULT_CONTRACT_CACHE_SIZE is set to 64MiB (64 * 1024 * 1024), which gives a cache-wide weight limit of 262,144 units. TinyUFO enforces this budget on every insertion, evicting entries by access frequency to stay within the limit.
  • The cache tracks the last block hash + epoch and clears itself when it detects a reorg or epoch change to prevent providing stale contracts.
  • Read-only/ephemeral Clarity db's are wired up, but will only use the cache if reading at chain tip.

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:

Benchmark No Cache Cached Speedup
direct_call/increment 32.456 µs 8.248 µs 3.9x
contract_call/proxy_increment 55.053 µs 9.462 µs 5.8x
repeated_calls/10x_proxy_increment 799.16 µs 119.73 µs 6.7x
deep_fold/4_deep_x20_fold 2,150.6 µs 255.66 µs 8.4x

Checklist

  • Test coverage for new or modified code paths
  • Changelog fragment(s) or "no changelog" label added (see changelog.d/README.md)

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.

1 participant