↓ Skip to main content
  1. Rust/

Speeding Up the Speedable: SIMD and What GTA Has to Do With It

1910 words·9 mins· loading · loading · ·
How a single CPU instruction can crunch an entire batch of data at once, and why the infamous GTA Online loading bug is a great reminder of what bad parsing costs.

In this post I will walk through the basics of vector processing (SIMD), look at real-world examples like simdjson, and cover the ways you can work with SIMD in Rust. Fair warning: there is a bit of theory here, but it sets up the practical part nicely.

📦 Companion Code Repository: Runnable examples covering auto-vectorization, std::arch, wide, pulp, std::simd, and a benchmark are open-sourced on GitHub: github.com/eaglebk/rust-simd-examples.


What GTA Online Has to Do With It
#

Before diving into CPU instructions, let’s recall the story of an enthusiast named t0st who fixed a long-standing GTA Online bug in 2021 that kept players staring at loading screens for 5-10 minutes.

Rockstar’s mistake happened during JSON parsing. While reading a 10 MB JSON file, C’s sscanf kept calling strlen on every single step to recompute the remaining string length. A routine operation ended up burning CPU cycles on redundant work.

Rockstar confirmed the root cause and awarded the researcher a $10,000 bounty (you can read the full story on Ars Technica).

I bring this up for a reason: it shows how a poorly implemented low-level operation can bottleneck an entire application.


Speeding Up JSON: The simdjson Approach
#

When JSON parsing performance comes up, simdjson (by Daniel Lemire and Geoff Langdale) is hard to ignore.

At first glance, JSON looks like a poor fit for vectorization. Standard sequential parsing depends on preceding characters: you have to track quotes, \ escapes, and nested objects.

The simdjson authors split the work into two stages:

  1. Structural indexing with SIMD. Vector instructions scan large chunks of text in parallel, locate structural characters, find string boundaries, and handle escape sequences.
  2. Document parsing. The second stage builds the actual structure using the already indexed positions.

As a result, simdjson parses JSON at several gigabytes per second, outperforming many traditional parsers and even some binary format implementations.


What SIMD Is and How It Works
#

To see where that speedup comes from, let’s drop down to machine instructions.

A machine instruction is a basic command a CPU executes directly: add two numbers, compare values, or load data from memory into a register.

For a long time, most code relied on scalar operations that handle one value at a time, such as the classic ADD EAX, EBX instruction adding two 32-bit integers. Over time, CPUs gained instruction set extensions: SSE, AVX, AVX2, AVX-512 on x86, and NEON on ARM. These brought vector instructions to everyday processors.

SIMD stands for Single Instruction, Multiple Data.

With SIMD, the CPU operates on multiple values in parallel. Instead of comparing bytes one by one, it can compare 16, 32, or 64 bytes in a single vector instruction.

The exact element count depends on register width and element size:

  • 128-bit holds 16 bytes (or four 32-bit f32 floats);
  • 256-bit holds 32 bytes (or eight f32 floats);
  • 512-bit holds 64 bytes (or sixteen f32 floats).

How It Looks on the Hardware
#

The idea is straightforward: instead of loading a single number into a register, the CPU loads an entire vector. A 128-bit SSE register fits four f32 values, and one vector instruction adds all four pairs at once.

This is not four scalar additions bundled together as syntax sugar. It is one vector instruction running on dedicated vector registers and SIMD execution units inside a single core, with no extra threads or cores required.

How SIMD processes 4 f32 values in a single instruction
unsafe {
let va = _mm_loadu_ps(a.as_ptr());
let vb = _mm_loadu_ps(b.as_ptr());
let sum = _mm_add_ps(va, vb);
_mm_storeu_ps(out.as_mut_ptr(), sum);
}
Memory (RAM) 4 f32 elements (128-bit)
[0] [1] [2] [3]
Vector Registers XMM0 / XMM1 (128-bit)
empty
Core SIMD ALU 1 vector instruction
idle
_mm_loadu_ps loads 4 f32 values (16 bytes) from memory into the 128-bit XMM0 and XMM1 vector registers without requiring strict alignment.

Of course, SIMD pairs well with multithreading. Multithreading spreads work across multiple CPU cores, while SIMD speeds up uniform operations inside each core. They complement each other rather than competing.


Where to Look for SIMD Opportunities
#

Spotting vectorization candidates in existing code is fairly straightforward. Whenever you see loops like this (pseudocode):

for (byte in bytes) { ... }
for (item in array) { ... }
for (char in string) { ... }

it is worth taking a closer look. Loops where each iteration handles independent elements are prime targets: adding two arrays, applying a filter, or scanning byte sequences.

Conceptually, processing one element per step turns into processing a whole chunk:

for (8 elements in array) { ... }

Not every loop can be vectorized, though:

  • Loop-carried dependencies. If an iteration depends on the previous iteration’s result, vectorization may be impossible or counterproductive.
  • Small datasets. SIMD shines on hundreds of thousands or millions of elements. On tiny slices, setup overhead can wipe out any gain.
  • Memory bottlenecks. If the program spends most of its time waiting on RAM, faster arithmetic will barely move the needle.

Always verify SIMD changes with benchmarks (for example with criterion, which I covered in Crate of the Day: Criterion, and in the examples repository). Today, vector instructions are everywhere: image and audio processing, digital signal processing (DSP), matrix math, cryptography, and runtimes like Node.js and Bun (see the simdjson real-world usage list).


Working With SIMD in Rust
#

Since I mostly write Rust, let’s look at four ways to use SIMD in the language (though the same ideas apply to C/C++, Go, or Zig).

1. Auto-vectorization
#

The simplest path: we write a plain idiomatic Rust loop, and LLVM tries to turn it into SIMD instructions in release builds.

There is no guarantee it will happen, though. It depends on the target CPU, compiler flags, and whether the optimizer can prove there are no aliasing or dependency issues. You can check the generated assembly to see what LLVM actually emitted.

2. Direct Intrinsics (std::arch)
#

When you want full control, Rust’s std::arch module exposes SSE, AVX2, AVX-512, and ARM NEON intrinsics directly.

On x86_64, for instance, you can load four f32 values with _mm_loadu_ps and add them in one shot with _mm_add_ps. The u suffix stands for unaligned (loading without requiring strict vector boundary alignment; we will dig into alignment in the practical follow-up).

The upside is exact control over the instructions emitted into the binary. The downside is unsafe code, handling CPU architectures explicitly, and dealing with any leftover tail elements that do not fill a full vector.

Scalar loop (1 element per step)
fn add_scalar(a: &[f32; 4], b: &[f32; 4], out: &mut [f32; 4]) {
    for i in 0..4 {
        out[i] = a[i] + b[i];
    }
}
Hover or click a step to highlight matching lines:
Select or hover a step above to highlight matching lines in both columns.
Explicit SIMD (4 elements at once)
#[cfg(target_arch = "x86_64")]
unsafe fn add_sse(a: &[f32; 4], b: &[f32; 4], out: &mut [f32; 4]) {
    use std::arch::x86_64::{_mm_add_ps, _mm_loadu_ps, _mm_storeu_ps};
    let va = _mm_loadu_ps(a.as_ptr());
    let vb = _mm_loadu_ps(b.as_ptr());
    let sum = _mm_add_ps(va, vb);
    _mm_storeu_ps(out.as_mut_ptr(), sum);
}
Comparison: a regular loop adds one pair at a time, while SSE intrinsics load and sum four f32 values in one instruction

3. Portable SIMD (std::simd)
#

To avoid tying your code to a single CPU architecture, Rust is developing std::simd. Instead of _mm_add_ps, you work with a portable Simd::<f32, 4> vector type and a regular + operator, letting the compiler pick the right instructions for the target.

One catch: std::simd is still experimental and requires nightly Rust with #![feature(portable_simd)].

4. Ready-Made SIMD Crates
#

If you want to skip low-level intrinsics and unsafe on stable Rust, the ecosystem has solid crates:

  • wide - portable vector types with standard arithmetic;
  • pulp - cross-architecture SIMD abstraction with runtime dispatch;
  • faster - a crate for ergonomic iterator vectorization.

Try the different approaches in the interactive examples below:

Working With SIMD in Rust
Step 1/3
 1// Step 1: Auto-vectorization of a plain loop
 2fn add_slices(a: &[f32], b: &[f32], c: &mut [f32]) {
 3    let len = a.len().min(b.len()).min(c.len());
 4    for i in 0..len {
 5        c[i] = a[i] + b[i];
 6    }
 7}
 8
 9fn main() {
10    let a = [1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
11    let b = [10.0_f32, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0];
12    let mut c = [0.0_f32; 8];
13
14    add_slices(&a, &b, &mut c);
15    println!("Auto-vectorization result: {:?}", c);
16}
17

1. Auto-vectorization

  • The code looks like a regular scalar loop with no special APIs.
  • In --release builds, LLVM can replace the loop with vector instructions on its own.
  • Whether it actually vectorizes depends on the target CPU, compiler flags, and absence of loop-carried dependencies.

What’s Next
#

That wraps up the theory. In the next post we will move to practice: pick a real algorithm (such as parsing or a DSP filter), vectorize it in a few different ways, inspect the generated assembly, and compare real benchmarks.

Useful Links#

Article read
Please rate how helpful and clear this article was to you

Related