[std] mem.readInt is confusing and hostile to optimization #638
Labels
breaking
Implementing this issue could cause existing code to no longer compile or have different behavior.
standard library
This issue involves writing Zig code for the standard library.
Milestone
The loop bounds are determined from the runtime-known
bytes.len
instead of the comptime-known@sizeOf(T)
. This prevents/discourages the compiler from unrolling the loop. For native endian reads, mem.readInt should optimize to a single (unaligned) load instruction, but this seems unlikely with the current API.Why is there an API for reading an int from too few bytes? When would you want that? This was surprising to me when I read what
readInt
would do.Proposal: Move the current API to something that indicates how weird it is, like
mem.readPartialInt
. I've already pushed optimizable implementations ofreadIntLE
andreadIntBE
which you can use if you know at comptime which one you want. Add amem.readIntWithEndian
that takes a runtime-known endian and calls one ofreadIntLE
orreadIntBE
. The long name is to discourage you from calling it unless you really only know the endian at runtime, which seems like a rare usecase.The text was updated successfully, but these errors were encountered: