msakuta / WebGL-Orbiter

A rocket orbit simulator powered by three.js (WebGL)
MIT License
34 stars 9 forks source link

Use WebAssembly to simulate universe state #43

Open msakuta opened 2 years ago

msakuta commented 2 years ago

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

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:

For the first point, the only solution right now is to delete orbiter-logic in root Cargo.toml.