Перейти к содержимому

Rust and Go Both Got 6x Faster Float Sums, in Opposite Ways

Alchemical Abyss

0:00 / 0:00

Rust and Go Both Got 6x Faster Float Sums, in Opposite Ways

3 просмотра · 10 ч назад
Alchemical Abyss
5 подписчиков
3 просмотра · 10 ч назад
Floating-point addition rounds after every step, so (0.1 + 0.2) + 0.3 and 0.1 + (0.2 + 0.3) are two different numbers. That is why a compiler has to add a list of floats in exactly the order you wrote, one number at a time, with every addition waiting for the one before it. In 2026 both Rust and Go got an official way round that. Rust 1.98.0 (20 August 2026) stabilised five "algebraic" float methods, algebraic_add and its siblings: no compiler flag, no unsafe, you change the one operation and the compiler may regroup the sum. Go 1.27 (19 August 2026) carries an experiment, switched on with GOEXPERIMENT=simd, with a package of vector types: nothing is reordered for you, you load the registers and add them yourself. This video measures both on the same 100,000 numbers on one laptop. In Rust, one method call made the loop 6.6 times faster, and the machine code shows why: four registers holding two numbers each instead of one. In Go, one vector register doubled the speed and four registers written by hand reached 7.2 times; four plain running totals, with no experiment at all, already gave 3.7 times. Then the price: the fast sums differ from the plain one in the last digits (here they are closer to the exact total), the Rust result changes between an optimised and an unoptimised build, and the Go result changes with the width of the machine's vector registers. Fine for statistics, graphics and simulation; not for money or anything that has to match bit for bit. The older tool, the C fast-math flag, is shown removing a check for NaN. The code is three small files: Rust, Go and C. Sources: Rust Blog, 20 Aug 2026: https://blog.rust-lang.org/2026/08/20... Go Blog, 24 Sep 2026: https://go.dev/blog/simd-experiment Go Blog, 2 Oct 2026: https://go.dev/blog/archsimd The channel runs a falling-sand world live 24/7:    / @alchemicalabyss   Chapters 0:00 The sum your compiler may not speed up 0:17 Why: (0.1 + 0.2) + 0.3 is not 0.1 + (0.2 + 0.3) 0:39 The plain loop: one addition at a time 0:54 20 August 2026: Rust 1.98 and algebraic_add 1:17 One method call, 6.6 times faster 1:27 The machine code, before and after 1:50 The answer changes in the last digits 2:10 Go 1.27: vectors by hand, as an experiment 2:36 Every measurement on one laptop 2:58 The price: the answer depends on the build 3:18 When to use it, the C flag, and my take #rust #golang #programming