Profiling and Code Optimization
2026-09-17
This module: fundamentals of single-thread performance
Concurrency and multi-threaded performance comes later.
Single core exhibits lots of ILP
GB of memory, access (esp latency) is not uniform!
Does this code benefit from
Respond at: https://PollEv.com/davidbindel252
or send davidbindel252 to 22333
GEMM: \(C = C + AB\) or
\[c_{ij} = \sum_{k=1}^n a_{ik} b_{kj}\]
Can make this blazingly fast! And would, in an HPC class.
P2: Mean Hamming distances in a dictionary
Algorithm notes:
Can change: dist code, comparison orders, data structures, …
Vec of Strings requires two indirections
dist is not currently tuned!We should forget about small efficiencies, say 97% of the time: premature optimization is the root of all evil.
… Yet we should not pass up our opportunities in that critical 3%.
- Knuth, Structured programming with go to statements, Computing Surveys (4), 1974.
If you think you might tune:
Want lots of work relative to data loads:
const parameterchunk iteratorAccounting for the string data and metadata, and assuming average string length 10, how big could \(B\) get before spilling out of a 32K L1 cache?
Respond at: https://PollEv.com/davidbindel252
or send davidbindel252 to 22333
Vec, HashMap, BTreeMap, Heap, BinaryHeapThings to look for:
Computational kernels are
Basic Linear Algebra Subroutines
Level 3 BLAS are key for high-perf transportable LA.
Building on kernel functionality is not perfect –
But: Ideally, someone else writes the kernel!
(Or it may be automatically tuned)
It is often a mistake to make a priori judgements about what parts of a program are really critical, since the universal experience of programmers who have been using measurement tools has been that their intuitive guesses fail.
- Knuth, Structured programming with go to statements, Computing Surveys (4), 1974.
Things to consider:
Basic picture:
Caveats: Effects from repetition, “characteristic” data
samplyMany tools available (see e.g. Rust performance book)
May time at function level or line-by-line
Fallback position: attribution of assembly!
[profile.release-prof]
inherits = "release"
debug = true
Rust notes:
debug = true in build profile for debugging symbolsrelease builds without debugCargo.toml for profiling profilecargo build --profile release-profllvm-mcaHardware context, some tricks, and some principles: