5 Premature optimization vs optimizing hot-path: Decisions that impact code performance
This chapter covers
- When premature optimization is evil; when it is a sane choice
- Finding hot-path in your code using performance testing and measurements
- Optimizing the hot-path
There is an old computer science saying that Premature Optimization is the root of all evil. It has a solid background because it’s accurate for a lot of use cases. Without any input data about expected traffic and SLA, it’s hard to reason about your code and its required performance. Optimizing random paths in code in such a situation is like shooting in the dark. You will complicate your code without a sane reason.
However, there is a situation when at the design stage, we know quite much about the expected traffic that our system will need to handle. In such a scenario, we can design performance benchmarks that will reflect the production traffic. Once we are able to simulate the traffic, we can measure paths in our code and find out the hot-path. The hot-path is a part of your code that does most of the work and is executed for almost every user request. We will learn that the Pareto principle can be used to find and estimate where the hot-path is. Once we have detected the hot-path, we can start trying to optimize it.