Giter Site home page Giter Site logo

stitch's Introduction

Stitch

Stitch is an experimental Wasm interpreter written in Rust that is designed to be very fast and lightweight.

Caution

If you're planning to use Stitch for your project, you should consider the following carefully:

Stitch achieves its speed by relying on sibling call optimisation (sibling calls are a restricted form of tail calls in which the callee has the same signature as the caller). Rust doesn’t provide a mechanism to guarantee that tail calls are optimised, but in practice, LLVM automatically optimises sibling calls on 64-bit platforms, provided certain constraints are met.

Note that LLVM does not strictly guarantee to optimise sibling calls either, so in theory it is possible for this feature to regress, which would cause Stitch to stop working. In practice, I consider this unlikely, as such a regression would have a significant negative impact on the performance of existing code. However, the possibility cannot be ruled out entirely.

We could add a fallback mode to Stitch that relies on a trampoline instead of tail calls. This would allow Stitch to continue working in the face of LLVM regressions, but would also negate most of its speed benefits, and in any case this fallback mode has not been implemented yet.

Performance

Stitch is very fast. I've compared it against several other engines, shown in the table here below:

Name Description
Wasmi The only other Wasm interpreter written in Rust that I know.
Wasm3 The fastest Wasm interpreter written in C that I know.
Wasmtime A JIT compiler for Wasm.

Coremark Results

The following table shows the Coremark scores for Stitch and the other engines I've compared it against on the three major 64-bit platforms:

Engine Mac Linux Windows
Stitch 2950 1684 4592
Wasm3 2911 1734 3951
Wasmi 788 645 1574
Wasmtime 12807 13724 34796

The following table shows the CPU I've used for each platform:

Platform CPU
Mac Apple M2 Pro @ 3.5GHz
Linux Intel Xeon E312xx @ 2.1GHz
Windows Intel i9-13900K @ 3.0GHz

As you can see, Stitch is several times faster than Wasmi on all major platforms. Compared to Wasm3, it is slightly faster on Mac, slightly slower on Linux, and significantly faster on Windows.

Stitch is much slower than Wasmtime (about 4x on Mac, about 8x on Linux and Windows), but that is to be expected, given that Wasmtime is a JIT compiler, whereas Stitch is an interpreter.

The reason Stitch is faster than Wasmi is that Stitch uses threaded code, whereas Wasmi uses an interpreter loop.

The reason Stitch is slightly faster than Wasm3 on Mac, but slightly slower on Linux, is likely because Stitch has more variants per instruction compared to Wasm3, which puts pressure on the instruction cache. I suspect this gives Stitch the edge on the Apple M2 Pro, with its large instruction cache, but Wasm3 the edge on the Intel Xeon E312xx, with its smaller instruciton cache.

The reason Stitch is significantly faster than Wasm3 on Windows is likely because Stitch uses the System V calling convention for its threaded code, while Wasm3 uses the vectorcall calling convention, which has fewer registers available to pass integer arguments.

The above results were obtained by going to the coremark directory and running:

cargo run --release

This will run the coremark-minimal.wasm testsuite and print the results for each of the engines above.

Micro Benchmarks

To get a better idea of how fast Stitch is, I've also ran it against several micro benchmarks. The following table shows the results for each of these benchmarks on Mac (using an Apple M2 Pro @ 3.5GHz):

Name Time
fac_iter 1_048_576 2.8561 ms
fac_rec 32 276.82 ns
fib_iter 1_048_576 5.0592 ms
fib_rec 32 52.136 ms
fill 0 1_048_576 3.9256 ms
sum 0 1_048_576 4.4489 ms

The above results were obtained by running:

cargo bench

Compile time

Stitch is very lightweight. It has 0 non-dev dependencies, and consists of roughly 15.000 lines of code.

The following table shows how fast Stitch compiles on Mac compared to other Wasm engines written in Rust (using an Apple M2 Pro @ 3.5GHz):

Engine Compile Time
Stitch 2.29s
Wasmi 15.61s
Wasmtime 81.02s

As you can see, Stitch compiles much faster than either Wasmi or Wasmtime. If compile time is important to you, Stitch might be a good choice for you.

Features

Stitch supports the following finished Wasm proposals:

Proposal Status
Import/Export of Mutable Globals
Non-trapping float-to-int conversions
Sign-extension operators
Multi-value
JavaScript BigInt to WebAssembly i64 integration
Reference Types
Bulk memory operations
Fixed-width SIMD
WASI

Portability

Stitch compiles and passes the Wasm core test suite on all the three major 64-bit platforms (Mac, Linux, and Windows).

Stitch currently does not run on 32-bit platforms. The reason for this is that I have not yet found a way to get LLVM to perform sibling call optimisation on these platforms (ideas welcome).

If you need broader portability than this, either Wasmi or Wasm3 might be a better choice for you.

Security

Stitch has a safe API, but uses a significant amount of unsafe code under the hood. This unsafe code has not been audited in any way, so I cannot give any strong guarantees about safety. If you need strong guarantees about safety, Wasmi might be a better choice for you.

That said, I’ve made a serious effort to ensure that all unsafe code in Stitch is sound, and conforms to the stacked borrows model. Unfortunately, it's currently not possible to run Stitch in Miri, since Miri is an interpreter, and therefore doesn't optimize sibling calls.

Usage

As a CLI Application

To install the CLI:

cargo install makepad-stitch

To run a Wasm binary:

makepad-stitch <file_name> <func_name> [<arg>]*

As a Rust Library

To learn how to use Stitch as a Rust library, please refer to the Stitch crate docs.

Design

TODO

stitch's People

Contributors

ejpbruel2 avatar

Stargazers

Paulo Henrique Silva avatar Giovanni Petrantoni avatar Alf-André Walla avatar harlan avatar Yuri avatar Paul avatar Jakub Hlusička avatar Marcus André avatar Victor Hugo Barros avatar Ryuta Suzuki avatar Maximilian Teegen avatar Tarun Ramakrishna Elankath avatar Patrick Reisert avatar Hrvoje Bandov avatar swlkr avatar Gregory Petrosyan avatar Shirish Nigam avatar Hiroki Noda avatar  avatar  avatar Patrick Smith avatar Paul Saunders avatar  avatar Anurag Hazra avatar Serg Alex avatar  avatar Yuan Zhang avatar ZJPzjp avatar MK avatar Alex avatar Shota Minami avatar Andrejs Agejevs avatar Marco Carmosino avatar Toby Lawrence avatar Jonathan Beri avatar Théo OIRY avatar  avatar Sam Johnson avatar Joey Ezechiëls avatar Tim Kersey avatar Graydon Hoare avatar marco gazzuolo avatar Oliver Gerlach avatar  avatar Jan Riemer avatar  avatar Kan-Ru Chen avatar Marenda SA avatar Ken Friis Larsen avatar Ribbon avatar  avatar Xiaobo avatar Robin Freyler avatar SatyrDiamond avatar Sean Jensen-Grey avatar Neodon avatar Tom Hemmes avatar hoemoon avatar Jackson Ming Hu avatar  avatar Roberto Acevedo avatar Amit Upadhyay avatar Elaina avatar Dan Todor avatar quininer avatar coderock avatar  avatar Lup Yuen Lee avatar Alexey Shekhirin avatar Andrew Dirksen avatar ismakubex avatar Pearce Keesling avatar dai avatar Yavor Kolev avatar Chris avatar Sam Morrison avatar Nikolay Kolev avatar zach avatar Steve Manuel avatar Syrus Akbary avatar Yerkebulan Tulibergenov avatar Oleksandr Bordun avatar Denis Cavalli avatar tjallingt avatar 夏 avatar mario avatar Daniel Mokeev avatar Nikolai Golub avatar Valentin Berlier avatar Chen Tao avatar 邻二氮杂菲 avatar Marc Espin avatar  avatar  avatar Edward Tan avatar

Watchers

Robin Freyler avatar  avatar

stitch's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.