clarity-optimizer
Skill2 starsChanged 30 days agoMITView on GitHub
Cut Clarity execution costs: cost dimensions, block and tenure budgets, Clarinet cost reports and safe rewrites. Use when a transaction is too expensive or hits a block limit.
In this repository24 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- llms.txtllms.txt
- clarity-apisSkill
- clarity-assetsSkill
- clarity-auditorSkill
- clarity-bitcoin-proofsSkill
- clarity-deploymentSkill
- clarity-ecosystemSkill
- clarity-environmentsSkill
- clarity-epochsSkill
- clarity-fuzzingSkill
- clarity-governanceSkill
- clarity-oraclesSkill
- clarity-protocolsSkill
- clarity-scholarSkill
- clarity-securitySkill
- clarity-sip-complianceSkill
- clarity-stakingSkill
- clarity-startSkill
- clarity-system-integrationSkill
- clarity-testingSkill
- clarity-upgradesSkill
- clarity-walletsSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
17 rules. About 0 are common, 11 are distinctive, and 6 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈11
Distinctive · estimate
This author's own choices. Worth a look.
6
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 48
Run the public entry with Clarigen/SDK trackCosts: true and read receipt.costs.
- Distinctive: line 52
Keep only changes that lower the binding dimension without breaking tests.
- Distinctive: line 54
Call shapes (default to Clarigen for project tests.
- Distinctive: line 86
Start baseline and candidate from equivalent fresh state.
- Very specific: line 92
Write each measurement from the receipt (total/limit, each with the five fields), then run python3 <optimizer>/scripts/compare_costs.py …
- Distinctive: line 110
Verify outputs, balances, storage, events, errors, authorization and rollback, including arithmetic boundaries.
- Very specific: line 115
Use the current node's /v2/pox epoch limits and version-matched consensus source.
- Distinctive: line 149
Do not write unchanged values.
- Very specific: line 154
Prefer native operations over hand-written loops (fold over a 200-item list costs per item).
- Very specific: line 161
Never cut trait identity, signatures, price freshness/confidence, try!
- Distinctive: line 162
Do not reorder an external call ahead of authentication to reuse its output.
- Very specific: line 168
Prefer the scholar's nested if chain over repeated (asserts!
- Distinctive: line 177
Before choosing between the two on cost, read the measured fixture (350 items at epochs 2.4 and 4.0) and its Bitflow source examples.
- Distinctive: line 178
Before epoch 3.3, callback argument charges use declared maximum type sizes.
- Distinctive: line 180
Prefer the smaller binding budget dimension, not runtime alone.
- Distinctive: line 186
Use supported address constants for readability and repeated identities, after checking their epoch, read-only and dependency limits.
- Distinctive: line 213
Test and hash the exact final artifact.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Clarity Optimizer
Optimize the measured limiting dimension; preserve authority, rounding, state, errors and consumed events.
Key distinctions
- Execution has five dimensions: runtime, read count/bytes, write count/bytes. Memory, stack and transaction size are separate bounds.
- Nakamoto blocks share tenure capacity. Higher fees do not buy extra execution budget.
- Fewer reads can increase runtime; shorter expressions are not proof of improvement.
- Source bytes, including whitespace and deployment-only code, spend runtime/read budget on later contract loads.
costs-*.clarevaluates schedule formulas. It cannot read the caller's consumed or remaining budget.- Epoch 4.0 uses native
costs_5.rs; the still-callablecosts-4contract returns an older schedule. There is no deployedcosts-5on mainnet or testnet. - Simulator limits can lag the network. The default 25%-of-remaining block soft limit is configurable miner policy.
Budget and the binding dimension
- The budget is five independent counters. In Nakamoto the
/v2/poxblock limit is the budget of a whole tenure. Default miners stop adding contract calls to a block once it passes 25% of what remains (tenure_cost_limit_per_block_percentage, default 25), but the call that crosses it is still included. - A transaction that overflows a tenure less than 20% used is dropped as too big; one that overflows a fuller tenure waits for a later one. Both rules are miner policy, not consensus ceilings (resource limits): size transactions with headroom for the target miner and remaining tenure budget.
| Dimension | Tenure limit (epoch 4.0) | Block soft limit in a fresh tenure (25%) | What spends it |
| runtime | 5,000,000,000 | 1,250,000,000 | evaluation, hashing, contract loading (grows with size) |
| read count | 30,000 | 7,500 | every map-get?, var-get, balance read and map or var write, 3 per contract-call? |
| read bytes | 200,000,000 | 50,000,000 | stored value sizes, full callee size per contract-call? |
| write count | 15,000 | 3,750 | every map-set/insert/delete, var-set, token movement |
| write bytes | 15,000,000 | 3,750,000 | stored value sizes written, deployed contract size |
- Epochs 2.1–3.4 have 15,000 reads and 100,000,000 read bytes; the other limits are the same.
- Epoch 4.0 changed what matters:
costs_5recalibrated runtime at 1 unit per 80,000 CPU instructions.map-get?went fromn + 1025to44runtime andsecp256k1-verifyfrom 8,349 to 38, but per-itemmapandfoldwork fell far less: function application and argument checks keep their costs_3 formulas. Read and write counts did not change. - These changes make storage counts and large-contract read bytes important profiling candidates.
- On epochs 2.1–3.4 (costs_3 and costs_4), runtime from list processing, hashing and large
lets can still bind.
Measure and compare
First move for "my transaction hits a budget limit" or "make it cheaper":
- Run the public entry with Clarigen/SDK
trackCosts: trueand readreceipt.costs. - Divide each
totalby the target network's/v2/poxlimit; the largest ratio is the binding dimension. At epoch 4.0 the receipt'slimit, whichcompare_costs.pyalso divides by, still has 15,000 reads and 100,000,000 read bytes. - Pick a transformation for that dimension.
- Re-measure on a fresh, identical fixture.
- Keep only changes that lower the binding dimension without breaking tests.
Call shapes (default to Clarigen for project tests; use a current compatible CLI/SDK and inspect the project's scripts):
- Clarigen's
txOk,txErrandtxwrappers retain the SDK receipt, includingcostswhen tracking is enabled. Generate types first and configure SDK cost tracking in the test environment:
// Fragment: target and sender come from the project's generated factory/accounts.
const receipt = txOk(target.operation(1000n), sender);
if (!receipt.costs) throw new Error('Cost tracking was not enabled');
console.log(JSON.stringify(receipt.costs, null, 2));- SDK directly, for an isolated measurement or a wrapper gap:
import { initSimnet } from '@stacks/clarinet-sdk';
import { Cl } from '@stacks/transactions';
const simnet = await initSimnet('./Clarinet.toml', true,
{ trackCosts: true, trackCoverage: false });
const sender = simnet.getAccounts().get('wallet_1')!;
const receipt = simnet.callPublicFn('target', 'operation', [Cl.uint(1000n)], sender);
if (!receipt.costs) throw new Error('Cost tracking was not enabled');
console.log(JSON.stringify(receipt.costs, null, 2));receipt.costs.totaland.limithaveruntime,readCount,readLength,writeCount,writeLength, plusmemoryandmemory_limitat the costs root.- Null costs are missing measurement, not zero cost. SDK 3.26 defaults to free cost tracking when omitted, which can hide cost-triggered aborts and budget overruns; enable tracking even for semantic regression tests. See environment fidelity.
- Set the target epoch before deployment/setup where analysis or boot state depends on it; a later
setEpochonly changes subsequent execution. - For whole-suite reports,
npx vitest run -- --costswritescosts-reports.json(coverage and costs).
Fixtures:
- Record language version, epoch, SDK/VM revision, source revisions and fixture state with each result.
- Start baseline and candidate from equivalent fresh state. Baseline complete public transactions: wrappers, oracle verification, token transfers, prints and callbacks. Helpers measured with private calls are diagnostic only.
- Include maximum list/buffer sizes and state, near-limit state, empty and failing paths, and asset/debt values near arithmetic boundaries. A failing operation can consume budget despite state rollback.
- Establish a minimal public/read-only-call baseline to separate contract loading from business logic; repeat with exact release bytes.
Compare:
- Write each measurement from the receipt (
total/limit, each with the five fields), then runpython3 <optimizer>/scripts/compare_costs.py before.json after.json(<optimizer>is this skill's directory):
// Fragment: receipt comes from the measured call above.
import { writeFileSync } from 'node:fs';
writeFileSync('before.json', JSON.stringify({
context: { epoch: '4.0', clarity: 6, sdk: '3.26.0', fixture: 'deposit-1000' },
costs: { total: receipt.costs!.total, limit: receipt.costs!.limit },
}));- Resulting file shape (values illustrate the schema, not a measured transaction):
{"context":{"epoch":"4.0","clarity":6,"sdk":"3.26.0","fixture":"deposit-1000"},"costs":{"total":{"runtime":100,"readCount":1,"readLength":100,"writeCount":1,"writeLength":100},"limit":{"runtime":5000000000,"readCount":15000,"readLength":100000000,"writeCount":15000,"writeLength":15000000}}}- Both records must have the same context and limits.
context.clarityis a free label but must match; keep contract revision labels outsidecontext, since baseline and candidate necessarily differ. The helper reports all dimensions, deltas, utilization, and the limiting dimension. - Verify outputs, balances, storage, events, errors, authorization and rollback, including arithmetic boundaries. Never remove a safety check to save cost.
- Report every dimension's absolute/percentage change, active-budget utilization, limiting dimension and unmeasured paths. Label estimates separately.
Interpret capacity:
- Use the current node's
/v2/poxepoch limits and version-matched consensus source. The SDK reports 15,000 reads / 100,000,000 read bytes even at epoch 4.0, while live epoch-4.0 limits are 30,000 / 200,000,000: keep the simulator limits in raw records and judge capacity against/v2/pox. - Dividing the tenure budget by one call's cost is arithmetic, not a TPS figure: blocks share the budget, tenure extensions reset it and other transactions compete for it.
- Transaction fees and execution costs are different quantities. The simulator may use generous/default limits or omit real mempool conditions: report those differences.
- Deployment analysis and serialization costs are separate from subsequent function costs; contract loading/dependency size can affect calls.
Sources and trackers: cost dispatch, SDK, SIP-044, SIP-034. Check both Clarigen issues and Clarinet issues for receipt/epoch/cost-tracking differences.
Transformations by dimension
Per-operation charges (cost table) are not transaction totals: measure before and after. Cache across writes/callbacks only after proving stability; read snapshot/cache invalidation, including mutations within one block.
The Clarity Book cost chapter remains useful for a minimal-call baseline, fewer repeated cross-contract loads and separating bulky setup. Its numeric limits, tooling examples and optimization rankings require current-version measurements.
Read count
| Change | Saves | Proof obligation |
Read a map entry / var once and bind it with let | 1 read per repeat | Nothing between the reads changes it (no intervening write, no external call that could) |
| Store fields read together in one tuple value | 1 read per extra field map | Read bytes grow with the tuple; writes of one field rewrite the whole value |
Fewer contract-call? hops (inline a tiny helper contract, call the protocol's bundled entry point) | 3 reads + callee size per hop | Same authorization, events and error behavior; the callee may be shared code you must not fork |
Avoid get-balance/getter calls whose value you already have from a return value | 3+ reads per call | The returned value is the same quantity (sBTC get-balance includes locked units) |
Replace "check then write" pairs with map-insert (returns false if present) | 1 read | Assert the boolean; keep the same error |
| Constants instead of data vars for values that never change | 1 read per use | Truly immutable for the life of the contract; governance can never need it |
| Paginate/bound list-driven loops | reads per item left out | Partial progress semantics are explicit and replay-safe |
Read bytes
contract-call?charges the callee's whole stored size each call. Calling a 60 KB contract ten times costs ~600 KB of read bytes and 30 reads before any logic runs. Keep hot-path contracts small (split admin/rarely used code out), and avoid calling a large contract for a trivial read.- Stored values are read whole. A data var holding a
(list 500 uint)or a fat tuple charges its full size on everyvar-get. When calls touch only a few items, index them in a map ({index: uint} → item); when calls use the whole list, one read of it can be cheaper than one read per item. Measure the binding dimension. - Strings and buffers in storage cost their bytes on every read. Store hashes or IDs where the full value is not needed on-chain.
Write count and bytes
- Each
map-set/var-setis a write, and so is each FT/STX/NFT movement (ft-transfer?= 2 writes,stx-transfer?= 1). Batch accounting updates into one entry where the data model allows. - Do not write unchanged values; skip the write when the computed value equals the stored one, if that read is already paid for.
- Deploy cost:
cost_contract_storagecharges the source size as write bytes (source size).
Runtime (binding on old epochs or heavy computation)
- Prefer native operations over hand-written loops (
foldover a 200-item list costs per item). - Clarity 6: variadic
(concat a b c d)is cheaper than nested binaryconcatchains. - Verify an oracle payload's signatures once per transaction and reuse the verified values within the contract, or pass them only to consumers that authenticate the caller as the verifier; a consumer that accepts price arguments from any caller must verify the payload itself. Keep each consumer's freshness and feed checks.
to-consensus-buff?+sha256over large tuples costs per byte; hash once and reuse.
Never trade away the invariant
- Never cut trait identity, signatures, price freshness/confidence,
try!propagation, slippage minima, health checks, or exactas-contract?allowances for savings. - Do not reorder an external call ahead of authentication to reuse its output.
(/ (* a b) d)and(* (/ a d) b)are different numbers. Reducing precision is a product decision, not an optimization.- Preserve event names/fields for indexers unless versioning the schema.
Switch-style dispatch
- Prefer the scholar's nested
ifchain over repeated(asserts! (not case?) result)dispatch. - With eight cases at epoch 4.0,
asserts!dispatch costs 23 more runtime than theifchain for case 1, 72 for case 8 and 103 for an unknown case; storage counts are equal. Neither pattern evaluates unselected actions. - This is a control-flow difference, not a guaranteed transaction saving. Re-measure the actual entry point, case distribution, nesting limits and source-size load cost.
Filtering with per-call context
- Clarity has no lambda closures. Pass operation context in a
foldaccumulator, or let a namedfilterpredicate read stored state. - Context/output
fold: no helper storage reads/writes, supports read-only per-call arguments and trait references. A growing result list increases argument/type-checking and append work. - Helper-variable
filter: can use much less runtime, but each predicate visit reads the helper. Setting it first adds one read and one write, even for an empty list. An already-correct persisted policy avoids setup, not the per-item reads. - Before choosing between the two on cost, read the measured fixture (350 items at epochs 2.4 and 4.0) and its Bitflow source examples.
- Before epoch 3.3, callback argument charges use declared maximum type sizes; from 3.3 they use actual values (VM argument charging). Include the accumulator's list bound in historical measurements.
- Set scratch context before every helper-filter invocation; it is contract state, not a captured local. A read-only function cannot perform that setup write; reading existing policy is allowed.
- Prefer the smaller binding budget dimension, not runtime alone. A cheaper filter does not justify replacing a multi-action fold with a boolean filter.
Repeated reads and address constants
- Binding a repeated map read with
letperforms one read instead of two, but the binding can cost more runtime than the saved read: at epoch 4.0 the example adds about 200 runtime units. That trade helps a read-bound path; a runtime-bound path may need the opposite choice. Measure the real entry point. - Re-measure historical optimizations under the active schedule; measurements under costs_3 do not establish savings under costs_5.
- Use supported address constants for readability and repeated identities, after checking their epoch, read-only and dependency limits. A once-used alias costs more runtime than the literal; each further use saves about one runtime unit and one read byte per source byte removed.
Example: binding a repeated read
;; id: repeated-read
(define-map positions principal {collateral: uint, rewards: uint})
(define-read-only (total-before (owner principal))
(+
(get collateral (default-to {collateral: u0, rewards: u0} (map-get? positions owner)))
(get rewards (default-to {collateral: u0, rewards: u0} (map-get? positions owner)))))
(define-read-only (total-after (owner principal))
(let ((position (default-to {collateral: u0, rewards: u0} (map-get? positions owner))))
(+ (get collateral position) (get rewards position))))total-afterperforms one map read instead of two.
Source size and recurring load costs
- Runtime is a deterministic meter, not elapsed time or only the instructions in the selected function. Ordinary contract entry charges
LoadContract, including transaction entry and repeatedcontract-call?calls; local function calls do not add another contract load. - Load size
nis source bytes, including comments/whitespace/unused initialization code, plus the memory size of evaluated constants. Contract caching does not remove the metered charge. cost_load_contract(Costs5 delegates to Costs4 then Costs3): runtimen + 80, read bytesn + 1, read count3. These are load components, not complete call costs. Removingksource bytes saveskruntime units andkread bytes per ordinary load, other behavior and constant sizes unchanged.- Publication also charges
ContractStorageon source lengths(runtime11*s + 7165, write bytess + 1, one write) after parsing and analysis, before top-level code runs. - One tab is one byte, so replacing four spaces saves three bytes; a single space is also one byte. Keep the project's formatting policy: this byte difference does not override it. Measure actual output bytes; formatters may expand tabs before publication.
- Test and hash the exact final artifact. Whitespace changes source hashes and signed deployment bytes even when execution behavior is equivalent. Never rename public functions, tuple fields or event fields solely for size without checking consumers.
- Top-level setup runs once, but its source remains chargeable on later calls; unused helper bodies remain too, and large computed constants add load data. Large initialization is permitted if publication, analysis, execution and memory limits fit: a tradeoff, not a prohibition.
- Move bulky setup data/orchestration to scripts or a separate setup/proposal contract when recurring savings justify the additional interface and calls. Preserve authenticated, bounded initialization and finalization; see deployment-time execution. Splitting frequently used code adds cross-contract loads and changes caller context: benchmark the complete path.
- Sources: load/publication charge sites, stored sizes, Costs5 delegation, effective load/storage formulas.
- To estimate what a whitespace, comment or retained-initialization change saves, read the measured examples.
- Extreme source compaction is explicit opt-in only: read it only when the user asks for it, and do not recommend or apply it during ordinary optimization.
References
- Cost formulas and cost contracts: read before estimating a formula by hand, choosing an epoch's schedule, extracting formulas from a
stacks-corecheckout, or calling a deployedcosts-*.clarcontract. - cost table: generated effective formula of every cost function under costs_3, costs_4 and costs_5. Open it to estimate one operation's charge.
- Versions: historical limits and schedule activation. Recall corrections: common cost and limit mistakes.
Reference baseline
Examples use Clarity 4+ syntax unless marked, checked on epoch 4.0. Source links in this skill and its references use these pins; links into sibling skills use their owner's SKILL.md.
stacks-core: ccf31dc69c.bitflow-dlmm: b4d3694c86.stacks-core-targeted: 01a60adcb6.sips: 642af6804d.clarinet: 52e4f35ea5.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

