libscratchcpp icon indicating copy to clipboard operation
libscratchcpp copied to clipboard

Don't use computed goto in the VM

Open adazem009 opened this issue 1 year ago • 7 comments

src/engine/virtualmachine_p.cpp Compilers will turn the switch statement into computed goto automatically.

adazem009 avatar Jan 29 '24 21:01 adazem009

@Arcnor What do you think about this?

adazem009 avatar Jan 29 '24 21:01 adazem009

You mean, removing the computed goto and relying on the compiler?

From what I've seen, the compiler might do something like that, but it's not controllable nor guaranteed, so maybe you want to keep the existing behavior until tests are done? After my changes, testing if this is getting done by the various compilers should be easy.

Arcnor avatar Jan 29 '24 21:01 Arcnor

That said cleaner code should probably prevail, given this VM probably doesn't need the highest performance anyway...

Arcnor avatar Jan 29 '24 21:01 Arcnor

That said cleaner code should probably prevail, given this VM probably doesn't need the highest performance anyway...

Actually, the VM needs high performance. I've been even thinking about implementing JIT compilation.

I'll of course do benchmarks and tests first to be sure the compiler uses computed goto, that's why I've given this issue the lowest priority.

adazem009 avatar Jan 30 '24 05:01 adazem009

Actually, the VM needs high performance. I've been even thinking about implementing JIT compilation.

Isn't this project meant to be used with Scratch, the learning platform? For those kind of projects I think even a normal interpreted loop will suffice, unless you have some special targets in mind or something... Writing a JIT is possible, but a project on its own (like LuaJit), specially if doing it in general instead of using something like LLVM's ORC, but I'd be interested in seeing your ideas and progress there.

Arcnor avatar Jan 30 '24 16:01 Arcnor

Actually, the VM needs high performance. I've been even thinking about implementing JIT compilation.

Isn't this project meant to be used with Scratch, the learning platform? For those kind of projects I think even a normal interpreted loop will suffice, unless you have some special targets in mind or something... Writing a JIT is possible, but a project on its own (like LuaJit), specially if doing it in general instead of using something like LLVM's ORC, but I'd be interested in seeing your ideas and progress there.

There are many cases where performance is needed, for example in 3D projects. Check these out, for example: https://turbowarp.org/868045282 https://turbowarp.org/869264071

TurboWarp is a Scratch mod that compiles Scratch scripts to JavaScript. These projects would be much slower (or not run at all) if you tried them in the original Scratch VM. See https://docs.turbowarp.org/how

I don't think I'll ever be able to beat TurboWarp with this project, but I believe it's possible with JIT compilation.

adazem009 avatar Jan 30 '24 17:01 adazem009

I see, so very special projects, that's definitely not what I think of when I think about Scratch 😄 . Very cool nonetheless!

And for sure, beating V8 is probably very difficult if not impossible, although your surface area might be much smaller, so YMMV. Let me know when you open the issue to work on that, I'd be interested in following, and maybe even contribute if time permits.

Arcnor avatar Jan 30 '24 17:01 Arcnor

Closing because the bytecode VM is going to be removed (#574).

adazem009 avatar Sep 09 '24 10:09 adazem009