11 Optimizations

 

This chapter covers

  • Understanding Rust’s zero-cost abstractions
  • Using vectors effectively
  • Programming with SIMD in Rust
  • Parallelization with Rayon
  • Using Rust to accelerate other languages

In this final chapter, we’ll discuss optimization strategies with Rust. Rust’s zero-cost abstractions allow you to confidently write Rust code without thinking too hard about performance. Rust delegates much of its machine code generation to LLVM, which has mature, robust, widely deployed, and well-tested code optimization. Code written in Rust will be fast and well optimized without you spending time hand-tuning code.

There are, however, certain cases in which you may need to dive deeper, which we’ll discuss, along with the tools you’ll need, in this chapter. We’ll also discuss how you can use Rust to accelerate code from other languages, which is a fun way to introduce Rust into codebases without needing to perform a complete rewrite.

11.1 Zero-cost abstractions

An important feature of Rust is its zero-cost abstractions. In short, Rust’s abstractions allow you to write high-level code, which produces optimized machine code with no additional runtime overhead. Rust’s compiler takes care of figuring out how to get from high-level Rust to low-level machine code in the optimal way, without the overhead. You can safely use Rust’s abstractions without needing to worry about whether they will create a performance trap.

11.2 Vectors

11.2.1 Vector memory allocation

11.2.2 Vector iterators

11.2.3 Fast copies with Vec and slices

11.3 SIMD

11.4 Parallelization with Rayon

11.5 Using Rust to accelerate other languages

11.6 Where to go from here

Summary

sitemap