There is a conditionally compiled section inside snappy elements that selects dereferencing the reinterpret_cast'ed pointer as the best implementation for reading and writing potentially unaligned 16, 32, and 64-bit integers on architectures that are known to support such operations (e.g. x86 ) A reserve for other architectures is to use a memcpy implementation .
My understanding is that the reinterpret_cast implementation demonstrates undefined behavior, and clang undefined default sanitizer does this.
What puzzles me: why not just use a memcpy based implementation? I would expect that all but the most broken compilers would use intrinsics to implement these memcpy calls, since the size is known at compile time. In fact, I would expect identical code from both implementations on any modern toolchain.
However, I also admit that instant was written by people who know what they are talking about. Therefore, it remains for me to wonder if there is any other advantage in using the reinterpret_cast mechanism, which outweighs its undefined behavior. Don't want performance to depend on compiler implementation quality? Something else I did not consider?
source
share