ntoh*/hton* is a bad API

42 points by 0x2ba22e11


fanf

I am similarly vexed by this common misdesign. See also Rob Pike’s old “byte order fallacy” article.

At least part of it is because this basic integer deserialization code should typically compile to an unaligned load followed by a byte swap instruction, so the low level translation kind of reinforces the bad tradition. Rob Pike’s suggested code ought to be wrapped up in a function, for convenience and safety; and since it will be in the hot path it might be better to write the function in a way that doesn’t rely on the compiler’s idiom detector, which pushes you towards using byteswap intrinsic. (Tho tbh compilers have been perfectly capable of doing that automatically for decades so the intrinsic seems unnecessary to me.)