/leyline:loop-optimizationfrom leyline
Decides when to hand-optimize loops vs trust the compiler for unrolling, SIMD, fusion, and branchless transforms. Use when reviewing or authoring a hot loop.
Ships in leyline. 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.
None: SKILL.md is the whole skill.
A decision rule for the five common loop transformations. Its value is knowing when manual application is redundant (the compiler already does it) or harmful (it defeats the vectorizer or fools your benchmark).
Skill(pensive:architecture-review).Skill(pensive:performance-review).-O2/-O3, so the manual form is redundant. Leave
unrolling to the compiler as well. Unlike the other two it is not on
by default (GCC needs -funroll-loops), but the compiler owns the
profitability decision and manual unrolling routinely defeats the
auto-vectorizer.restrict /
__restrict__) and loop shape first. Confirm with an optimization
report (-fopt-info-vec-missed, -Rpass-missed=loop-vectorize).
Reach for intrinsics last and accept the portability cost.| Technique | Helps where | When NOT to apply by hand |
|---|---|---|
| Unrolling | C/C++/Rust FP reduction chains (multi-accumulator) | Auto-vectorizable loops (defeats vectorizer); OOO CPUs; icache pressure; Python |
| SIMD / vectorization | C/C++/Rust loops the compiler misses; Python via NumPy | Before fixing aliasing/loop shape; short trip counts; unverified that emitted SIMD runs |
| Loop fusion | Bandwidth-bound array loops; Python via numexpr/Numba | When it spills registers or mixes strided access; compute-bound bodies; blocks vectorization |
| Hoisting (LICM) | Python (no compiler does it); C/C++/Rust only when aliasing blocks the proof | -O2+ compiled code: redundant and can lengthen live ranges |
| Strength reduction | Compilers do it; near-useless by hand | -O2+ compiled code: blocks the compiler's IV analysis and vectorization |
| Branch elimination (branchless) | Hot loops whose branch tracks unpredictable data | Predictable branches, selectivity stably skewed toward one side, sorted or clustered input, before profiling |
A separate axis from the five transforms above. Those change loop structure. This one removes control flow from inside the body. The compiler will not do it for you. Rewriting a conditional push as an unconditional store plus a conditional index advance changes which memory the loop writes, so LLVM cannot apply it as a semantics-preserving transformation.
The lever is branch misprediction rather than instruction count. A branch whose outcome tracks unpredictable data costs roughly 15-20 cycles per miss. A predictable branch (loop conditions, bounds checks) is close to free and needs no treatment at all.
Worked example: filtering 1M random f64 values against a threshold on
an Intel i7-10875H.
| Selectivity | .filter().collect() | Branchless |
|---|---|---|
| 1% | 0.59 ms | 1.09 ms |
| 25% | 2.69 ms | 1.05 ms |
| 50% | 3.94 ms | 1.03 ms |
| 75% | 2.75 ms | 1.02 ms |
| 99% | 1.49 ms | 1.11 ms |
Read that table as variance. Branchless does not make the loop faster. It makes the cost independent of the data, winning the 50% worst case by about 4x and losing the 1% best case by about 2x. The same 50% case on sorted input runs at 0.93 ms under the ordinary branchy filter, beating branchless outright, because a sorted predicate predicts perfectly.
The decision therefore turns on the production selectivity distribution, not on any single benchmark row. Apply it when the predicate is near-random and the worst case is what hurts. Skip it when selectivity is stably skewed, or when input arrives sorted or clustered.
Two costs the timing column hides. The output buffer is allocated at
full input length, so a 1% filter over 1M f64 reserves 8 MB to return
80 KB. And the branchless form is harder to read, which is a
maintenance cost paid on every future edit rather than once.
Source: https://www.greyblake.com/blog/branchless-rust/
Both traps tie into Skill(imbue:proof-of-work): a speedup claim needs
a measurement on representative data.
What SKILL.md sets; muted values are Claude Code's defaults
19 skills in this plugin
/additive-bias-defenseInverts burden of proof for code additions. Use when reviewing PRs, planning refactors, or running unbloat to challenge every addition's necessity./authentication-patternsProvides auth patterns for API keys, OAuth, and token management. Use when implementing or reviewing service authentication and credential handling./content-sanitizationProvides sanitization guidelines for external content in skills and hooks. Use when loading GitHub Issues, PRs, WebFetch results, or any untrusted input./damage-controlRecovers broken agent state via crash recovery, context overflow, and merge conflict protocols. Use when an agent session fails or a worktree is corrupted./decision-journalDecision journal (tradeoffs, lessons learned), deferred-capture contract ([Deferred] issues, CLI args, source labels), storage templates. Use to log a lesson./document-conversionConverts documents and URLs to markdown via tiered fallback (MCP markitdown, native tools, user notice). Use when a skill must ingest PDF, DOCX, or URL content.npx claudepluginhub athola/claude-night-market --plugin leylineOther plugins' skills for the same job