Measure Before Optimize

lesson workflow active workflow/measure-before-optimize.md View on GitHub

Measure Before Optimize

Rule

Always measure and profile to identify actual performance bottlenecks before implementing optimizations.

Context

When considering performance improvements or caching implementations for any system component.

Detection

Observable signals indicating need for measurement:

Pattern

Measure first, then decide based on data:

# Measure performance with profiling
pytest tests/test_lessons*.py --profile --durations=10

# Analyze results to identify actual bottlenecks
# - Check top time consumers
# - Look for operations >100ms
# - Document baseline metrics

# Decision based on data:
# - No bottleneck found? No optimization needed
# - Bottleneck found? Target specific operations

Anti-pattern: Optimization without measurement

# smell: assumption without data
# "This looks complex, so it must be slow"
# Implements caching for lesson parsing
# No profiling data to support the need

Anti-pattern: Latency-win claims in PR descriptions / issue comments

# smell: PR body says "simple lookups should drop to ~5-10s"
# but no before/after timing is shown, and the target number
# is anchored on one component (prompt size) rather than on
# the actual cost model (turns × tok/s + startup + tool-use).

# Correct shape:
# - show baseline (e.g. "currently p50 = 42s over N calls")
# - explain cost model and which component dominates
# - project delta from the dominant component, not from the
#   smallest one
# - after landing, post measured delta — not re-projected one

Outcome

Following this pattern leads to:

Benefits:

Related

Match Keywords

should drop to should be faster needs caching add caching without a baseline no baseline number before/after timing p50 = is the bottleneck pytest --profile