/scalus:optimize-contractfrom scalus
Analyzes Scalus/Cardano smart contracts for execution budget optimization. Identifies expensive patterns, unnecessary allocations, and redundant traversals in @Compile annotated validators, providing concrete rewrites with budget impact estimates.
Ships in scalus. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Analyze Scalus/Cardano smart contracts for execution budget optimization opportunities.
assertBudgetEquals or assertBudgetWithin) on representative inputs — simple case, worst case, and typical case.EvalTestDsl budget assertions to find which code paths dominate the budget.Find on-chain code by searching for:
@Compile annotationValidator, DataParameterizedValidator, or ParameterizedValidatorPlutusV3.compile(), PlutusV2.compile(), or PlutusV1.compile()Search patterns:
grep -rn "@Compile" --include="*.scala" <path>
grep -rn "extends Validator" --include="*.scala" <path>
grep -rn "extends DataParameterizedValidator" --include="*.scala" <path>
@Compile annotated code in specified pathsbtn quick or specific test to confirm budget improvementFor detailed patterns with Scalus code examples, see references/patterns.md.
| ID | Pattern | Problem | Fix |
|---|---|---|---|
| O001 | Multiple list traversals | Separate filter then map then length | Combine into single foldLeft |
| O002 | foldRight on lists | Not tail-recursive, builds thunks | Use foldLeft with reverse if order matters |
| O003 | list.flatten | O(n*m) via nested foldRight + ++ | Accumulate with foldLeft and prepend |
| O004 | list.distinct | O(n^2) — foldLeft with exists | Use SortedMap or deduplicate at source |
| O005 | list :+ elem (append) | O(n) per append | Use elem +: list (prepend) and reverse once |
| O006 | Reconstructing Value | Full Value maintains invariants expensively | Use SortedMap or PairList directly when possible |
| O007 | AssocMap anywhere | get scans to hit-or-end; union is O(n*m) (get per left key, exists per right key) | Use SortedMap: get stops early via Ord, union is one linear merge |
| O008 | list.map(f).filter(p) | Two traversals, intermediate list | Single foldLeft combining map + filter |
| O009 | list.length == 0 / map.size == 0 | O(n) traversal; SortedMap.size is O(n) although inline | Use isEmpty on List, SortedMap, AssocMap: one nullList, O(1) |
| O010 | AssocMap.fromList on large input | O(n^2) dedup | Pre-sort and use SortedMap.fromStrictlyAscendingList |
| ID | Pattern | Problem | Fix |
|---|---|---|---|
| O011 | Expensive checks before cheap ones | Wasted budget on failing txs | Put cheapest/most-likely-to-fail checks first |
| O012 | Late require for invalid input | Work done before validation | Fail fast — validate inputs at the top |
| O013 | Linear condition chains | Average n/2 evaluations for n conditions | Structure as binary decision tree |
| O014 | No short-circuit in &&/` | ` | |
| O015 | list.exists after construction | Building list just to search it | Inline the search into the fold that builds the data |
| ID | Pattern | Problem | Fix |
|---|---|---|---|
| O016 | === on a BigInt/ByteString behind a type variable | Lowers to equalsData, not equalsInteger/equalsByteString: 1 761 779 vs 832 313 cpu, 2.1x (measured) | Make the key type concrete at the comparison site (a BigInt-keyed or ByteString-keyed helper, not a generic K) |
| O017 | Constructing tuples/records to return | Allocation + destructuring overhead | Use continuation-passing or accumulator parameters |
| O018 | List[(A, B)] map operations | ~12 builtins per element | Use PairList — ~4 builtins per element via fstPair/sndPair |
| O019 | Pattern matching for Data access | Constructs intermediate Scala objects | Use Data builtins directly when structure is known |
| O020 | Hand-written equalsData(a.toData, b.toData) or a.toData == b.toData | Buys nothing: for every Data-backed type === already lowers to equalsData; both spellings pin to 901 mem / 1 653 665 cpu on a Value (measured) | Derive Eq and write a === b; never compare TxInfo-scale structures whole |
| ID | Pattern | Problem | Fix |
|---|---|---|---|
| O021 | pow(2, n) | Generic exponentiation loop | Use exp2(n) — single builtin via byte shift |
| O022 | Manual log2 via division loop | O(log n) divisions | Use log2(n) — single builtin via integerToByteString |
| O023 | Recomputing same expression | Duplicated subexpressions | Use let bindings; V3 optimizer has CSE but don't rely on it |
| O024 | generateErrorTraces = true in prod | Trace strings bloat script and budget | Set generateErrorTraces = false for production builds |
| O025 | Complex pure computations on-chain | Expensive on-chain work | Move computation off-chain, pass result as redeemer, verify on-chain |
| ID | Pattern | Problem | Fix |
|---|---|---|---|
| O031 | out.datum.inlineOrFail[T](msg) === expected | Decodes, then compares field-wise: 461 lovelace | out.hasInlineDatum(expected): one equalsData on the wrapped datum, 286 lovelace. Use inlineOrFail only to read fields |
| O032 | xs.exists(_ === x) | exists is find(p).isDefined: allocates an Option for a Boolean; a fixed per-call tax of 326 483 cpu (miss) / 564 996 cpu (hit) on V3 | xs.contains(x): an intrinsic, no Option, no Eq closure. For a non-equality predicate use forall or a hand fold, not exists |
| O033 | xs.filter(p).length | 2 traversals plus k mkCons; filter is a non-tail foldRight; no pass fuses them | xs.count(p): one tail-recursive foldLeft, no allocation |
| O034 | filter(p).length === BigInt(1) then .head, or count(p) === BigInt(1) | 2 passes (or 1 pass plus a second scan for the element); the guard and the lookup are separate | xs.findUniqueOrFail(p, msg): one pass, returns the element, fails on 0 or 2+. Against count(p) === BigInt(1) on inputs: fee 3 175 vs 3 307 (3 inputs), 6 289 vs 6 804 (10 inputs) |
| O035 | value.lovelaceAmount or a hand-written walk to the ADA entry | Walks the Data map: about 30 more machine steps per call than the builtin; lovelaceAmount is deprecated | value.getLovelace: one lookupCoin at PV11 |
| O036 | A check the token invariant already proves | Runs on every spend: a datum.isDefined guard on a UTxO that holds the state token; an exact burn quantity checked in both the policy and the spend branch | Drop it once the security review's token lifecycle table holds (mint places the token in a valid UTxO, every transition keeps it). A policy Burn branch shrinks to tx.onlyBurnsUnder(policyId), never to true. See "Let the Token Invariant Do the Work" |
| ID | Pattern | Problem | Fix |
|---|---|---|---|
| O026 | Small recursive helpers | Call overhead per recursion | Unroll first 1-2 iterations for common small cases |
| O027 | Non-tail-recursive numeric loops | Stack growth | Rewrite with accumulator parameter |
| O028 | Redundant FromData/ToData conversions | Serialization round-trips | Keep data in Data form between operations |
| O029 | list.reverse.foldLeft | Extra O(n) reverse pass | Use foldRight if list is small, or build in correct order |
| O030 | Building closures in inner loops | Allocation per iteration | Lift closure outside loop if captures don't change |
The most impactful optimization: move work off-chain.
Instead of computing a result on-chain, have the off-chain code compute it and pass it as a redeemer field. The validator only checks correctness.
// Expensive: compute on-chain
val sqrtResult = radicand.sqRoot
// Cheap: verify pre-computed result
val sqrtResult = redeemer.sqrtValue
require(sqrtResult * sqrtResult <= radicand)
require((sqrtResult + 1) * (sqrtResult + 1) > radicand)
Put cheapest and most-likely-to-fail validations first. Every require that fails
early saves the budget of all subsequent code.
// Good: cheap check first
require(isSignedBy(txInfo, admin), "not admin")
require(expensiveValueCheck(txInfo), "value mismatch")
// Bad: expensive check first
require(expensiveValueCheck(txInfo), "value mismatch")
require(isSignedBy(txInfo, admin), "not admin")
Never traverse a list twice when once will do. Combine filter + map + count into a single fold.
// Bad: three traversals
val filtered = items.filter(_.isValid)
val mapped = filtered.map(_.amount)
val total = mapped.foldLeft(BigInt(0))(_ + _)
// Good: single traversal
val total = items.foldLeft(BigInt(0)) { (acc, item) =>
if item.isValid then acc + item.amount else acc
}
PairList uses raw UPLC pair builtins (~4 ops/element) vs List[(A, B)] (~12 ops/element).
// Expensive
map.toList.map { case (k, v) => (k, f(v)) }
// Cheap — 3x fewer builtins
map.toPairList.mapValues(f)
===, and Keep Key Types ConcreteFor every Data-backed type a === b already lowers to one equalsData builtin. Hand-written
equalsData(a.toData, b.toData) or a.toData == b.toData produces identical UPLC (measured:
both pin to 901 mem / 1 653 665 cpu on a Value). The cost that is real: === on a BigInt
or ByteString behind a type variable emits equalsData instead of equalsInteger /
equalsByteString, 1 761 779 vs 832 313 cpu (2.1x, measured).
// Same UPLC, worse to read: do not write this
require(equalsData(toData(outputDatum), toData(inputDatum)))
// Write this (derive Eq on the datum type)
require(outputDatum === inputDatum)
// Generic key: equalsData on every step, 2.1x slower
def lookup[K: Eq](key: K, entries: List[(K, BigInt)]): Option[BigInt] =
entries.find(_._1 === key).map(_._2)
// Concrete key: equalsInteger
def lookup(key: BigInt, entries: List[(BigInt, BigInt)]): Option[BigInt] =
entries.find(_._1 === key).map(_._2)
For a continuing datum, compare without decoding: out.hasInlineDatum(expected) costs 286
lovelace against 461 for out.datum.inlineOrFail[T](msg) === expected (measured, O031).
The ledger guarantees: inputs are sorted by TxOutRef, values are ordered by policy ID,
outputs never contain negative quantities, minted values exclude ADA.
Align your algorithms with these invariants instead of re-validating them.
If you check membership in the same set multiple times, build a decision closure once.
// Bad: O(n) per check
require(signatories.exists(_ === admin1))
require(signatories.exists(_ === admin2))
// Better: single traversal, check both
val (hasAdmin1, hasAdmin2) = signatories.foldLeft((false, false)) { case ((a1, a2), sig) =>
(a1 || sig === admin1, a2 || sig === admin2)
}
require(hasAdmin1 && hasAdmin2)
log2 and exp2 use integerToByteString/shiftByteString — much cheaper
than iterative computation.
// Cheap
val bits = x.log2
val powerOf2 = n.exp2
// Expensive
val bits = manualLog2Loop(x)
val powerOf2 = pow(BigInt(2), n)
The compiler swaps some prelude calls for builtins or a direct recursion; the contract skill
lists them under "Intrinsics". At PV11 the Value operations lower to the CIP-153 builtins:
quantityOf/getLovelace to lookupCoin, +/-/* to unionValue/scaleValue,
containsAtLeast to valueContains, insertCoin/withoutLovelace to insertCoin. A method
that is not on the list (lovelaceAmount, exists, filter(p).length) runs as ordinary
compiled code.
Memory is mostly machine steps, not builtins: every CEK step costs 100 memory units. A per-line
profile (evaluateProfile, see the profiling docs page) of a typical validator shows builtin
calls at about 5% of memory. Cut steps: fewer calls, fewer pattern matches, intrinsics instead of
walks.
// ~30 machine steps per call: walks the Data map
val ada = out.value.lovelaceAmount
// One builtin at PV11
val ada = out.value.getLovelace
When the security review proves the token lifecycle (smart-contract-security-review, "Token
Lifecycle"): the mint puts the token in an output at the script with a valid inline datum, and
every spend branch keeps it there, then each UTxO that holds the token has that datum. A check
that re-proves it is redundant on that UTxO:
// spend: the datum is never read, and every token-holding UTxO has one by induction
require(datum.isDefined, "Must have inline datum") // drop
// mint, Burn redeemer: the only token is in the oracle UTxO, a continuing spend branch keeps it
// there, so burning it runs the spend Burn branch, which checks the signature and the quantity
case Burn => require(tx.mint.hasOnly(policyId, name, -1), "burn one") // before
case Burn => require(tx.onlyBurnsUnder(policyId), "burn only") // after
Two limits. Prove the invariant first; without it the check is the only protection. And keep the
part no invariant covers: case Burn => () lets the Burn redeemer mint new tokens, so a burn
branch always keeps onlyBurnsUnder(policyId).
The reverse also holds: a rewrite must not make a continuing output stricter. Replacing field
checks with out.hasInlineDatum(expected) pins every field; if one field records the lovelace
(amount) or a time the builder picks, pin it to the new value (out.value.getLovelace), not the
old one, or the honest transaction that adds min-ADA fails (security review V039).
// V3 lowering, no error traces, UPLC optimizer on
private given Options = Options.release
Use EvalTestDsl for precise budget measurement:
import scalus.testing.dsl.EvalTestDsl.*
// Exact budget match — catches regressions AND improvements
eval(compiled)
.onVM(PlutusV3.makePlutusV3VM())
.expectSuccess()
.assertBudgetEquals(memory = 129528, steps = 37_067868)
// Upper bound — for tests where budget fluctuates slightly between builds
eval(compiled)
.onVM(PlutusV3.makePlutusV3VM())
.expectSuccess()
.assertBudgetWithin(memory = 140000, steps = 40_000000)
Format convention: Always use named parameters and _ at million boundary:
ExUnits(memory = 129528, steps = 37_067868)
Use clickable file_path:line_number format for all code locations.
### [IMPACT] ID: Optimization Name
**Location:** `full/path/to/File.scala:LINE`
**Estimated savings:** ~X% memory, ~Y% steps (or: high/medium/low)
**Current code** (`full/path/to/File.scala:LINE-LINE`):
```scala
// actual code from file
Optimized code:
// proposed optimization
Rationale: Why this is faster and what budget cost it avoids.
### Summary Table
| ID | Impact | Location | Pattern | Est. Savings |
|---|---|---|---|---|
| O-01 | High | path/File.scala:123 | Multiple traversals → single fold | ~30% steps |
| O-02 | Medium | path/File.scala:87 | inlineOrFail === x → hasInlineDatum(x) | 286 vs 461 lovelace |
| O-03 | Low | path/File.scala:200 | Unroll small recursion | ~2% steps |
Current budget: ExUnits(memory = X, steps = Y) Estimated budget after optimization: ExUnits(memory = X', steps = Y')
## Interactive Workflow
For each finding:
1. Display issue with location and proposed optimization
2. Prompt: "Apply optimization? [y/n/s/d]"
- y: Apply change, re-run budget test
- n: Skip, log as "declined"
- s: Skip without logging
- d: Show detailed budget breakdown
3. After all findings: run `sbtn quick` to verify correctness
4. Generate summary report with actual budget deltas (before/after)
## Reference
For detailed optimization patterns with Scalus code examples, see:
- `references/patterns.md` — Full pattern catalog with before/after code and budget estimates
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
cardano, scalus, smart-contract-optimization, plutus, execution-budget
npx claudepluginhub scalus3/scalus --plugin scalus7 skills in this plugin
/contractGuide for developing Scalus smart contracts. Use when writing or modifying Cardano validators in Scala 3 with Scalus./contract-testGuide for testing Scalus smart contracts. Use when writing unit, property, budget, or Emulator tests for Cardano validators./local-developmentUse when developing or testing Scalus smart contracts with the local Emulator and TxBuilder. Covers how to create an Emulator provider, submit transactions, test negative validator cases, and test confirmation delay and rollbacks./smart-contract-security-reviewSecurity review for Scalus/Cardano smart contracts. Analyzes @Compile annotated validators for vulnerabilities like redirect attacks, inexact value validation, missing token verification, broken token lifecycle invariants, honest transactions the validator rejects, ADA-only comparisons, rounding direction, and self-dealing. Use when reviewing on-chain code, before deploying validators, or when /smart-contract-security-review is invoked. Requires explicit path argument./typescriptUse when TypeScript or JavaScript code calls the npm package scalus - the Cardano Emulator, Plutus script evaluation and budgets, applying parameters to a script, evaluating or balancing a transaction, or a Lucid, Mesh or Evolution SDK provider backed by Scalus./using-scalusUse at the start of any task in a Scalus project, to choose the Scalus skill for smart contract work or for TypeScript and JavaScript code that uses the npm package scalus.Other plugins' skills for the same job