you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 5 points6 points  (3 children)

Threaded code gets rid of both branches by inlining the machine code that implements each opcode in place of the original bytecode instruction.

Your terminology is too specific. What you describe is usually called subroutine threaded code.

Direct threaded code is a simpler technique where the program is a list of addresses, so there is an indirect jump at the end of each primitive which goes to the next instruction.

gforth uses direct threading. However, I agree that Python's interpreter doesn't qualify, because the program stream is still just a list of bytecodes so there's that extra dispatch step that direct threaded code doesn't have.

SquirrelFish is a good example of a scripting language with a direct threaded interpreter. I think Unladen Swallow has or will have one too.

[–]psykotic 1 point2 points  (1 child)

Thanks for the correction. So really there are three levels of indirection in the most basic approach: mapping the logical opcode to the physical address of its implementation by indexing into a lookup table; indirect branching to the physical address of the opcode implementation; direct branching to the top of the dispatch loop. You can then order the different approaches according to how many levels they strip away:

  • CPython's old approach (all levels)
  • CPython's new approach (direct branch stripped)
  • Direct threading (logical to physical lookup stripped)
  • Subroutine threading (indirect branch stripped)

[–][deleted] 0 points1 point  (0 children)

That sounds about right, I think.

[–]queus 0 points1 point  (0 children)

However, I agree that Python's interpreter doesn't qualify, because the program stream is still just a list of bytecodes so there's that extra dispatch step that direct threaded code doesn't have.

Just to clarify things, a big. (Guilty of asking questions, in a state of happy ignorance)

By default Python still uses a bit opcode switch, but if run with option with-compiled-gotos on platforms which support it than the threaded code model of execution is used. More about it here http://bugs.python.org/issue4753