Skip to content

Use WebAssembly to simulate universe state #43

@msakuta

Description

@msakuta

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions