-
Notifications
You must be signed in to change notification settings - Fork 371
CIP-???? | Plutus Script Caching #1031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
colll78
wants to merge
3
commits into
cardano-foundation:master
Choose a base branch
from
colll78:plutus-script-caching
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+112
−0
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| --- | ||
| CIP: ??? | ||
| Title: Plutus Script In-Memory Caching | ||
| Category: Plutus | ||
| Status: Proposed | ||
| Authors: | ||
| - Philip DiSarro <[email protected]> | ||
| Implementors: [] | ||
| Discussions: | ||
| - https://github.com/IntersectMBO/cardano-ledger/blob/master/docs/adr/2024-08-14_009-refscripts-fee-change.md | ||
| Created: 2025-05-01 | ||
| License: CC-BY-4.0 | ||
| --- | ||
|
|
||
| ## Abstract | ||
|
|
||
| We propose introducing a memory-bound, Least Recently Used (LRU) in-memory caching layer within Cardano nodes to store deserialized Plutus scripts. This optimization will significantly reduce redundant deserialization operations and improve node performance, especially in high-throughput environments or dApps with widely reused scripts. Scripts not found in cache are lazily reloaded from transaction data or reference inputs via cold reload. | ||
|
|
||
| ## Motivation: why is this CIP necessary? | ||
|
|
||
| Currently, each Plutus script must be deserialized every time it is encountered during validation, even if it has been used in many recent transactions. This introduces non-trivial CPU overhead, especially for frequently invoked scripts like validators in DeFi protocols, DEXs, or DAOs. | ||
|
|
||
| This CIP proposes a RAM-based cache to: | ||
|
|
||
| - Reduce CPU overhead per block validation | ||
| - Improve performance for scripts reused across transactions | ||
| - Avoid repeated deserialization of identical script bytes | ||
| - Enable faster block validation, especially under load | ||
|
|
||
| **Beneficiaries**: | ||
| - Node operators and SPOs | ||
| - Off-chain indexers and relays | ||
| - Application developers with high-frequency validators | ||
|
|
||
| ## Specification | ||
|
|
||
| ### Caching Layer Requirements | ||
|
|
||
| - Cache stores deserialized `PlutusScript` objects in memory | ||
| - Keyed by script hash (Blake2b-224, consistent with ledger use) | ||
| - LRU eviction policy to release least recently used scripts | ||
| - Global memory cap (e.g., 256 MB, configurable via node config) | ||
|
|
||
| ### Behavior | ||
|
|
||
| - On script validation, check in-memory cache first | ||
| - If cache hit: use deserialized script directly | ||
| - If cache miss: | ||
| - Deserialize from transaction or reference input | ||
| - Insert into cache, possibly evicting an older entry | ||
|
|
||
| ### Cold Reload | ||
|
|
||
| - Evicted scripts or rarely used scripts are re-deserialized as needed | ||
| - Cache does not affect determinism or validation results | ||
| - Deserialization errors are handled identically to current logic | ||
|
|
||
| ### Configuration Parameters | ||
|
|
||
| Add new parameters to the node configuration: | ||
|
|
||
| ```yaml | ||
| PlutusScriptCache: | ||
| maxCacheSizeMB: 256 | ||
| evictionPolicy: LRU | ||
| ``` | ||
|
|
||
| ## Rationale: how does this CIP achieve its goals? | ||
|
|
||
| This proposal follows the same spirit as performance optimizations in other smart contract chains (e.g. Solana's JIT caching model). It does not modify on-chain structures or Plutus semantics — only the **execution engine of node software**. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it is not changing anything on-chain, is it even a CIP? |
||
|
|
||
| - **Backwards compatible**: does not change transaction format or script representation | ||
| - **Deterministic**: cache hit/miss does not change validation result | ||
| - **Node-local**: entirely handled in the node runtime | ||
|
|
||
| ### Alternatives considered | ||
|
|
||
| - Persistent on-disk script cache: slower, more complex concurrency | ||
| - No caching: leads to redundant deserialization cost | ||
|
|
||
| ### Interaction with ADR-009 Reference Script Fee Changes | ||
|
|
||
| [ADR-009](https://github.com/IntersectMBO/cardano-ledger/blob/master/docs/adr/2024-08-14_009-refscripts-fee-change.md) introduced a tiered fee structure for reference scripts due to the high deserialization overhead of large scripts. This CIP mitigates that overhead at the node level by reusing previously deserialized scripts in memory. | ||
|
|
||
| This caching mechanism does **not replace** or invalidate the fee policy defined in ADR-009. The **fee remains based on raw reference script byte size**, regardless of cache hits, as: | ||
|
|
||
| - Cache state is local to the validator and non-deterministic from a protocol perspective | ||
| - Fees must remain consistent, independent of validator memory state | ||
|
|
||
| However, this CIP **reduces the real-world validator cost** of handling such scripts, aligning economic deterrents (via ADR-009) with operational efficiency (via caching). | ||
|
|
||
| Future extensions may explore whether cache hits can be made partially fee-aware for further optimization, but that is out of scope for this proposal. | ||
|
|
||
| ## Path to Active | ||
|
|
||
| ### Acceptance Criteria | ||
|
|
||
| - Implementation merged into `cardano-node` | ||
| - Benchmarks show measurable reduction in script deserialization cost under load | ||
| - No deviation in transaction validation outcomes due to caching | ||
|
|
||
| ### Implementation Plan | ||
|
|
||
| 1. Modify Plutus script execution in `cardano-node` to check memory cache before deserialization | ||
| 2. Integrate cache via bounded LRU store | ||
| 3. Add config flags to control cache size and policy | ||
| 4. Test under synthetic load with high script reuse | ||
colll78 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Copyright | ||
|
|
||
| This CIP is licensed under [CC-BY-4.0](https://creativecommons.org/licenses/by/4.0/legalcode). | ||
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.
Uh oh!
There was an error while loading. Please reload this page.