Chapter 14

Algorithms, data structures, and runtime instrumentation

This chapter connects the choice of an algorithm §14.1 or data structure to the operations it must support and to the cost of each operation, and states the assumptions under which those costs hold. It begins with asymptotic complexity §14.2, the growth of cost with the size of the input, in the worst case, in the average case and in the amortized sense, which averages over a sequence of operations. It then describes the common structures: arrays §14.5 and linked lists §14.6; hash tables §14.7, their load factors §14.9 and their collisions, which differ from the collisions that matter in cryptography; balanced search trees §14.10, interval structures and half-open intervals §14.12; sets and multisets §14.13; queues, ring buffers §14.15 and priority queues §14.16; and graphs, with reachability §3.21 search and the topological ordering §14.19 of directed acyclic graphs §14.17. Memoization §14.20, caching and cache invalidation §14.21, callbacks §14.22 and event loops §14.23 complete the first part.

The second half is about observing a running program. Runtime instrumentation §14.24 adds code or hardware events that record what a program does, either statically, when the program is built, or dynamically, while it runs, through probes and hooks, function wrappers §14.27, trampolines §14.28, breakpoints §14.29 and watchpoints. The chapter compares tracing every event with sampling some of them and explains sampling bias §14.31, lost trace records §14.32, patching a running program at a quiescent point §14.34, and how to know which code the instrumentation covers. The final entries are about performance: benchmark harnesses, whether computation or communication is the bottleneck, queueing delay §14.38 and Little’s law §14.39, and deamortization §14.40, which spreads the migration of a table to a larger array over many operations so that no single operation is slow while every lookup still finds the right entry.

In this chapter

  1. 14.1 Algorithm
  2. 14.2 Asymptotic complexity
  3. 14.3 Worst-case versus average-case complexity
  4. 14.4 Amortized analysis
  5. 14.5 Array
  6. 14.6 Linked list
  7. 14.7 Hash table
  8. 14.8 Hash collision versus cryptographic collision
  9. 14.9 Load factor
  10. 14.10 Balanced search tree
  11. 14.11 Interval data structure
  12. 14.12 Half-open interval
  13. 14.13 Set versus multiset
  14. 14.14 Queue and FIFO
  15. 14.15 Ring buffer
  16. 14.16 Priority queue
  17. 14.17 Directed acyclic graph (DAG)
  18. 14.18 Graph reachability search
  19. 14.19 Topological ordering
  20. 14.20 Memoization and caching
  21. 14.21 Cache invalidation
  22. 14.22 Callback
  23. 14.23 Event loop
  24. 14.24 Runtime instrumentation
  25. 14.25 Static versus dynamic instrumentation
  26. 14.26 Probe and hook
  27. 14.27 Function wrapper
  28. 14.28 Trampoline
  29. 14.29 Breakpoint and watchpoint
  30. 14.30 Trace versus sampling
  31. 14.31 Sampling bias
  32. 14.32 Tracing buffer loss
  33. 14.33 Dynamic patching and hot update
  34. 14.34 Quiescent point
  35. 14.35 Instrumentation coverage
  36. 14.36 Benchmark harness
  37. 14.37 Computational versus communication bottleneck
  38. 14.38 Queueing delay
  39. 14.39 Little’s law
  40. 14.40 Deamortization: spreading array migration while preserving lookup meaning