JavaScript: From Closures to Autonomous Systems

Capítulo 7. Speculative Execution

Part II. A Machine That Learns to Run Code8 min · en inglés

07

Speculative Execution

In 1992 Urs Hölzle, Craig Chambers and David Ungar were solving a problem that had nothing to do with speed. By then the Self compiler had learned to optimize programs so well that almost nothing of them remained in the machine code. Functions dissolved inside other functions, variables lived in registers or vanished altogether, and when a programmer stopped the program in a debugger, there was nothing to show them: the program they had written simply did not exist in memory. Giving up the optimizations was a pity, and without them the debugger worked like a television with the cable pulled out.

The authors of Self figured out how to unfold optimized code back on demand. At points chosen in advance, the compiler left a note saying where each variable of the source program currently lived. When the debugger stopped at such a point, the system used the note to rebuild the function’s frame in the form the programmer expected and continued execution in unoptimized code. Only the function being looked at in the debugger was unfolded, and everything else kept running at full speed. The paper was called «Debugging Optimized Code with Dynamic Deoptimization», and the word «deoptimization» that we saw in V8’s logs in the first chapters comes from there.

Fifteen years later this emergency exit, invented for a debugger, became the main engine of speed. If the engine can at any moment drop optimized code and return to code that is guaranteed to behave according to the specification, then it can optimize for the likely case even when nothing is guaranteed. Without an emergency exit a compiler has to account for every case, even those that will never happen. With one, it can take as many risks as it likes, as long as there is a check at every turn.


Take a function that adds up the numbers in an array:

function sum(arr) {
  let s = 0;
  for (let i = 0; i < arr.length; i++) s += arr[i];
  return s;
}

While the function runs in the interpreter, the interpreter records at every operation what passed through it. V8 keeps a separate table for this for every function, with a line for every addition, every field read and every call. Suppose sum has received only arrays of small integers. V8 stores such numbers right inside the reference, without a separate object in memory, and they fit into 31 bits. When the function is handed to the smart compiler, the compiler reads the table and reasons like a gambler with statistics. Since the array has always held small integers, they can be added with a single integer processor instruction, without checking whether this is a number at all, whether it is fractional, or whether it is a string. All that remains is to put in two small checks: that the array still consists of small integers and that the sum has not overflowed 31 bits. Each such check is a fork where one road leads on through fast code and the other to the exit, to deoptimization.

Gratis, solo falta un paso

El resto del libro es gratis

Deja tu correo y te mando un enlace para confirmarlo. Después del clic, todos los capítulos se abren aquí, en este dispositivo, durante un año. Cuando salga un libro nuevo, escribiré a esa dirección; responde “basta” a cualquier correo y no vuelvo a escribir.

¿Tienes cuenta INITE? Entra y el libro se abre sin correo.

No se cobra nada por el libro. Si quieres apoyarlo, puedes donar desde cualquier capítulo.

Cómo se guarda la dirección