Hasty Briefsbeta

Bilingual

Threaded Code

a year ago
  • #Performance Optimization
  • #Virtual Machine
  • #Threaded Code
  • Threaded code is a technique for implementing virtual machine interpreters, originally described by James R. Bell in 1973.
  • Subroutine-threaded code involves sequences of code addresses representing virtual machine instructions, but it's not interpretive in the original sense.
  • Direct threaded code uses an instruction pointer to execute sequences of instructions, with each instruction jumping to the next via a shared or individual NEXT routine.
  • Indirect threaded code introduces a level of indirection with code and parameter fields, allowing shared code for similar instructions but with separate data.
  • Switch threading uses a switch statement to implement threaded code, which can lead to high misprediction rates in modern CPUs' branch target buffers.
  • Call threading represents each virtual machine instruction as a function, using global variables for registers, which may not be as efficient as local variables in switch threading.
  • Performance varies among threading techniques, with direct and indirect threading generally offering better performance than switch or call threading due to branch prediction efficiencies.
  • Implementing threaded code portably can involve using GNU C extensions for machine-independent engines or generating code automatically from stack effect notations.
  • Historical context includes early uses of code fields and the evolution of threading techniques from string interpretation to modern implementations.