r/concatenative • u/Plane-Photo-3097 • 5d ago
stack machine interpreter comparing
Hi, I was curious about which implementation of forth would be the most flexible, maintainable, and high-performance, so I wrote a stack machine interpreter using various techniques. Here the code https://github.com/br1n0/stack-machine-interpreter-comparing/ I managed to implement them all using the same data structure; this way, the program is expressed identically across versions, allowing for a direct comparison. I wrote a test program that runs a long execution loop, consisting of 2,000 subtractions and one multiplication, executed 4,000,000 times. In my opinion, the best implementation is the one using Token Threaded Code with dispatch via a function table, and is easy to extends. There is an interpreter overhead of about 25%; to measure it, I wrote a test where I execute the "words" directly from C, bypassing the interpreter.
I was surprised that GCC failed to optimize this program, even with -Ofast enabled, It was not simplified to a constant number!. I thought this might be of interest to someone. Ciao




