5.8 Performance Considerations

Rust numeric types offer different trade-offs:

  • i32/u32: Often optimal on most 32-bit and 64-bit CPUs, so Rust defaults to i32.
  • i64/u64: Generally efficient on 64-bit architectures, potentially heavier on 32-bit.
  • i128/u128: Not natively supported on most CPUs; the compiler typically generates multiple instructions to emulate 128-bit arithmetic, so performance can be noticeably slower than for 64-bit integers.
  • f64: Usually faster than f32 on modern hardware, thanks to widespread support for double-precision math instructions.
  • Smaller types (i8, i16): May save space in large arrays or in embedded contexts, but can require more frequent overflow checks or upcasts internally.

For operations over large datasets (for example, iterating through big arrays), using smaller numeric types may improve cache efficiency, but you should weigh that against potential overflow risks and the overhead of additional conversions. Rust can also leverage SIMD instructions and concurrency in a safe manner, making careful attention to data alignment, cache usage, and minimal type conversions beneficial for performance.