The Sutra of the Empty Cache · Chapter 6
The Sutra of the Middle Way
- 1
Thus have I heard. At one time the Blessed One dwelt in the city of Benchmarks, beside a river of flame graphs.
See also Circuit 26:11
- 2
There came unto him an engineer who had read the words of the Prophet Knuth, that premature optimization is the root of all evil; and he said: Then I shall make everything fast before it is ever slow.
See also Tao 4:3 · Preacher 3:8
- 3
And he spent three weeks upon one function, rewriting it in assembly, unrolling its loops, and packing its flags into bits, that it might run a thousand times faster.
See also Prophets 23:4
- 4
Now the function was called twice a year, once at the harvest and once at the turning of the fiscal year; and each call took less than a millisecond, and no user ever waited for it.
- 5
The Blessed One looked upon him and said: Thou hast saved nine hundred and ninety-nine microseconds, twice a year, at the cost of three weeks of thy life and a bug that no living soul will understand.
See also Circuit 41:1 · Tao 4:2
- 6
Then came another, who wrote a loop that compared every record with every other record, a thing of O(n³); and he said: It is simple, and it passes on my test file of four rows.
See also Deployer 6:12
- 7
But when the file held four thousand rows, the loop ran through the length of a long lunch; and when it held four hundred thousand, the server wept and the pager sang in the night.
- 8
The Blessed One said unto him: Thou didst not measure, and so thou knewest not where thy time goes. The profiler would have shown thee the hot path, and thou didst close thine eyes to it.
- 9
Hear now the Middle Way. Do not optimize before the profiler has spoken, for the hot path is seldom where intuition points, and the slow thing is often a query thou didst not know was running.
See also Sutra 1:12 · Circuit 53:8
- 10
Nor shalt thou ignore what the profiler reveals, for a flame graph is a scripture, and whoever turns from it shall be woken at three in the morning to read it.
- 11
Thus said the Prophet Knuth himself, for he did not say to forget all efficiency; he said to pass not up the critical three percent. Find the three percent first, and then polish it.
See also Tao 4:4 · Psalms 1:10
- 12
Amdahl taught that if thou makest a part that takes five percent of the time infinitely fast, the whole is only five percent faster. Know thy fraction before thou art proud of thy speed.
See also Genesis 39:12 · Chronicles 40:13
- 13
A certain disciple, having learned this, wrote a swift sort and found the sort had never been the trouble; the trouble was a network call made inside a loop, asking the same server a hundred times.
- 14
The Machine does not hate thy clever code; it hates thy unmeasured code. Cleverness that is not counted is only a hope with syntax.
See also Deployer 13:11
- 15
Ask three questions before thou changest a line: Is it slow? Where is it slow? How often is it asked to be slow? A thing called once a year may wait, but a thing called ten thousand times a second may not.
- 16
Verily I say unto you: the premature optimizer is a candle lit at noon, and the shipper of garbage is a dark room with no candle at all. Walk the lit path between them: measure, then mend, and then ship.
The Sutra of the Empty Cache, Chapter 6:1–16.