Premature optimization changes code for performance before expected workload or service-level requirements justify the change. Optimizing an arbitrary path can add complexity and maintenance cost without improving production performance. The alternative is not to ignore performance, but to measure the workload and focus on code that matters. section 7.0
Use evidence to decide where optimization belongs:
- Establish expected traffic, input sizes, and service-level requirements before choosing an optimization. section 7.0 section 7.5
- Simulate representative traffic and identify the hot path. Compare the likely benefit with the size and review cost of the proposed changes. section 7.0 section 7.5
- Use latency percentiles to assess responsiveness and mean execution time with request rate to estimate aggregate workload. A p99 value is not an average. section 7.2.3
- Benchmark alternatives with production-like inputs and scrutinize the benchmark itself, including warm-up and runtime effects. section 7.1.2
Concurrency without a workload requirement
An account-lookup prompt assumed multithreading was needed. The LLM generated several concurrent strategies and a custom benchmark that assumed 100,000 accounts without asking. After the count was reduced to a production-like 10,000, the custom benchmark showed the single-threaded version outperforming the alternatives. One concurrent implementation repeatedly called indexOf while grouping accounts, introducing quadratic work. The benchmark’s warm-up and runtime risks mean its timings should be treated cautiously, not as definitive measurements. section 7.1.2
Listing 7.7 The custom benchmark results
Single-threaded time: 25 ms Parallel stream time: 63 ms Concurrent time: 30398 ms Optimized time: 406 ms
The custom benchmark results illustrate the apparent slowdown; validate the comparison with a reliable benchmark before relying on the exact timings. section 7.1.2
Choose the path that carries the load
A word service receives about one word-of-the-day request for every 20 validation requests. In the simulated workload, their p99 latencies are 33 ms and almost 82 ms respectively. Mean latencies and request rates indicate that validation accounts for over 98% of request-handling work. Optimizing validation is therefore a more useful starting point than changing the less-used endpoint. section 7.2.3
When using an LLM, supply measured traffic and input limits, then restrict the proposed change to the identified hot path. Review and benchmark the result: an assistant can amplify an unsupported assumption or optimize unrelated code even when given hot-path information. section 7.1.2 section 7.5




