Integer NaN in Rust
Today I want to talk about one cool little thing about Rust that I learned recently.
Rust has a widely used
with great language
and using it prevents a whole class of errors commonly encountered in C++. As
a bare metal, cycle-counting aficionado I see these cool features and can’t
help but wonder “Sure, but what does it cost?”
It turns out Rust also implements a “null pointer optimization” which allows it
to represent an
Option of pointer types exactly like you would in C++–as a
simple pointer, but with the benefit that the programmer is forced to check it
properly. It doesn’t even stop there! Other types with “invalid” encodings
can be optimized down to smaller representations, too.
So, how about a common use case for systems using limited communication bandwidth: integer values with a not-a-number canary? It turns out we can get the same optimization to fire!