savi-lang / savi

A fast language for programmers who are passionate about their craft.
BSD 3-Clause "New" or "Revised" License
155 stars 12 forks source link

Fix Bytes `read_native_*` methods to gracefully handle unaligned addresses. #485

Closed jemc closed 2 weeks ago

jemc commented 1 month ago

Casting a U8 pointer to a wider pointer may result in an unaligned address, which can apparently cause performance issues and/or signals on some platforms, and is generally thought to be bad practice.

We can avoid this by casting in the opposite direction instead (wider type to U8) and use memcpy, which will be well-optimized by LLVM and will also safely handle unaligned addresses appropriately.

This is apparently faster than the more obvious trick of getting the bytes one-by-one and bit-shifting them to combine them.