But when Nat is a part of a struct, doesn’t need it to be boxed independently on the size of the number (to have a fixed offset for following fields of the struct)?
@timo, ah yes, Motoko does indeed unbox small Nat/Int32/64 as well, my mistake.
@ZhenyaUsenko, I doubt that checked Nat32 ops are notably faster than Nat ops within the 31 bit value range. Did you measure that?
Of course, unchecked (i.e., wrapping) ones are somewhat faster, but since undetected integer overflow is one of the most dominant source of serious bugs in systems (and has enabled costly exploits of smart contracts), I would not recommend using them, except in very specific situations or if somebody actually proved the code correct.
@qwertytrewq, fields in a struct or slots in an array are always 4 bytes wide, regardless of type or value (so that generics can be type-agnostic). Like elsewhere, Nat/Int values are only boxed when they don’t fit that, i.e., they exceed the 31 bit range (with the remaining bit used to distinguish the two cases).
Not much. But looping through array with a Nat32 counter and converting the counter to Nat only to access array index is faster than having a Nat counter and doing no conversions even for checked Nat32 ops - the difference increases for wrapping operators.