3 ms·
What do you mean by "thread your switch statement"?
by skybrian 8y ago
What do you mean by "thread your switch statement"?
- anisppp 8y agohttps://en.wikipedia.org/wiki/Threaded_code https://en.wikipedia.org/wiki/Threaded_code
- smuser 8y agoI think he means have a different thread for each case or a subset of cases instead of having any explicit switch statement.
- ridiculous_fish 8y agoThe "big switch statement" approach is for each bytecode instruction to complete by jumping to a centralized dispatch location (i.e. the switch statement). The "threaded" approach is for each bytecode instruction to complete by decoding and jumping to the handler for the next instruction. Basically instead of "break" you have `goto handlers[nextIp->opcode].` The advantages of threading are fewer jumps and better branch prediction (since branch prediction is tied to IP). The disadvantages are slightly larger code and compilers struggle to optimize it, since the control flow is not structured.
- filereaper 8y agoThis method of design is called a Continuation Passing Style Interpreter. [1] Here's a production version from OpenJ9's JVM ByteCode Interpreter. [2] [1] https://kseo.github.io/posts/2017-01-09-continuation-passing-style-interpreter.html https://kseo.github.io/posts/2017-01-09-continuation-passing... [2] https://github.com/eclipse/openj9/blob/01be53f659a8190959c1641eac20b0488739e94c/runtime/vm/BytecodeInterpreter.hpp#L4872 https://github.com/eclipse/openj9/blob/01be53f659a8190959c16...