Files
peltier.io/playbook/05-algorithmic.md
T
Claude 328aafae77 Bootstrap hotpath from engineering spec v0.1
Commit the spec and scaffold the system it describes:

- SPEC.md: the full v0.1 engineering spec
- crates/: trust-layer Rust workspace, unit-tested
  - bench-runner: interleaved ABAB scheduling with discarded warm-ups,
    deterministic bootstrap CIs over the ratio of medians,
    CI-lower-bound accept rule, env fingerprinting, and a CLI with
    compare + A/A self-test modes (A/A verified passing end-to-end)
  - diff-test: gate outcome model, sha256 corpus pin verifier that
    refuses on mismatch, per-target equivalence policy parser
    (byte-identical default, explicit fp-tolerance opt-in)
  - ledger: append-only SQLite attempt schema with UPDATE/DELETE
    refused by triggers, anti-double-attempt query
  - report: cores-saved and dollars/yr ROI math over speedup CIs,
    pricing.toml inputs
- playbook/: classes 1-7 with preconditions, procedure, verification
  notes, and known failure modes per class
- agent/: Python package skeleton — six-tool contract and prompting
  spine, harness IPC deferred to Phase 1
- config/: accept.toml (2% threshold, 30 runs/side), pricing.toml
- targets/, corpora/, results/calibration/: layout and policy docs

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EcKPWQ3MQD4NdEHwar96u2
2026-07-06 18:34:51 +00:00

44 lines
2.1 KiB
Markdown

# Class 5 — Algorithmic
Complexity class and primitive selection. Highest variance: the biggest
wins live here, and so do the most seductive wrong ideas.
## Preconditions
- Profile signature: one dominant frame that is *algorithm*, not memory
(high IPC, low miss rate, still slow); superlinear scaling of runtime
with input size; hot `contains`/`find` over linear structures; repeated
recomputation of pure functions on repeated arguments.
## Procedure (cheapest first)
1. Better std/library primitive: `HashMap` for linear scans, `sort_unstable`
over `sort`, `entry()` API over get-then-insert, buffered I/O,
`memchr`-class scanning.
2. Precomputation: hoist invariant work out of loops; build lookup tables
at startup when the domain is small.
3. Memoization for pure hot functions with high argument repeat rates —
confirm the repeat rate from a counting instrument before adding a
cache.
4. Complexity-class replacement (the real prize): O(n²) → O(n log n)
with a known-correct algorithm. Prefer a well-tested library
implementation over a hand-rolled one, always.
## Verification notes
- HashMap iteration order is not deterministic order: if results flow
from iteration order into output, replay diverges — either sort at the
boundary or reject the change.
- Memoization + interior mutability in multithreaded targets touches the
concurrency review tier (SPEC §8).
- Differential fuzz is load-bearing here: algorithmic replacements have
edge-case surfaces (empty input, duplicates, already-sorted, unicode)
where "equivalent" algorithms differ. 10k iterations minimum, seeded
with corpus-derived cases.
## Known failure modes
- Asymptotic win, constant-factor loss at real input sizes — the ledger
records the workload; the verdict is measured at that workload.
- Caches without bounds: memoization that trades a 10% speedup for
unbounded RSS growth. Check max RSS; bound the cache.
- "Equivalent" floating-point reformulations that aren't (different
rounding paths) — FP output changes route to the tolerance policy or
human review, never silently absorbed.