The Same Function Is Cheap in Two Places and Ruinous in the Third, and the Flat List Shows One Number
Last timeIts Own Time or Everything Below
A profile is a tree flattened into a list, and flattening discards which path the time came from. Reading the tree finds the caller to fix instead of the function to blame.
What flattening discards
A flat profile has one line per function. That line sums every call of that
function, from everywhere in the program, into one number.
Most of the time this is fine. It fails in one specific and extremely common
situation: a function that is called from several places and behaves
differently depending on who called it.
| calls | microseconds each | total milliseconds | |
|---|---|---|---|
| called from render | 1200.0 | 2.0 | 2.4 |
| called from validate | 800.0 | 3.0 | 2.4 |
| called from the import l | 40.0 | 900.0 | 36.0 |
| the flat profile line | 2040.0 | 0.0 | 40.8 |
The failure is structural rather than a shortcoming of any particular tool. A
single number per function cannot represent a function whose cost depends on
its context, and plenty of functions do: a cache that hits for one caller and
misses for another, a parser given small inputs here and enormous ones there, a
retry that only ever fires on one path.
How stacks become a tree
The tree view exists because the raw data already contains the paths. Every
sample recorded a full stack, and merging identical stacks is all that happens.
The lesson stops here
5 more paragraphs to go
You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.
The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.
See the planThe contentsThis is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents