Using WebAssembly to simulate has two main benefits:
We can use exactly the same logic among the server and the browser
We can gain performance in CPU intensive simulation
The bundled Wasm file tend to be smaller than JS
The first point is important to keep the client synchronized with the server.
We can't always keep the server and the client synchronized, so the client needs to predict the simulation in its own.
Previously we did this by replicating the logic in TypeScript and Rust, but it is wasteful to implement it twice and it has better chance of bugs that logic may not work the same way.
For the second point, I measured the performance with the same condition.
Calculation with JavaScript (TypeScript):
Calculation with WebAssembly (Rust):
For WebAssembly, there is also another step to convert Wasm data to JavaScript (Three.js objects), but it seems that gain of performance by Wasm is greater than the overhead.
The drawback of wasm that I know of:
cargo deb doesn't work well with mixed targets in Rust workspace
Wasm's debug mode is very slow (much slower than TypeScript)
For the first point, the only solution right now is to delete orbiter-logic in root Cargo.toml.
Using WebAssembly to simulate has two main benefits:
The first point is important to keep the client synchronized with the server. We can't always keep the server and the client synchronized, so the client needs to predict the simulation in its own. Previously we did this by replicating the logic in TypeScript and Rust, but it is wasteful to implement it twice and it has better chance of bugs that logic may not work the same way.
For the second point, I measured the performance with the same condition.
Calculation with JavaScript (TypeScript):
Calculation with WebAssembly (Rust):
For WebAssembly, there is also another step to convert Wasm data to JavaScript (Three.js objects), but it seems that gain of performance by Wasm is greater than the overhead.
The drawback of wasm that I know of:
cargo deb
doesn't work well with mixed targets in Rust workspaceFor the first point, the only solution right now is to delete
orbiter-logic
in root Cargo.toml.