$npx -y skills add keep-starknet-strange/starknet-agentic --skill cairo-optimizationImproves 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.
| 1 | # Cairo Optimization |
| 2 | |
| 3 | 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. |
| 4 | |
| 5 | ## When to Use |
| 6 | |
| 7 | - Reducing gas/steps in hot paths after correctness is established. |
| 8 | - Profiling Cairo functions to find bottlenecks. |
| 9 | - Rewriting expensive arithmetic, loops, or storage patterns. |
| 10 | - Applying BoundedInt optimizations for limb assembly and modular arithmetic. |
| 11 | - Packing storage fields to reduce slot usage. |
| 12 | |
| 13 | ## When NOT to Use |
| 14 | |
| 15 | - Early feature prototyping without tests (write tests first with `cairo-testing`). |
| 16 | - Contract architecture decisions (`cairo-contract-authoring`). |
| 17 | - Security audit (`cairo-auditor`). |
| 18 | - Deployment operations (`cairo-deploy`). |
| 19 | |
| 20 | ## Quick Start |
| 21 | |
| 22 | 1. Confirm tests pass with `snforge test`. |
| 23 | 2. Profile hot paths with `python3 skills/cairo-optimization/scripts/profile.py profile`. |
| 24 | 3. Load references based on optimization type — see the table in [Orchestration](#orchestration). |
| 25 | 4. Apply one optimization class at a time, re-test after each. |
| 26 | 5. Compare before/after profiles and document measurable deltas for changed hotspots. |
| 27 | 6. Encode stable optimization regressions in `../../evals/cases/contract_skill_benchmark.jsonl` to prevent benchmark drift. |
| 28 | 7. Emit a handoff block using `../references/skill-handoff.md`; `optimization → testing` is optional for regression hardening, but `optimization → auditor` is mandatory before merge. |
| 29 | |
| 30 | ## Rationalizations to Reject |
| 31 | |
| 32 | - "Let's optimize before we have tests." |
| 33 | - "We can skip re-profiling — the change is obviously better." |
| 34 | - "BoundedInt types are too complex — let's use raw u128 math." |
| 35 | - "We can calculate bounds by hand instead of using the CLI tool." |
| 36 | |
| 37 | ## Mode Selection |
| 38 | |
| 39 | - **profile**: User wants to find bottlenecks. Run profiler, identify hotspots. |
| 40 | - **arithmetic**: User wants to optimize math (DivRem, loops, Poseidon). Apply rules from legacy-full.md. |
| 41 | - **bounded-int**: User wants BoundedInt optimization for limb assembly or modular arithmetic. |
| 42 | - **storage**: User wants to pack storage fields or reduce slot usage. |
| 43 | |
| 44 | ## Orchestration |
| 45 | |
| 46 | **Turn 1 — Baseline.** Before optimizing anything: |
| 47 | |
| 48 | (a) Determine mode: `profile`, `arithmetic`, `bounded-int`, or `storage`. |
| 49 | |
| 50 | (b) Verify tests pass. Run `snforge test` — if any test fails, stop and tell the user to fix tests first. |
| 51 | |
| 52 | (c) Read the target code. Use Glob to find `.cairo` files, then Read to inspect them. Identify: |
| 53 | - Hot-path functions (called frequently or with expensive operations). |
| 54 | - Current arithmetic patterns (division, modulus, loops, bitwise ops). |
| 55 | - Storage layout (field types, packing opportunities). |
| 56 | - BoundedInt usage or opportunities. |
| 57 | |
| 58 | (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. |
| 59 | |
| 60 | (e) Load references based on the optimization type: |
| 61 | |
| 62 | | Request involves | Load reference | |
| 63 | |-----------------|---------------| |
| 64 | | Arithmetic rules (DivRem, loops, Poseidon, integer types) | `{skill_dir}/references/legacy-full.md` (Rules 1-12) | |
| 65 | | BoundedInt types, limb assembly, modular arithmetic | `{skill_dir}/references/legacy-full.md` (BoundedInt section) | |
| 66 | | Storage packing, StorePacking trait | `{skill_dir}/references/legacy-full.md` (Rule 9) | |
| 67 | | Profiling CLI, metrics, troubleshooting | `{skill_dir}/references/profiling.md` | |
| 68 | | Anti-pattern/optimized-pattern pairs | `{skill_dir}/references/anti-pattern-pairs.md` | |
| 69 | |
| 70 | 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. |
| 71 | |
| 72 | **Turn 2 — Plan.** Before changing any code, output a brief plan: |
| 73 | |
| 74 | 1. **Hotspots** — list the top 3-5 functions by step cost from the profile. |
| 75 | 2. **Optimizations** — for each hotspot, identify which rule(s) apply (by number from legacy-full.md). |
| 76 | 3. **Anti-patterns found** — list any anti-pattern/optimized-pattern pairs detected in the code. |