Should I use small datatypes when I know my numbers are small?

A) The primary reason to go for small-size items is storage efficiency. That only matters when you have a lot of them, or when they pack with other data into a struct that is significantly more cache-efficient than when the larger-size items are used. Be aware that on some architectures access to sub-word-size fields, particularly writes, may be less efficient than when you use an item sized to the CPU data path.

B) If your data is used for indices into slices, your code will be cleaner if you size the index data by usize or isize. So unless A) applies, for indices choose usize or isize.

C) For other items, if you size your items to the CPU data-path width, it will have maximal performance and minimal code size. Other sizes may encode less compactly in the instruction stream, or may entail hardware slowdowns in some cases. In particular, size data used with atomic operations to the CPU data-path width.

6 Likes