Awesome Reviewers

Reviewer · Performance Optimization

Benchmark-driven hot-path tuning

Treat performance work on hot paths as an evidence-based exercise: identify the path with profiling/benchmarks, pick the right data structure and allocation strategy for the actual access pattern, and bound any work whose cost can grow unexpectedly. Concretely:

Instruction

Treat performance work on hot paths as an evidence-based exercise: identify the path with profiling/benchmarks, pick the right data structure and allocation strategy for the actual access pattern, and bound any work whose cost can grow unexpectedly. Concretely:

  • Before changing an algorithm or data structure for speed, benchmark it (including edge cases such as low and high call counts) and double-check that the numbers actually support the described trade-off, e.g. a “break-even point” claim should match the benchmark output.
  • On a hot path, prefer the structure that fits the real cardinality: for small, fixed-size collections (rule of thumb: up to ~16 items) use a slice or, for a known-small fixed set like 2 classes, explicit struct fields instead of a map — map lookups and hashing are comparatively expensive at that scale.
  • Avoid unnecessary allocations in loops: reuse a scratch map/buffer across iterations instead of recreating it per call, only clone/trim a slice down from its backing capacity when the spare capacity is actually large relative to what’s needed, and reuse decoders/encoders as shared instances instead of re-allocating them per call.
  • Bound unbounded or input-driven work: cap the number of alternatives, literals, or searches scanned per item (e.g., capping regex alternation checks, or the number of literal searches performed on a line) so that cost doesn’t scale linearly with attacker/user-controlled input size; short-circuit loops as soon as a threshold is met instead of scanning everything and discarding the result.
  • Deduplicate before doing expensive work (e.g., building a set before cloning/copying values) rather than doing the expensive operation on every occurrence.
  • When restructuring a system for pipelining or splitting work into tasks for performance, verify the full chain the optimization depends on (e.g., confirm that downstream consumers actually honor the narrowed section/row reference) — an optimization that doesn’t propagate end-to-end can silently work against its own goal by adding extra I/O or overhead.
  • Don’t over-engineer for a theoretical case; if a profile doesn’t show a cost, keep the simpler implementation and revisit only when data justifies the added complexity.

Example — preferring a slice over a map for a small, bounded set on a hot path:

// Avoid: map lookup cost on a hot path for at most 2 items.
type tocScopedBuilders struct {
    builders map[class]logsBuilder
}

// Prefer: explicit fields, no hashing/lookup overhead.
type tocScopedBuilders struct {
    inTimeBuilder logsBuilder
    lateBuilder   logsBuilder
}