WebGL-Orbiter icon indicating copy to clipboard operation
WebGL-Orbiter copied to clipboard

Use WebAssembly to simulate universe state

Open msakuta opened this issue 2 years ago • 0 comments

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):

image

Calculation with WebAssembly (Rust):

image

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.

image

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.

msakuta avatar Apr 03 '22 19:04 msakuta