Content deleted Content added
Pseudocode is unnecessarily confusing with multiple ++/-- operations on the same variable in an expression. |
|||
Line 226:
===Token threading===
Token-threaded code
A common approach, historically, is [[bytecode]], which typically uses 8-bit opcodes
<syntaxhighlight lang="c">
start:
vpc = &thread
dispatch:
// Any inter-instruction operations are performed here (e.g. updating global state, event processing, etc)
addr = table[i]▼
jump
CODE_PTR decode(BYTE_CODE **p) {
// In a more complex encoding, there may be multiple tables to choose between or control/mode flags
}
thread: /* Contains bytecode, not machine addresses. Hence it is more compact. */
1 /*pushA*/
Line 247 ⟶ 251:
pushA:
*sp++ = A
jump
pushB:
*sp++ = B
jump
add:
addend1 = *--sp
addend2 = *--sp
*sp++ = addend1 + addend2
jump
</syntaxhighlight>
Line 262 ⟶ 266:
For instructions where the individual operations are simple, such as "push" and "add", the [[Computational overhead|overhead]] involved in deciding what to execute is larger than the cost of actually executing it, so such interpreters are often much slower than machine code. However, for more complex ("compound") instructions, the overhead percentage is proportionally less significant.
Counter-intuitively, there are times when token-threaded code can sometimes run faster than the equivalent machine code when that machine code ends up being too large to fit in the physical CPU's L1 instruction cache. The higher [[code density]] of threaded code, especially token-threaded code, can allow it to fit entirely in the L1 cache when it otherwise would not have thereby avoiding cache thrashing. However, it should be noted that threaded code consumes both instruction cache (for the implementation of each operation) as well as data cache (for the bytecode and tables) unlike machine code which only consumes instruction cache; this means threaded code will eat into the budget for the amount of data that can be held for processing by the CPU at any given time. In any case, if the problem being computed involves applying a large number of operations to a small amount of data then using threaded code may be an ideal optimization.
<ref name="heller"/>
|