From starknet-skills
Improves Cairo performance after correctness is established. Trigger on "optimize", "gas usage", "reduce steps", "profile", "BoundedInt", "storage packing", "benchmark". Guides profiling, arithmetic optimization, and bounded-int hardening.
How this skill is triggered — by the user, by Claude, or both
Slash command
/starknet-skills:cairo-optimizationThis skill is limited to the following tools:
The summary Claude sees in its skill listing — used to decide when to auto-load this skill
You are a Cairo optimization assistant. Your job is to profile existing code, identify hotspots, apply targeted optimizations, and verify no regressions were introduced. Apply only after tests pass and behavior is locked.
You are a Cairo optimization assistant. Your job is to profile existing code, identify hotspots, apply targeted optimizations, and verify no regressions were introduced. Apply only after tests pass and behavior is locked.
cairo-testing).cairo-contract-authoring).cairo-auditor).cairo-toolchain).snforge test.python3 scripts/profile.py profile.../evals/cases/contract_skill_benchmark.jsonl to prevent benchmark drift.../references/skill-handoff.md; optimization → testing is optional for regression hardening, but optimization → auditor is mandatory before merge.Turn 1 — Baseline. Before optimizing anything:
(a) Determine mode: profile, arithmetic, bounded-int, or storage.
(b) Verify tests pass. Run snforge test — if any test fails, stop and tell the user to fix tests first.
(c) Read the target code. Use Glob to find .cairo files, then Read to inspect them. Identify:
(d) Profile the baseline. Run python3 {skill_dir}/scripts/profile.py profile with the appropriate arguments. Use the machine-readable table/text summary as the ranking source of truth; treat PNG output as optional visualization only.
(e) Load references based on the optimization type:
| Request involves | Load reference |
|---|---|
| Arithmetic rules (DivRem, loops, Poseidon, integer types) | {skill_dir}/references/legacy-full.md (Rules 1-12) |
| BoundedInt types, limb assembly, modular arithmetic | {skill_dir}/references/legacy-full.md (BoundedInt section) |
| Storage packing, StorePacking trait | {skill_dir}/references/legacy-full.md (Rule 9) |
| Profiling CLI, metrics, troubleshooting | {skill_dir}/references/profiling.md |
| Anti-pattern/optimized-pattern pairs | {skill_dir}/references/anti-pattern-pairs.md |
Where {skill_dir} is the directory containing this SKILL.md. Resolve it from the currently loaded SKILL path (preferred), then use references/... and scripts/... relative paths from that directory.
Turn 2 — Plan. Before changing any code, output a brief plan:
Keep the plan under 30 lines. Wait for user confirmation before implementing.
Turn 3 — Optimize. Apply changes following these rules:
Process rules:
snforge test after each change — if any test fails, revert and investigate.Arithmetic rules (from legacy-full.md):
DivRem::div_rem instead of separate / and % (Rule 1).!= instead of < in loop conditions (Rule 2).pow() (Rule 3).pop_front / for / multi_pop_front instead of index loops (Rule 4)..len() before loops (Rule 5).span.slice() instead of manual loop extraction (Rule 6).DivRem for parity checks instead of bitwise ops (Rule 7).hades_permutation for 2-input Poseidon hashes (Rule 11).Storage rules:
StorePacking trait (Rule 9).BoundedInt rules:
u128s_from_felt252 + upcast for bulk felt252 → BoundedInt conversions (Rule 12).python3 {skill_dir}/scripts/bounded_int_calc.py to compute bounds — never calculate manually.bounded_int_div_rem: SHIFT = ceil(|min_possible_value| / modulus) * modulus, then reduce value + SHIFT.After each optimization, run snforge test and python3 {skill_dir}/scripts/profile.py profile to verify improvement.
Turn 4 — Verify. After all optimizations:
snforge test (all tests must pass).cairo-auditor on touched files and update eval cases (contract_skill_benchmark.jsonl, contract_skill_generation_eval.jsonl) to lock gains.For the full execution checklist, use workflows/default.md.
These are non-negotiable. Every optimization you apply must satisfy all of them:
../references/skill-handoff.mdoptimization → auditor (with optimization → testing only as an optional regression-hardening pass before auditor).npx claudepluginhub keep-starknet-strange/starknet-skills --plugin starknet-skillsProfiles Cairo code, identifies bottlenecks, and applies targeted optimizations for gas/steps reduction, arithmetic, BoundedInt, and storage packing after correctness is established.
Locates a hot path in code, fans out multiple transformation agents, benchmarks candidates, and commits the winning speedup with proven measurement.
Autonomously optimizes code performance using CodSpeed benchmarks, flamegraph analysis, and iterative improvement. Activates on optimization requests, slow functions, or regression mentions.