Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: jsBeeb and WebAssembly #150

Open
sassquad opened this issue Aug 10, 2017 · 8 comments
Open

Feature request: jsBeeb and WebAssembly #150

sassquad opened this issue Aug 10, 2017 · 8 comments

Comments

@sassquad
Copy link

Hi,

This WebAssembly lark looks interesting. Wondering whether jsBeeb can utilise wasm in the future. It would be an interesting diversion. In theory, JS is one of many options for compiling to WASM.

Your thoughts?

@mattgodbolt
Copy link
Owner

Heyas!

It can definitely use wasm. The original intention of jsbeeb was to show that emulation's possible using "normal" hand-written Javascript. I tried writing asm.js by hand but it's so tedious I gave up.

Most asm.js (and all wasm as far as I can tell) is generated by compiling C++ or other languages. This would mean a significant rewrite of jsbeeb, and with other emulators already written in C++ it seems like unless jsbeeb offers more over those emulators we should really just combine forces and work on a unified C++ codebase that supports compiling to wasm.

@sassquad
Copy link
Author

Thanks for your input @mattgodbolt .

I guess my suggestion came from this weird, and possibly terrifying thought - that websites won't need HTML, CSS and Javascript anymore. Instead, you'll get proprietary websites built entirely in WASM, using their language of choice.

Or this - a website built in MODE1 with PLOT codes and VDU23 character definitions instead of CSS, and BASIC PROCedures and FNs for back/front end code! 😧

@murkle
Copy link
Collaborator

murkle commented Jul 17, 2019

Interesting comment about wasm: https://news.ycombinator.com/item?id=20458624 and here's Chuckie Egg on an Acorn Atom in wasm!
https://floooh.github.io/tiny8bit/atom.html?file=atom/cchuck.tap&joystick=mmc&input=%2ALOAD%0A

FWIW: in my 8-bit emulators I'm closer to the 10% performance difference for code compiled in clang with -O3 (the native version uses latest Xcode's clang, the WASM the latest LLVM WASM backend via emscripten):
https://floooh.github.io/tiny8bit/cpc-ui.html

The time in the top-right is the time spent per frame in the emulator, in the WASM version that's somewhere around 2.5 to 2.8ms on my laptop.

The native version is somewhere between 2.0 and 2.2ms.

From my experiments, it's quite easy to accidentally hit a "slow path" on the WASM side, for instance registers are spilling earlier (AFAIK because WASM has one or two registers set aside). Things are getting better though. About a year ago, the same emulator code seemed to have triggered some specific slow-path and the WASM version was nearly 4x slower.

Of course it's always possible to put more time into optimizing the native version of a piece of code when targeting a specific compiler version, compiler-specific intrinsics and a specific CPU model. This is where WASM will always lag behind, with a portable ISA you'll always hit a wall where further optimization simply isn't possible.

So from my experience: somewhere between 10% slower and 3x slower is all within the "expected" range, the more important part is that performance is predictable for a given piece of code compiled with a given compiler version (e.g. you won't have the GC come in and do its thing for a few hundred milliseconds). And at least the emscripten team is very responsive and interested in such pathological cases.

@sassquad
Copy link
Author

Judging by this video from the 2019 Chrome Developers Summit, a lot of work has been done to support threads, yielding significant performance improvements.

@mattgodbolt
Copy link
Owner

Hey folks! I don't know that jsbeeb will ever do wasm! I have one experiment using asm.js (a similar but different technology) - #177.

WebAssembly isn't designed to be hand-written: it's made for being a target for compilation. So, like I said up above it's not really a goal for jsbeeb.

With respect to threads: I don't know how I could use threading to speed up jsbeeb: there's only one logical thread of execution: the CPU. Everything else is synchronized at the cycle level with that, so it's not like I can have one thread for video, one for CPU, one for sound etc -- they all would end up locked waiting for each other. I'm not saying it can't be done, but it's not a simple thing :)

@murkle
Copy link
Collaborator

murkle commented Dec 3, 2020

@mattgodbolt have you seen AssemblyScript? Maybe that's a way forward with some refactoring rather than a rewrite???

https://www.assemblyscript.org/
https://github.com/as2d/as2d

@mattgodbolt
Copy link
Owner

Ooh! thanks @murkle !! I'll take a look! :) yes!

@mattgodbolt
Copy link
Owner

on man this is a rabbit hole...one that looks far too fun to get in to :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants