Accessing variables in Forth can be a little slow. Consider a write to a variable:
100 FRED !
Three distinct instructions, 8 bytes, and three passes through the Forth inner interpreter. It would be nice if we could make variables a little faster.
Here's something I came up with. It's very simple indeed. FVARs, or, Fast Variables.
When you define a Fast Variable, two new words are generated, providing faster access to the variable:
Functionally the same as FRED @ but only one pass through the inner interpreter. Compiles to two bytes.
Same as FRED ! but, again, only one pass through the inner interpreter. Compiles to two bytes.
So, the above example, which is 8 bytes and three passes through inner interpreter can be reduced to:
6 bytes, and two passes through NEXT. It's also 30% faster.
Let's look at a fetch:
4 bytes. Two passes through the interpreter. This reduces to:
2 bytes, one pass through NEXT. Also 30% faster.
Here's the code to implement Fast Variables. As written the code occupies 188 bytes and requires no buffers or variables. The "peeker" and "poker" words that it creates for you are machine code words. They're tiny and fast. Enjoy.
18th October 2016