scratchcpp / libscratchcpp

C++ library for building Scratch project players
Apache License 2.0
22 stars 8 forks source link

Don't use computed goto in the VM #461

Closed adazem009 closed 3 weeks ago

adazem009 commented 8 months ago

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

adazem009 commented 8 months ago

@Arcnor What do you think about this?

Arcnor commented 8 months ago

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 commented 8 months ago

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

adazem009 commented 8 months ago

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.

Arcnor commented 8 months ago

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.

adazem009 commented 8 months ago

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.

Arcnor commented 8 months ago

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.

adazem009 commented 3 weeks ago

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