When should inline be used in Rust?

One limitation of the current Rust compiler is that it if you’re not using LTO (Link-Time Optimization), it will never inline a function not marked #[inline] across crates. Rust uses a separate compilation model similar to C++ because LLVM’s LTO implementation doesn’t scale well to large projects. Therefore, small functions exposed to other crates need … Read more

Why is there a large performance impact when looping over an array with 240 or more elements?

Summary: below 240, LLVM fully unrolls the inner loop and that lets it notice it can optimize away the repeat loop, breaking your benchmark. You found a magic threshold above which LLVM stops performing certain optimizations. The threshold is 8 bytes * 240 = 1920 bytes (your array is an array of usizes, therefore the … Read more

Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?

Rust originally did enable LLVM’s noalias attribute, but this caused miscompiled code. When all supported LLVM versions no longer miscompile the code, it will be re-enabled. If you add -Zmutable-noalias=yes to the compiler options, you get the expected assembly: adds: mov eax, dword ptr [rsi] add eax, eax add dword ptr [rdi], eax ret Simply … Read more

Does the C++ standard allow for an uninitialized bool to crash a program?

Yes, ISO C++ allows (but doesn’t require) implementations to make this choice. But also note that ISO C++ allows a compiler to emit code that crashes on purpose (e.g. with an illegal instruction) if the program encounters UB, e.g. as a way to help you find errors. (Or because it’s a DeathStation 9000. Being strictly … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)