Section 18.5.1

Amdahl’s law for an interpreter optimization claim

Definition

Amdahl’s law is a formula for the overall speedup §18.1.13 of a task when only one part of the task is made faster. Speedup is the old time divided by the new time. Let pp be the fraction of the original time spent in the part that is improved, and let ss be how many times faster that part becomes. The whole task then becomes 1/((1−p)+p/s)1 / ((1 - p) + p / s) times faster. The unimproved fraction 1−p1 - p caps the overall speedup at 1/(1−p)1 / (1 - p), however large ss is.

The idea

A detector is a small program that examines each event a system reports and decides whether the event breaks a rule. The detector is stored as bytecode §2.4, a compact instruction format for a virtual machine §2.3 (VM): a program that carries out such instructions instead of the processor running them directly. An interpreter §2.13, the simplest way to build a VM, fetches each bytecode instruction, decodes it and then carries it out. Compiling the detector to native code, the processor’s own machine instructions, removes the fetching and decoding. Suppose the compiled instructions run four times faster than the interpreted ones. The tempting conclusion is that every call that runs the detector becomes four times faster.

The conclusion fails because a call does more than run the detector’s instructions. The system first builds the context, the record of event data that the detector reads; the detector calls helper functions §2.19, routines of the host program that are native code already; and the system then acts on the verdict. Compilation speeds up none of that work, so the gain for the whole call is limited by the share of time that the detector’s own instructions took.

A trip is a fair picture: 2 hours of driving to and from airports plus 8 hours of flying. A plane four times faster cuts the trip to 2+8/4=42 + 8 / 4 = 4 hours, a speedup of 10/4=2.510 / 4 = 2.5, not 4. Even an instant flight leaves the 2 hours of driving, so no plane makes the trip more than 10/2=510 / 2 = 5 times faster. The picture stops being accurate where the parts interact. In a computer, speeding up one part can change the time of the others, for example by changing what the processor’s caches hold.

How it works

The calculation needs measured times, not counts of instructions or lines of code.

  1. Fix the endpoints: the event that starts the measured interval and the event that ends it, for example the entry into the callback §14.22, the function the system calls for each event, and the return from it.

  2. Split the baseline time TT of that interval into the time TaT_a of the part the change accelerates and the time TuT_u of everything else, so that T=Tu+TaT = T_u + T_a. Measure both on the same workload, meaning the same inputs.

  3. Compute p=Ta/Tp = T_a / T.

  4. Measure ss, the speedup of the accelerated part alone: its old time divided by its new time.

  5. Measure any extra time aa that the change adds to every call.

  6. The new time is T′=Tu+Ta/s+aT' = T_u + T_a / s + a, and the overall speedup is T/T′T / T'.

One call of a detector that a VM runs has the following parts, and compilation accelerates only the instruction execution.

Gene Amdahl made this argument in words in 1967, about computers with many processors. There the improved part is the work that can be spread over NN processors, so at best s=Ns = N. The formula is the standard later statement of his argument; the 1967 paper itself contains no equation.

Worked example

The numbers are invented. One call of the callback takes T=10T = 10 μ\mus (microseconds) when the detector is interpreted. A timing breakdown splits the call into three segments:

Check: 1.0+8.6+0.4=10.01.0 + 8.6 + 0.4 = 10.0 μ\mus.

Compilation accelerates only the 8.0 μ\mus of instruction execution, so p=8.0/10=0.8p = 8.0 / 10 = 0.8. Let the compiled instructions run s=4s = 4 times faster, taking 8.0/4=2.08.0 / 4 = 2.0 μ\mus. The VM segment becomes 2.0+0.6=2.62.0 + 0.6 = 2.6 μ\mus, not zero. The whole call becomes 1.0+2.6+0.4=4.01.0 + 2.6 + 0.4 = 4.0 μ\mus, and the overall speedup is 10/4.0=2.510 / 4.0 = 2.5. The formula gives the same result: 1/((1−0.8)+0.8/4)=1/(0.2+0.2)=1/0.4=2.51 / ((1 - 0.8) + 0.8 / 4) = 1 / (0.2 + 0.2) = 1 / 0.4 = 2.5.

The cap. If the compiled instructions took no time at all, the call would still take 1.0+0.6+0.4=2.01.0 + 0.6 + 0.4 = 2.0 μ\mus. The speedup can therefore never exceed 10/2.0=510 / 2.0 = 5, which equals 1/(1−0.8)1 / (1 - 0.8).

Added costs. If the compiled version adds 1.0 μ\mus of new work to every call, the call takes 4.0+1.0=5.04.0 + 1.0 = 5.0 μ\mus, and the speedup falls to 10/5.0=210 / 5.0 = 2. Compiling also has a one-time cost. Let a just-in-time compiler §2.14, which translates bytecode to native code while the system runs, translate the detector once, when it is loaded. If that takes 300 μ\mus and nothing is added per call, each call saves 10−4.0=6.010 - 4.0 = 6.0 μ\mus, and compiling pays for itself after 300/6.0=50300 / 6.0 = 50 calls. A detector that runs fewer than 50 times before it is replaced costs more time compiled than interpreted.

Stated precisely

Let TT be the baseline time of one measured interval, written as T=Tu+TaT = T_u + T_a, where TaT_a is the time of the part a change accelerates and TuT_u is the time of everything else. Let p=Ta/Tp = T_a / T, let s>0s > 0 be the speedup of the accelerated part (its old time divided by its new time), and let a≥0a \ge 0 be the extra time the change adds to the interval. The new time T′T' and the overall speedup SS are T′=Tu+Tas+a,S=TT′=1(1−p)+p/s+a/T.T' = T_u + \frac{T_a}{s} + a, \qquad S = \frac{T}{T'} = \frac{1}{(1 - p) + p / s + a / T}. With a=0a = 0 this is Amdahl’s law. For p<1p < 1, the overall speedup SS never exceeds 1/(1−p)1 / (1 - p), whatever ss is.

The formula is exact when three assumptions hold.

  1. Additivity: the parts run one after another and do not overlap, so the time of the interval is the sum of the times of its parts.

  2. Unchanged remainder: the change leaves TuT_u as it was. It does not alter cache contents, memory layout or scheduling in a way that changes the other parts.

  3. Same workload and endpoints: TaT_a, TuT_u and ss are measured with the same inputs and the same start and end events as the claim they support.

Amdahl’s law keeps the amount of work fixed and asks how much sooner it finishes. Gustafson’s law (1988) [408] asks instead how much more work a parallel computer finishes in the same time when processors are added.

The equation holds for each single call, and therefore for mean times, because the mean of a sum equals the sum of the means. It does not hold for medians §11.13: the median of a sum is in general not the sum of the medians of its parts. A breakdown that stacks the median time of each segment therefore need not add up to the median time of the whole call.

Limits and common mistakes

Amdahl’s law is bookkeeping, not a prediction. It is exact only while its three assumptions hold, and a real change can alter the parts the formula treats as fixed. Compiled code, for example, occupies memory of its own and can change what the processor’s caches hold for the other parts.

The fraction pp is a fraction of time on one workload, not a fraction of code. A detector that makes up 90% of the code may take 30% of the time. A detector that rejects most events after a few instructions has a small pp and gains little from compilation. A speedup measured on one workload does not carry over to another.

A figure that shows only the detector’s own time can show ss, the speedup of the accelerated part. It cannot by itself show SS, the speedup of the whole call, because it leaves out TuT_u.

A faster call does not mean more events handled per second. When another stage, such as a radio link, limits that rate, a faster callback leaves it unchanged; the computational versus communication bottleneck §14.37 entry treats that case.

Questions answered

Does compiling a detector to native code remove the entire VM segment?

No, compiling a detector to native code does not remove the entire VM segment. The VM (virtual machine) segment of a timing breakdown is the time from entering the VM to leaving it. Compiling removes only the overhead of the interpreter, the part of the VM that carries out the detector’s bytecode, its stored instructions, one at a time: the work of fetching, decoding and dispatching each instruction. That makes the detector’s own operations cheaper. The segment still contains the compiled instructions, which take time; the bodies of helper functions, the host program’s routines that the detector calls, which were native code already; every safety check that was not proved unnecessary in advance; and the work of entering and leaving the detector. Work outside the segment, such as building the detector’s input and acting on its verdict, does not change at all.

Amdahl’s law gives the size of the gain: if the part that compiling speeds up took a fraction pp of the original time and becomes ss times faster, the whole call becomes 1/((1−p)+p/s)1 / ((1 - p) + p / s) times faster. In an invented example, a 10 μ\mus (microsecond) call has an 8.6 μ\mus VM segment holding 8.0 μ\mus of interpreted instructions and 0.6 μ\mus of helper bodies. With s=4s = 4, the VM segment shrinks to 8.0/4+0.6=2.68.0 / 4 + 0.6 = 2.6 μ\mus, not zero, and the call to 4.0 μ\mus, a speedup of 2.5.

Related entries: Median (§11.13), Computational versus communication bottleneck (§14.37), Virtual machine (VM) (§2.3), Interpreter (§2.13), Just-in-time compilation (JIT) (§2.14), Contrast, normalization, ratio, and percent overhead (§18.1.13), The cost of interpreter versus JIT execution (§17.6). Further reading: [184, 408].