From 46s to 5s -- Optimizing a 350 Line Raytracer in Rust

Click for: original source

An interesting article by Carl Fredrik Samson about his journey with Rust. He started with porting some code from a familiar language, C# or C++ in this case. But if you just port the code you will probably miss some of the features that makes Rust special.

In this post he talks about how to port a short raytracer written in C#/C++ codebase to Rust, then applying some simple optimizations by leveraging some features in Rust.

Author then explains each line of code and how it was ported from C# to Rust (or as close as he could get). The observation?:

Rust is pretty similar to the optimized C# when we run the code ported line by line as similar as possible, but the memory usage is less than 14 of C#.

Then he describes:

  • Optimizing part 1. Use iterators
  • Optimizing part 2. Buffer the results if you can
  • Optimizing part 3. Using Rayon
  • Bonus: Fixing the huge increase in memory usage.

Rayon is a paralleization library for Rust, and gives us superpowers if you already have a piece of code that uses an iterator to produce a result.

Read more to learn how they slashed down render time to one ninth of original! Good read for anybody interested in systems programming languages like Rust.

[Read More]

Tags programming software performance