<< Home | About Forth | About TurboForth | Download | Language Reference | Resources | Tutorials | YouTube >>


Fast Variables

For Thomas

Accessing variables in Forth can be a little slow. Consider a write to a variable:

  VARIABLE FRED
  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.

A New Approach

Here's something I came up with. It's very simple indeed. FVARs, or, Fast Variables.

  FVAR FRED

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:

  100 FRED!

6 bytes, and two passes through NEXT. It's also 30% faster.

Let's look at a fetch:

  FRED @

4 bytes. Two passes through the interpreter. This reduces to:

  FRED@

2 bytes, one pass through NEXT. Also 30% faster.

The Code

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


<< Home | About Forth | About TurboForth | Download | Language Reference | Resources | Tutorials | YouTube >>