ContentsThe library

Reading a Profile Honestly

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.

FIG 1One function, three callers
callsmicroseconds eachtotal milliseconds
called from render1200.02.02.4
called from validate800.03.02.4
called from the import l40.0900.036.0
the flat profile line2040.00.040.8
The bottom row is what a flat profile prints: one entry, two thousand calls, forty milliseconds. The third row is the actual finding, and it is invisible in the bottom row because averaging a nine hundred microsecond call with two thousand two microsecond calls produces a figure that describes neither. The import loop is passing data of a shape that makes this function slow, and no amount of staring at the merged line will reveal that.

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 contents

This 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

The rest of this course

  1. 01It Is Slow Is Not a Measurement, and Profiling Before You Have One Wastes the Week
  2. 02One Profiler Interrupts a Thousand Times a Second and Guesses, the Other Watches Every Call and Changes the Answeropening only
  3. 03The Function at Number Seven Received Nine Samples, Which Means You Know Almost Nothing About Itopening only
  4. 04The Function Holding Ninety Per Cent of the Time Does Nothing, and the One Doing the Work Holds Four Per Centopening only
  5. 05The Same Function Is Cheap in Two Places and Ruinous in the Third, and the Flat List Shows One Numberyou are here
  6. 06Three and a Half of the Four Seconds Are Invisible, Because the Profiler Only Looks When the Program Is Runningopening only
  7. 07The Sample Landed Three Instructions After the One That Was Slow, and the Function It Blames Does Not Exist Any Moreopening only
  8. 08The New Version Is Eight Per Cent Faster, and So Is the Old One If You Run It Enough Timesopening only

Read alongside