ContentsThe library

What Makes Code Fast

Most of the Time Is Going Somewhere That Does Not Appear Anywhere in Your Source

Last timeOne Instruction, Many Values

Allocation, following pointers, and the bookkeeping the language performs on your behalf routinely account for most of the run time, and none of it is visible in the code you wrote.

What allocation costs

The lessons so far have treated the program as arithmetic and memory accesses.

Real programs spend a great deal of their time on neither, and the work is

invisible in the source, which is why it is so often missed.

Start with allocation. Asking for a small object means finding a free block of

the right size, updating the structures that track what is free, and returning

a pointer. Releasing it means putting the block back, possibly merging it with

neighbours, and updating those structures again. A good allocator does this in

a few tens of cycles each way.

FIG 1Costs with nothing corresponding to them in the source
cycles, roughlyvisible in the source
allocating one small obj400
releasing it again300
following a pointer alre40
following a pointer not 2500
a call through a functio150
a bounds check on an acc20
updating a reference cou80
The second column is zero in every row, which is the point of the figure. Nothing in the source text says allocate forty cycles here. The two marked rows are the expensive ones and they are also the two most likely to happen once per element in a loop that looks like it is doing almost nothing.

Put that against what the object is used for. A loop that creates a small

temporary each iteration, does three arithmetic operations with it, and

discards it, spends roughly seventy cycles on the temporary and three on the

work. The profile will show the time inside the allocator, which is correct and

often misread as the allocator being slow, when the real finding is that it is

being called far too often.

FIG 2What the temporaries cost
total cycles spent on object lifetime
objects created during the operation
cycles to allocate one
cycles to release one
Worth computing early because the result is often larger than everything else being measured. A request that allocates two hundred small objects, which is unremarkable in code built from small pieces, spends fourteen thousand cycles on their creation and destruction before any of them has been used for anything.

The remedies are all about the count rather than the cost per object. Reuse a

buffer across iterations instead of allocating one each time. Allocate a block

of objects together rather than individually. Keep the object on the stack when

its lifetime allows. Each of these reduces n, which is the only term you

control cheaply.

The lesson stops here

4 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. 01Two Loops Doing Exactly the Same Arithmetic, One of Them Fifty Times Slower
  2. 02A Hundred Additions in the Time of Four, Unless Each One Waits for the Lastopening only
  3. 03Sorting the Array First Makes the Loop Six Times Faster Without Changing What It Computesopening only
  4. 04Compute Both Answers and Throw One Away, Which Is Faster Than Deciding Which to Computeopening only
  5. 05One Number Tells You Which Half of the Toolbox to Openopening only
  6. 06The Same Add, Eight Numbers at a Time, If You Can Convince the Compiler It Is Safeopening only
  7. 07Most of the Time Is Going Somewhere That Does Not Appear Anywhere in Your Sourceyou are here
  8. 08The Fastest Possible Version of a Tenth of Your Program Buys You Eleven Per Centopening only

Read alongside