Skip to content
Sean William Carroll
Go back

AI-Assisted Performance Optimization

I’ve been interested in profiling and optimization for a few years now. I’ve done it a few times in professional code, though I’ve not really had the opportunity to do so lately. For the past several weeks, I’ve been having fun with Claude by building a physics engine from (mostly) vanilla Rust. The engine is all Rust, and I’ve had Claude wrap things in TypeScript when appropriate so I could display some visual demos on this site. I’ve written 0% of the code, all of it coming from Claude (mostly Opus 5.5, and some Fable 5.2 when I had the credit), but I’ve been managing the process. I’ve pushed for particular features, including gravity and collisions, acoustics in the form of physically simulating piano strings (an homage to a fun project from university), and some interesting demos of swarming behavior. All along, I’d been stopping my agent to do health checks of the codebase when I could: mutation testing with cargo-mutants, benchmarking with criterion, property testing with proptest and so on, in addition to the normal unit testing and coverage collection. Recently, I finally took the time to pick a particular benchmark and see if I could improve things.

The following post describes what my agent and I did. I was primarily inspired by Casey Muratori’s lecture Simple Code, High Performance, which prompted me to examine the assembly of some hot loops to see if I could get them to vectorize better.

4,096 bodies under their own gravity: every pair, every step, at 60 Hz.

The problem in question is calculating the forces of gravity between nn discrete bodies.

Background

The force

Every body pulls on every other with Newton’s gravity, softened:

Fij=G mimj r(∣r∣2+ε2)3/2,r=xj−xi\mathbf F_{ij} = G\, m_i m_j \, \frac{\mathbf r}{\left(|\mathbf r|^2 + \varepsilon^2\right)^{3/2}}, \qquad \mathbf r = \mathbf x_j - \mathbf x_i

Per pair: a subtraction for r\mathbf r, a sum of squares, one square root, one division, and a few multiplies to scale r\mathbf r and add the result to each body. The square root and division are the expensive part.

The technique: SIMD

A CPU’s vector (SIMD) instructions do the same operation on several numbers at once: one fsqrt.4s takes four square roots. If four pairs’ worth of numbers sit side by side, four pairs cost about what one did.

Unless a section says otherwise, everything here ran on my laptop, an Apple M4, and the assembly is AArch64 (64-bit ARM) with its NEON vector instructions: 32 registers of 128 bits, v0 to v31. A suffix says how a register is split into lanes: .4s is four 32-bit floats (f32), .2s two, .2d two 64-bit floats (f64); a bare s5 or d5 is one scalar float, the lowest lane of v5. The listings use Apple’s assembler syntax, which puts the lane shape on the instruction (fsqrt.4s v20, v19) where ARM’s standard syntax puts it on the registers (fsqrt v20.4s, v19.4s). The benchmarks also include an AMD Zen 5 (x86-64) and WebAssembly, and say so.

The path we took

The short version, in order (on the M4 unless noted; every step and number is in the notebook):

The loop, before and after

The engine’s pairwise gravity before this work and as it is now, in three parts. The code is the engine’s, lightly edited (shorter comments, some lines wrapped, a slice or two written the short way).

The loop over bodies. Before, the bodies were copied into one array of (position, mass, inverse mass) and the loop went body by body, the pair loop nested inside. Now the copy is one array per quantity, and each body takes two passes: pass one over every later body, pass two adding up body ii‘s force.

Before

// Masses once per call: n divisions,
// not n² in the pair loop.
let bodies: Vec<(Vec2<S>, S, S)> = p
    .pos()
    .iter()
    .zip(p.inv_mass())
    .map(|(&x, &w)| {
        let m = if w == S::ZERO { S::ZERO }
                else { S::ONE / w };
        (x, m, w)
    })
    .collect();

// Each pair once: body i, then every
// later body j.
let mut acc_rest = acc;
let mut body_rest = bodies.as_slice();
while let (
    Some((a_i, acc_tail)),
    Some((&(x_i, m_i, w_i), body_tail)),
) = (
    mem::take(&mut acc_rest).split_first_mut(),
    body_rest.split_first(),
) {
    // the pair loop, adding into sum_i
    // (next)
    acc_rest = acc_tail;
    body_rest = body_tail;
}

After

// One array per quantity: x, y, mass,
// inverse mass (Bodies::of).
let n = b.x.len();
// Each pair's force on body i, for pass two.
let (mut fx, mut fy) =
    (vec![S::ZERO; n], vec![S::ZERO; n]);

let mut acc_rest = acc;
let (mut x_rest, mut y_rest, mut m_rest, mut w_rest) =
    (&b.x[..], &b.y[..], &b.m[..], &b.w[..]);
while let (
    Some((a_i, acc_tail)),
    Some((&px_i, x_tail)),
    Some((&py_i, y_tail)),
    Some((&m_i, m_tail)),
    Some((&w_i, w_tail)),
) = (
    mem::take(&mut acc_rest).split_first_mut(),
    x_rest.split_first(),
    y_rest.split_first(),
    m_rest.split_first(),
    w_rest.split_first(),
) {
    // pass one: every later body
    pass_one(
        (px_i, py_i, g_const * m_i, eps_sq),
        (x_tail, y_tail, m_tail, w_tail),
        acc_tail,
        (&mut fx, &mut fy),
    );
    // pass two: body i's sum, in 16 lanes
    let len = acc_tail.len();
    let sum_x = lane_sum(&fx[..len]);
    let sum_y = lane_sum(&fy[..len]);
    *a_i += Vec2::new(sum_x, sum_y) * w_i;
    acc_rest = acc_tail;
    (x_rest, y_rest, m_rest, w_rest) =
        (x_tail, y_tail, m_tail, w_tail);
}

The pair loop. Before, one pair at a time, body ii‘s sum added in order, and a branch for the 0/0 case. Now, pass one: every pair’s force for body ii with nothing summed across pairs (so the compiler can do four at once), the branch a select, each force kept in fx / fy.

Before

let mut sum_i = *a_i;
for (a_j, &(x_j, m_j, w_j)) in
    acc_tail.iter_mut().zip(body_tail)
{
    let r = x_j - x_i;
    let d_sq = r.length_sq() + softening_sq;
    if d_sq == S::ZERO {
        continue; // coincident: skip, not 0/0
    }
    let f = r * (g_const * m_i * m_j
        / (d_sq * d_sq.sqrt()));
    sum_i += f * w_i; // body i, in order
    *a_j -= f * w_j; // body j
}
*a_i = sum_i;

After: pass_one

let others = xs.iter().zip(ys).zip(ms).zip(ws);
let kept = fx.iter_mut().zip(fy.iter_mut());
for ((a_j, (((&px_j, &py_j), &m_j), &w_j)),
     (fx_j, fy_j)) in acc.iter_mut().zip(others).zip(kept)
{
    let (rx, ry) = (px_j - px_i, py_j - py_i);
    let d_sq = rx * rx + ry * ry + eps_sq;
    let s = g_mi * m_j / (d_sq * d_sq.sqrt());
    // coincident: 0/0, selected away (no branch)
    let s = if d_sq == S::ZERO { S::ZERO } else { s };
    let (f_x, f_y) = (rx * s, ry * s);
    a_j.x -= f_x * w_j; // body j
    a_j.y -= f_y * w_j;
    (*fx_j, *fy_j) = (f_x, f_y); // kept for body i
}

Body ii‘s sum. Before, one running total, one add per pair, in order: each add waits for the last. Now, 16 running totals (pair jj in lane j mod 16j \bmod 16), added together at the end as a tree, halves first. The order is written in the source, so every target, vector or not, gives the same bits.

Before

// In the pair loop, one add per pair:
sum_i += f * w_i;
// …and after it:
*a_i = sum_i;

After: lane_sum, lane_total

fn lane_sum<S: Scalar>(values: &[S]) -> S {
    let mut lanes = [S::ZERO; LANES]; // 16
    let (chunks, rest) = values.as_chunks::<LANES>();
    for chunk in chunks {
        for (lane, &v) in lanes.iter_mut().zip(chunk) {
            *lane += v;
        }
    }
    for (lane, &v) in lanes.iter_mut().zip(rest) {
        *lane += v;
    }
    lane_total(&lanes)
}

/// (l0 + l1) + (l2 + l3), … level by level.
fn lane_total<S: Scalar>(lanes: &[S; LANES]) -> S {
    let mut level = *lanes;
    let mut width = LANES;
    while width > 1 {
        width /= 2;
        let mut next = [S::ZERO; LANES];
        let (pairs, _) = level.as_chunks::<2>();
        for (sum, &[a, b]) in
            next.iter_mut().zip(pairs).take(width)
        {
            *sum = a + b;
        }
        level = next;
    }
    let [total, ..] = level;
    total
}

The pair loop in assembly

The pair loops as the compiler built them, f32 on an Apple M4 (AArch64). Before, one pair an iteration: the 2-wide vectors (.2s) hold only that pair’s xx and yy, the square root and division are scalar (fsqrt s16, fdiv s5), and fcmp / b.eq is a branch that skips two bodies exactly on top of each other with no softening (0/0). After, four pairs an iteration (.4s): four square roots and four divisions in one instruction each; the branch became a select (fcmeq and bic zero those pairs’ force); ld2 / st2 split and rejoin the bodies’ xx and yy; and each pair’s force is kept (str) for a second pass that adds body ii‘s sum in its 16 lanes.

Before: one pair an iteration

LBB193_57:
	cmp x1, x15
	b.eq LBB193_53
	mov x16, x15
	add x15, x15, #1
	ldr d4, [x11, x17]
	fsub.2s v4, v4, v1      ; r
	fmul.2s v5, v4, v4
	faddp.2s s5, v5         ; |r|²
	fadd s5, s9, s5         ; + ε²
	add x17, x17, #16
	fcmp s5, #0.0           ; 0/0?
	b.eq LBB193_57          ;   skip
	add x17, x12, x16, lsl #4
	ldp s7, s6, [x17, #8]
	fmul s7, s3, s7
	fsqrt s16, s5           ; 1 sqrt
	fmul s5, s5, s16
	fdiv s5, s7, s5         ; 1 div
	fmul.2s v4, v4, v5[0]   ; force
	fmul.2s v5, v4, v2[0]
	fadd.2s v0, v0, v5      ; i's sum
	fmul.2s v4, v4, v6[0]
	ldr d5, [x13, x16, lsl #3]
	fsub.2s v4, v5, v4
	str d4, [x13, x16, lsl #3] ; j's share
	b LBB193_56

After: four pairs an iteration

LBB95_8:
	ldr q7, [x17], #16      ; 4 x
	ldr q16, [x0], #16      ; 4 y
	ldr q17, [x1], #16      ; 4 masses
	ldr q18, [x3], #16      ; 4 1/masses
	fsub.4s v7, v7, v4      ; 4 r.x
	fsub.4s v16, v16, v5    ; 4 r.y
	fmul.4s v19, v7, v7
	fmul.4s v20, v16, v16
	fadd.4s v19, v19, v20   ; 4 |r|²
	fadd.4s v19, v6, v19    ; + ε²
	fmul.4s v17, v17, v2[0]
	fsqrt.4s v20, v19       ; 4 sqrts
	fmul.4s v20, v19, v20
	fdiv.4s v17, v17, v20   ; 4 divs
	fcmeq.4s v19, v19, #0.0 ; 0/0?
	bic.16b v17, v17, v19   ;   select
	fmul.4s v7, v7, v17     ; 4 forces
	fmul.4s v16, v16, v17
	fmul.4s v17, v18, v7
	ld2.4s { v19, v20 }, [x16]
	fsub.4s v21, v19, v17
	fmul.4s v17, v18, v16
	fsub.4s v22, v20, v17
	st2.4s { v21, v22 }, [x16], #32 ; j's
	str q7, [x4], #16       ; kept for
	str q16, [x5], #16      ;   i's sum
	subs x6, x6, #4
	b.ne LBB95_8

What changed in the source

Four things got the compiler from the left-hand loop to the right:

Benchmarks

4,096 bodies, one call of the gravity loop (8.4 million pairs), before and after, on an AMD Ryzen 7 9700X (Zen 5) and the M4. “Native” is a default build for the machine’s baseline instruction set; “AVX2” and “AVX-512” are native builds allowed to use those wider vectors. “wasmtime” and “V8” are the same Rust compiled to WebAssembly with simd128, run by wasmtime or by V8 (Chrome’s engine, in Node).

f32051015202530msZen 5, nativeZen 5, native, f32, before: 13.42 msZen 5, native, f32, after: 4.68 ms (−65%)13.42 ms4.68 ms −65%Zen 5, AVX2Zen 5, AVX2, f32, before: 13.16 msZen 5, AVX2, f32, after: 2.79 ms (−79%)13.16 ms2.79 ms −79%Zen 5, AVX-512Zen 5, AVX-512, f32, before: 13.14 msZen 5, AVX-512, f32, after: 1.58 ms (−88%)13.14 ms1.58 ms −88%Zen 5, wasmtimeZen 5, wasmtime, f32, before: 14.35 msZen 5, wasmtime, f32, after: 4.67 ms (−67%)14.35 ms4.67 ms −67%Zen 5, V8Zen 5, V8, f32, before: 22.23 msZen 5, V8, f32, after: 4.72 ms (−79%)22.23 ms4.72 ms −79%M4, nativeM4, native, f32, before: 8.77 msM4, native, f32, after: 4.28 ms (−51%)8.77 ms4.28 ms −51%M4, wasmtimeM4, wasmtime, f32, before: 10.52 msM4, wasmtime, f32, after: 4.43 ms (−58%)10.52 ms4.43 ms −58%M4, V8M4, V8, f32, before: 10.55 msM4, V8, f32, after: 4.33 ms (−59%)10.55 ms4.33 ms −59%f64051015202530msZen 5, nativeZen 5, native, f64, before: 18.74 msZen 5, native, f64, after: 10.88 ms (−42%)18.74 ms10.88 ms −42%Zen 5, AVX2Zen 5, AVX2, f64, before: 18.70 msZen 5, AVX2, f64, after: 6.67 ms (−64%)18.70 ms6.67 ms −64%Zen 5, AVX-512Zen 5, AVX-512, f64, before: 18.75 msZen 5, AVX-512, f64, after: 3.20 ms (−83%)18.75 ms3.20 ms −83%Zen 5, wasmtimeZen 5, wasmtime, f64, before: 18.72 msZen 5, wasmtime, f64, after: 10.92 ms (−42%)18.72 ms10.92 ms −42%Zen 5, V8Zen 5, V8, f64, before: 18.72 msZen 5, V8, f64, after: 10.88 ms (−42%)18.72 ms10.88 ms −42%M4, nativeM4, native, f64, before: 8.72 msM4, native, f64, after: 8.61 ms (−1%)8.72 ms8.61 ms −1%M4, wasmtimeM4, wasmtime, f64, before: 9.82 msM4, wasmtime, f64, after: 8.71 ms (−11%)9.82 ms8.71 ms −11%M4, V8M4, V8, f64, before: 10.65 msM4, V8, f64, after: 8.30 ms (−22%)10.65 ms8.30 ms −22%

Zen 5 gains far more than the M4: its scalar square root and division were the bottleneck, and vectors spread that cost over four lanes, where the M4’s are fast enough that in f64 there was little left to win. Wider vectors keep paying on Zen 5, down to a fifth of the time with AVX-512. And with simd128 on, WebAssembly runs within a few percent of native.

The engine, the experiments and every number here: physics (the notebook is docs/gravity-vectorize.md).


Share this post on:

Previous Post
Hello, particles