agentsclimarketplace

Performance optimization

Skill whitequeen306/code-cortex-loop/skills/performance-optimization

Deep performance analysis for /cortexloop pass 5. Hot paths, complexity, measurement — go deep with proof; defer correctness and security to other passes.From its SKILL.md

Install
npx -y skills add whitequeen306/code-cortex-loop --skill performance-optimization

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • 15 stars15 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

SKILL.md

5.9 KB, as published. Nobody here has run it

Performance Optimization

Methodology adapted from performance-deity (MIT). Every optimization must be proven with real numbers.

Overview

Optimize code by measuring first, analyzing bottlenecks, applying targeted fixes, and proving improvement with before/after benchmarks. Never guess at performance — measure, fix, verify.

When to Use

  • Code review flags slow paths, N+1 queries, or unbounded operations
  • /cortexloop performance pass
  • User reports latency, jank, or high memory usage
  • Before/after a refactor that touches hot paths
  • React/Next.js apps with unnecessary re-renders
  • API endpoints with missing pagination or eager loading

When NOT to use:

  • Code is not on a hot path and no measurable problem exists
  • Premature optimization would harm readability without measurable gain
  • You cannot run benchmarks in the current environment (note this and use static analysis only)

The Four Phases (Required)

Execute all four phases in order. Do not skip any phase when applying fixes in Direct mode.

Phase 1 — Establish Baseline

  1. Identify the exact code path to optimize (function, query, component, endpoint).
  2. Run a micro-benchmark:
    • Write a temporary benchmark script in the workspace.
    • Include a warm-up phase (≥10 iterations, discard results).
    • Run ≥100 iterations; record Average and P95 execution time.
    • Run via terminal, then delete the temporary script.
    • Fallback: use time / built-in profilers if the script fails due to missing deps.
  3. Record baseline numbers before writing any optimized code.
  4. In Report mode, include baseline numbers in the finding even if no fix is applied yet.

Phase 2 — Algorithmic Analysis

State explicitly:

  • Time complexity (Big-O) of the current implementation
  • Space complexity and primary allocation sites
  • Bottleneck in one precise sentence, e.g.:
    • "Nested loops causing O(n²) scaling"
    • "N+1 query: one SELECT per item in loop"
    • "Full table scan — missing index on user_id"
    • "React parent re-render cascades to all list items"
    • "Synchronous file read blocks event loop"

Phase 3 — Refactoring

Apply fixes in priority order (highest impact first):

CategoryPatterns to fixTypical fix
DatabaseN+1, missing indexes, SELECT *JOIN/batch load, EXPLAIN ANALYZE, add index
AlgorithmO(n²) lookups, repeated scansHash map/set, sort + binary search
MemoryUnbounded arrays, leak refsPagination, streaming, weak refs / cleanup
AsyncSync I/O on hot pathasync/await, parallel with limits
NetworkOver-fetching, no cacheGraphQL field selection, ETag, compression
FrontendRe-renders, large bundlesmemo, virtualization, code splitting
BuildSlow CI, cold buildsCache layers, incremental builds

After each change in Direct mode:

  1. Re-run the benchmark from Phase 1.
  2. If not measurably faster → revert and try a different approach.
  3. Run existing tests — optimization must not break behavior.

Phase 4 — Report

Present a Performance Report table for every applied optimization:

MetricBaselineOptimizedΔ
AverageXmsYms-Z%
P95XmsYms-Z%

Follow with a one-paragraph explanation grounded in CPU/memory/I/O theory.

In Report mode (no changes applied), use this table with projected improvements and mark as estimated until verified.

Stack-Specific Checklists

Database (SQL / ORM)

- [ ] Run EXPLAIN ANALYZE on slow queries
- [ ] Check for N+1 (ORM eager vs lazy loading)
- [ ] Verify indexes on WHERE/JOIN/ORDER BY columns
- [ ] Pagination on list endpoints (limit + cursor/offset)
- [ ] Avoid SELECT * — fetch only needed columns

React / Next.js

- [ ] Profile with React DevTools — identify avoidable re-renders
- [ ] memo/useMemo/useCallback only where profiling shows benefit
- [ ] Virtualize long lists (react-window, tanstack-virtual)
- [ ] Dynamic import for heavy components
- [ ] Check bundle size impact (next/bundle-analyzer or equivalent)

Node.js / Backend

- [ ] No sync fs/crypto on request path
- [ ] Connection pooling for DB/Redis
- [ ] Rate-limit expensive endpoints
- [ ] Cache pure/read-heavy responses with TTL

Python

- [ ] Profile with cProfile/py-spy before optimizing
- [ ] Vectorize with numpy/pandas where appropriate
- [ ] Use generators for large datasets
- [ ] Avoid repeated regex compilation in loops

Severity Classification (for /cortexloop aggregation)

SeverityCriteria
CriticalO(n²) or worse on user-facing path; unbounded memory; blocks event loop
HighN+1 queries; missing pagination; measurable P95 > 500ms on core flow
MediumAvoidable re-renders; missing cache; suboptimal but acceptable
LowMicro-optimization; premature unless hot path

Output Format (Report mode)

## Performance Findings

### [SEVERITY] [Title]
- **Location:** path:line
- **Bottleneck:** [precise description]
- **Current complexity:** O(?)
- **Baseline:** Avg Xms, P95 Yms (or "not measured — static analysis only")
- **Recommendation:** [specific fix]
- **Projected improvement:** [estimate or measured Δ]

Rules

  1. No optimization without measurement — except static analysis findings flagged as "unverified"
  2. Preserve behavior — performance changes must pass all existing tests
  3. One change at a time in Direct mode — benchmark after each
  4. Prefer architectural fixes over micro-optimizations (fix N+1 before tweaking loops)
  5. Document trade-offs — if a fix adds complexity, say so

Keep looking

Skills are one crate of 627,801. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.