4 ms·
> Using a usize often seems wasteful for indexing into small arrays and slices The opposite is often the case, as the architecture may not support offsetting a
by ATsch 4y ago
> Using a usize often seems wasteful for indexing into small arrays and slices
The opposite is often the case, as the architecture may not support offsetting a pointer by those sizes, requiring lots of casting in tight loops. Making that explicit makes the programmer aware of this.
This is one major reason that C compiler developers want all overflow to be undefined behavior, as it allows them to upgrade for loops that use smaller index types to larger ones.
- staticassertion 4y agoThat seems easy to optimize out, especially given rust's mutability guarantees. If I have a u8 and I index into an array in a loop the compiler should be free to cast it to a usize above that loop, right? The benefit being you can store much smaller structures, plus in the case of memory unsafety you reduce attacker control. The vast majority of strings I personally use are certainly well under 2^32 bytes, and most are probably under 2^8.