Optimization
About optimization
Goal
The general goals of system optimization is to improve end-user experience.
It is in fact pretty loose and would have included the product feature.
But in narrow sense, we often care about the two basic aspects:
- reduce latency.
- reducing computation cost.
The object of optimization is, of course, the entire software stack, from the application on the top, and all the way down to the bare metal (including system software, OS kernel, and the hardware).
Analysis
We can often start analysis from two complementary views:
- the analysis of the workload.
- the analysis of the resources.
Observability
TODO: what are the observability tools? read more
- counters, statistics, metrics.
- profiling.
- tracing.
I would often opt to a customized instrumentation (mostly static) for the code (after that I got the main overview of the program working), and then make it a customized tracing tool which would provide me data so that analysis and reasoning of my hypothesis can be performed.
Experimentation
TODO: elaborate more on this direction.
apply experiment: applying a synthetic workload to the system and measure its performance.
- macro experimentation (end to end)
- micro experimentation: focused on a particular component.
branch misprediction
Modern CPUs use branch prediction to guess the outcome of conditional branches, before the condition is fully resolved, as it can do pipeline to optimize instruction sequence. This allows the CPU to keep fetching and executing instructions instead of waiting.
A branch misprediction typically costs about 10–20 CPU cycles.
| Situation | Approximate added latency |
|---|---|
| Correctly predicted branch | ~0–1 cycles of effective overhead |
| Mispredicted branch, modern desktop CPU | ~10–20 cycles |
| Especially deep pipeline | ~20–30 cycles |
| Small/simple embedded CPU | ~3–10 cycles |
One of the most effective ways to reduce mispredictions is to simply avoid branches in programming
Memory and Data Accessing
Now this is a memory access related issue so might consult memory.md.