
Code Refinement
Scan your codebase with an agent for O(n²) loops, repeated sorts, and other block-level algorithm waste before you ship or optimize hot paths.
Overview
Code Refinement is an agent skill most often used in Ship (also Build) that detects block-level algorithmic inefficiencies and suggests better algorithms or data structures.
Install
npx skills add https://github.com/athola/claude-night-market --skill code-refinementWhat is this skill?
- Detects nested loops on the same collection and suggests index/hash-map replacements
- Flags repeated sort/search inside loops with sort-once guidance
- Scoped to code-block-level patterns—not system architecture or database query plans
- Includes grep/awk-style detection hints for Python nested-loop anti-patterns
- Parent skill pensive:code-refinement with algorithm-efficiency module metadata
- 4+ documented detection pattern families (nested loops, repeated sort/search, etc.)
- Code-block scope only—not system or DB query optimization
Adoption & trust: 1 installs on skills.sh; 304 GitHub stars; 2/3 security scanners passed (skills.sh audits); trending (+100% hot-view momentum).
What problem does it solve?
Your agent shipped working code that still hides O(n²) loops and repeated sorts that will hurt real users once data grows.
Who is it for?
Indie backends and scripts where you can grep the repo and want checklist-driven complexity review without a dedicated performance team.
Skip if: Database index design, caching topology, or fleet-wide profiling—this module excludes architecture and query-plan optimization by design.
When should I use this skill?
You need block-level algorithm or complexity improvements on existing code, especially nested loops and repeated sorting patterns.
What do I get? / Deliverables
You get targeted refactor patterns and detection cues so the agent can replace wasteful blocks with indexed lookups, single sorts, or clearer complexity—ready for a focused perf PR.
- List of anti-patterns with suggested refactors
- Optional grep commands to locate nested-loop hotspots
Recommended Skills
Journey fit
Spans multiple journey phases - primary shelf plus alternate fits below.
Performance and algorithm fixes belong on the ship shelf because solo builders invoke this after features exist and before release or when latency spikes—not during initial idea validation. Canonical placement is perf: the module targets time/space complexity and block-level optimization patterns, explicitly not architecture or SQL tuning.
Where it fits
While implementing batch matching over in-memory lists, run refinement to avoid accidental quadratic joins.
Before launch, scan hot paths for sorts inside request handlers that should be precomputed once.
During PR review, ask the agent to flag nested-loop patterns the human skimmed past.
When production metrics show CPU spikes on a job, re-run block-level checks on the reported module.
How it compares
Use instead of vague “make it faster” chat requests when you want procedural anti-pattern detection at the loop level.
Common Questions / FAQ
Who is code-refinement for?
Solo builders and small teams using Claude Code, Cursor, or Codex who want agent-guided algorithm cleanup on existing code before or right after ship.
When should I use code-refinement?
During Ship perf work when endpoints lag; during Build when implementing list-heavy features; after review when grep shows nested loops on the same collection.
Is code-refinement safe to install?
It is read/analysis oriented (Read, Grep, Glob)—review the Security Audits panel on this Prism page and inspect the parent night-market repo before granting broad filesystem access.
SKILL.md
READMESKILL.md - Code Refinement
# Algorithm Efficiency Module Identify time and space complexity inefficiencies at the code block level. ## Scope This module focuses on **code-block-level** optimizations, not system architecture or database query optimization. It catches patterns where a better algorithm or data structure eliminates unnecessary work. ## Detection Patterns ### 1. Nested Loop on Same Collection (O(n^2) -> O(n) or O(n log n)) ```python # Anti-pattern: O(n^2) lookup for item in items: for other in items: if item.id == other.parent_id: ... # Better: O(n) with index index = {item.id: item for item in items} for item in items: parent = index.get(item.parent_id) ``` **Detection:** ```bash # Find nested for-loops on same variable (Python) grep -n "for .* in " --include="*.py" -r . | \ awk -F: '{file=$1; line=$2; var=$0; gsub(/.*in /,"",var); gsub(/:.*/,"",var); print file, line, var}' | \ sort | uniq -f2 -d ``` ### 2. Repeated Sort / Search ```python # Anti-pattern: sorting inside a loop for query in queries: sorted_data = sorted(data) # O(n log n) per query = O(m * n log n) result = bisect.bisect(sorted_data, query) # Better: sort once sorted_data = sorted(data) # O(n log n) once for query in queries: result = bisect.bisect(sorted_data, query) # O(m * log n) ``` **Detection:** ```bash # Find sort/sorted inside loops grep -n "sorted\|\.sort()" --include="*.py" -r . | while read line; do file=$(echo "$line" | cut -d: -f1) num=$(echo "$line" | cut -d: -f2) # Check if inside a for/while loop sed -n "$((num-5)),$((num))p" "$file" | grep -q "for \|while " && echo "SORT_IN_LOOP: $line" done ``` ### 3. List Where Set/Dict Suffices ```python # Anti-pattern: O(n) membership test if item in large_list: # O(n) ... # Better: O(1) membership test large_set = set(large_list) if item in large_set: # O(1) ... ``` **Detection:** ```bash # Find "in list_var" patterns (heuristic) grep -n " in \[" --include="*.py" -r . grep -n " not in " --include="*.py" -r . | grep -v "not in {" | grep -v "not in set(" ``` ### 4. String Concatenation in Loop ```python # Anti-pattern: O(n^2) string building result = "" for item in items: result += str(item) + ", " # Better: O(n) with join result = ", ".join(str(item) for item in items) ``` ### 5. Unnecessary Intermediate Collections ```python # Anti-pattern: builds full list just to iterate all_items = [transform(x) for x in data] # allocates full list for item in all_items: process(item) # Better: generator (lazy evaluation) for item in (transform(x) for x in data): process(item) ``` ### 6. Repeated Computation (Missing Memoization) ```python # Anti-pattern: recomputes expensive value def get_result(n): # called 1000x with same n values return expensive_compute(n) # Better: cache from functools import lru_cache @lru_cache(maxsize=128) def get_result(n): return expensive_compute(n) ``` ## Complexity Estimation Heuristics Rather than formal Big-O analysis, use practical heuristics: | Pattern | Likely Complexity | Flag When | |---------|------------------|-----------| | Single loop over data | O(n) | Data > 10K and no early exit | | Nested loop, same data | O(n^2) | Always flag | | Sort inside loop | O(m * n log n) | Always flag | | `in list` inside loop | O(n * m) | List > 100 items | | Recursive without memo | O(2^n) potential | Recursive calls > 1 | | String concat in loop | O(n^2) | Loop > 100 iterations | ## Scoring | Pattern | Severity | Confidence | |---------|----------|------------| | Nested loop, same data | HIGH | 85% | | Sort/search in loop | HIGH | 90% | | List where set suffices | MEDIUM | 80% | | String concat in loop | MEDIUM