Billion row challenge: parsing temperatures fast(ish)

February 20, 2026 [Programming, Rust, Tech, Videos]

I watched Jon Gjengset taking on the one billion row challenge and got interested in one small corner of it: parsing temperatures, and in particular parsing temperatures in a "branchless" style - with no if statements - to help the CPU run the code as fast as possible. In the end, the results were surprising.

After the stream I added a version of Jon's parse_temperature to the comparison. His version assumes we already know where the semicolon is, so I had to add code to find it, and I didn't use the SIMD code he used to do that because it requires unstable Rust. With those caveats, my version was 4x faster(!). However, I'm pretty sure we're going to need to find that semicolon ourselves if we take on the full challenge, so this is not a fair comparison.

Also after the stream I realised I'm pretty sure there is a simple improvement I can make to branchy to make it even faster...

Follow me on mastodon: @andybalaam@mastodon.social

Comments

Tiger12506 2026-07-19

The assembly for branchy is pretty wild.

LBB5_3 label code at the end is always run. It checks for hyphen, sets up -1 (65536), and moves it into reg only if the compare says there was hyphen (cmovnew), and applies the negative sign (imul) before return (ret)

So the jumps above it are skipping over the blocks before the label.

mov ecx, byte ptr [rdi + rsi - 1] // last digit
byte ptr [rdi + rsi - 2]          // '.' character (missing because we do nothing)
mov eax, byte ptr [rdi + rsi - 3] // second digit
mov edx, byte ptr [rdi + rsi - 4] // first digit
byte ptr [rdi + rsi - 5]          // hyphen

add eax, eax
lea eax, [rax + 4*rax]

or (x + x) * 4 + x is a cheap way of multiplying by 10 (leveraging pointer offset calculation hardware of lea instr)

There will always be a digit after the decimal and before the decimal, and the compiler knows this, so by the time it sets up first digit (mov edx, byte ptr [rdi + rsi - 4]), it has already computed answer for last digit and second digit, and stored that result in eax.

That frees up ecx (cx) to become the sign variable, which gets initialized to -1 (mov cx, -1)

cmp dx, 45          // checks first digit char against hyphen (the match branch)
je .LBB5_3 
lea r8d, [rdx - 48] // r8 stores to numeric representation of first digit

cmp r8b, 10         // This is the match branch that checks if digit is 0 - 9 (done after!! converting to num)
jae .LBB5_3

jae = jump if above or equal, which is basically jge, just for unsigned (0xFF > 0x01)

And lastly of interest...

imul edx, edx, 100 // Multiply the character by hundred
add eax, edx       // Add to total
add eax, -4800     // convert char to number after the multiplication!!!

Interesting that the compiler moved the '0' - '9' check into the 0 - 9 domain to be able to turn the two-sided range check into a one-sided above 9 check.

Lots of instruction re-ordering by the compiler.

andybalaam 2026-07-20

Thank you for the amazing analysis Tiger12506! Fascinating.