stephen-w-choo / ray-tracer-browser

3d renderer for the browser. A personal project to practice linear algebra, test-driven development and TypeScript.
0 stars 0 forks source link

Matrix multiplication slow #6

Open stephen-w-choo opened 9 months ago

stephen-w-choo commented 9 months ago

Rendering is slow. I expected it to be slow given that it's a ray tracer that compiles down to an interpreted language (JavaScript), but even so, it's not ideal. Even with Bun running the native TypeScript, it's slower than I'd like.

I think that main bottleneck is the matrix calculations.

I've initially implemented the Matrix type as a plain 2d array of numbers.

I can turn it into a 1d Float32 array for efficiency, since I'm only ever using a 4x4 array of numbers. Using 32-bit floats should be more than precise enough - I don't need the default 64-bit numbers of JS

stephen-w-choo commented 8 months ago

The Float32 array approach seems to have paid off - it's pretty much made everything 5-6 times faster on the benchmarks.

It's still... not fast, but I'm not convinced TypeScript can be much faster than this.

Options for further gains at this point are:

  1. Use Bun webworkers for concurrency?
  2. Target WASM through AssemblyScript - AssemblyScript unfortunately lacks some key features in WASM (concurrency being one of them), which will hinder performance
  3. The most extreme option - rewrite in a systems language to target WASM There are 4 languages which are considered to have first class WASM support - C, C++, Rust, Go (the usual systems languages. There's no way I'm writing it in C. Go would be the easiest to learn and use, but it doesn't have operator overloading. This leaves Rust and C++ as the two plausible options. Both are relatively unfamiliar languages to me and will take a while to learn.