I have recently been learning The Rust programming language and I absolutely love it! Rust is a systems-level language (like C and C++) which provides compile-time guarantees about memory safety and safe concurrent behaviour. Readers of my Cybersecurity and technology blogs will know that I am interested in computer security, and we’re reminded on an almost monthly basis at the moment that computer security often leaves a lot to be desired, what with vulnerabilities and exploits of public data sadly being a frequent occurrence. Rust programs simply will not compile when the program contains code which often causes these problems, meaning that bad code can be caught during development rather than later when there is more at stake. This is definitely the way we need to go, so I’d recommend anyone with an interest in programming or security to give it a try!
As well as Rust, I have also been investigating WebAssembly, which is a new way of deploying compiled programs to the web (and other) platforms. WebAssembly is a new, low-level, binary format which can be used as a compiler target, meaning that compiled languages can be compiled to it and then run in a web browser. There are already some amazing demonstrations of this available, such as the Epic Zen Garden, which uses the Unreal 4 engine compiled to WebAssembly!
The good news is that Rust can already compile to WebAssembly! So, I strongly believe that a combination of Rust and WebAssembly will be a powerful driving force for the web in the years to come.
My goal for this demonstration is to provide a convenient way of analysing the relative performance of the three modules in different browsers on different platforms. I will keep this demonstration updated as support for WebAssembly improves in order to ensure that all of the latest features are used to provide the best performance.
The WebAssembly module is written in Rust and compiled using Rust’s own wasm32-unknown-unknown target, which is currently only available in the nightly branch. I also hope to include a module built using the wasm32-unknown-emscripten target to see if performance varies between the two.
Finally, the WebGL module uses a GPU shader to perform the rendering. This is by far the fastest of the three modules which is to be expected as the GPU is made for tasks such as this. It’s useful to compare the performance of the other two modules with this however so that the performance difference between the best case can be observed.
As mentioned, WebAssembly is brand new. Browser developers have done a great job of integrating the specification, but this is just the first step. As with most new technologies, the first step is to get things working; optimising the implementation usually follows once support has stabilised. I strongly suspect that the implementations at present are un-optimised and that performance will continue to improve with each new browser version.
Foreign function call costs
Uint8ClampedArray. Again, I expect these overheads to be reduced as support is improved in browsers.
Feel free to run these tests yourself, and please let me know if you have any interesting results!
I plan to update this demonstration project whenever a new development occurs so that it can always be used as an up-to-date way to benchmark WebAssembly. I will also update the project to include different modules for WebAssembly compiled with different methods, such a compiled from C code.
The code for the Rust implementation is available on GitHub so feel free to take a look and let me know if my implementation could be improved.