Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Performance and Resource Management Principles

53. Premature Optimization Principle

Definition. Avoid optimizing before identifying actual bottlenecks; measure first, then optimize the hot paths.

Rationale. Guessing at bottlenecks wastes effort and often complicates code for no gain. Measurement reveals where time is actually spent; optimize there.

Example. Profile before rewriting a loop; the bottleneck may be I/O, not CPU.

Anti-pattern / failure mode. Micro-optimizing cold code while ignoring measured hot paths; “optimization” that harms readability. Review signal: complexity added without profiling evidence.

Related. Locality of Reference (#54), Data-Oriented Design (#60).

54. Locality of Reference

Definition. Favor access patterns that improve cache efficiency; keep related data close in memory.

Rationale. Modern CPUs are fast but memory is slow; cache misses dominate. Layouts that access contiguous, related data exploit caches and drastically improve throughput.

Example. Store fields of a struct together and iterate arrays of structs rather than chasing pointers.

Anti-pattern / failure mode. Pointer-heavy structures with scattered allocations; random access over large datasets. Review signal: high cache-miss rates in profiling.

Related. Data-Oriented Design (#60), Zero-Copy (#59).

55. Batch Processing Principle

Definition. Group operations to reduce overhead; amortize fixed costs across many items.

Rationale. Per-operation overhead (syscalls, network round trips, transactions) dominates small operations. Batching amortizes these costs and increases throughput.

Example. Insert 1000 rows in one transaction instead of 1000 individual commits.

Anti-pattern / failure mode. Per-item network/DB calls in a loop; “chatty” APIs. Review signal: high request counts for bulk work.

Related. Resource Pooling (#57), Concurrency over Parallelism (#61).

56. Lazy Evaluation

Definition. Delay computation until its result is needed; avoid work that may never be used.

Rationale. Eager computation wastes resources on results that may be discarded. Laziness defers cost and can avoid it entirely.

Example. Generate a report only when a user opens it; compute derived fields on demand.

Anti-pattern / failure mode. Eagerly loading large datasets or computing expensive values “just in case.” Review signal: memory/CPU spent on unused results.

Related. Premature Optimization (#53), Resource Pooling (#57).

57. Resource Pooling

Definition. Reuse expensive resources instead of repeatedly creating them; pool connections, threads, or buffers.

Rationale. Creating resources (connections, processes, large buffers) is costly. Pooling amortizes creation and bounds resource usage.

Example. A database connection pool reused across requests.

Anti-pattern / failure mode. Opening a new connection per request; unbounded allocation of expensive objects. Review signal: high allocation/GC or connection churn.

Related. Bounded Resource (#58), Batch Processing (#55).

58. Bounded Resource Principle

Definition. Put explicit limits on memory, connections, queues, and workloads; prevent unbounded growth from exhausting resources.

Rationale. Unbounded structures grow until the process crashes. Explicit bounds provide backpressure and predictable behavior under load.

Example. Cap a request queue at 1000 items and reject or block beyond that.

Anti-pattern / failure mode. Unbounded in-memory queues, caches, or buffers. Review signal: OOM under load; latency spikes from GC.

Related. Backpressure (#48), Resource Pooling (#57).

59. Zero-Copy Principle

Definition. Minimize unnecessary data copying; pass references or use buffers that can be shared.

Rationale. Copying data wastes CPU and memory bandwidth and increases latency. Avoiding copies, especially in hot paths, improves throughput and reduces GC pressure.

Example. Send a file from disk to a socket using sendfile instead of reading into user space.

Anti-pattern / failure mode. Repeatedly copying large payloads between layers. Review signal: high CPU in serialization/copy code.

Related. Locality of Reference (#54), Data-Oriented Design (#60).

60. Data-Oriented Design

Definition. Organize data to optimize access patterns and processing efficiency; design for how data is read and transformed.

Rationale. Object-oriented layouts often scatter related data, hurting cache and throughput. Designing around data access patterns yields simpler, faster processing.

Example. Store positions as arrays of x/y/z rather than an array of Point objects for tight loops.

Anti-pattern / failure mode. Deep object graphs traversed in hot loops. Review signal: profiling shows time in field access/chasing.

Related. Locality of Reference (#54), Zero-Copy (#59).

61. Concurrency over Parallelism

Definition. Structure independent activities to make progress without necessarily executing simultaneously; concurrency is about structure, parallelism about execution.

Rationale. Concurrency decomposes a program into independent tasks that can interleave; parallelism runs them at once. Designing for concurrency (clear ownership, message passing) yields correct, scalable systems even on one core.

Example. An event loop handling many connections concurrently on a single thread.

Anti-pattern / failure mode. Confusing the two and bolting threads onto tightly coupled code. Review signal: data races and lock contention from “just add threads.”

Related. Share Nothing (#103), Message Passing (#106).