gxhash icon indicating copy to clipboard operation
gxhash copied to clipboard

Use aligned loads in get_partial_safe

Open bill-myers opened this issue 2 years ago • 3 comments

In get_partial_safe, it's possible to use aligned loads by declaring the buffer as MaybeUninit<State> and then casting the pointer for std::ptr::copy and zeroing the rest of the buffer, instead of declaring the buffer as an u8 array.

https://github.com/ogxd/gxhash/blob/b2b9d24eb35a48a2a18b1498f48693e523533200/src/gxhash/platform/x86_128.rs#L35-L39

bill-myers avatar Nov 17 '23 18:11 bill-myers

From what I can see there is a compiler optimization that stack allocates [0i8; VECTOR_SIZE] instead of heap allocating (probably because VECTOR_SIZE is a constant), so MaybeUninit<State> may not be faster.

ogxd avatar Nov 17 '23 20:11 ogxd

About to close this one unless someone has some snippet to propose?

ogxd avatar Dec 23 '23 20:12 ogxd

I tried using a struct which contains only the byte array and is marked as #[repr(align(16))]. I have not tested the performance yet, but this should still allocate on the stack and force 16-byte alignment.

notsatvrn avatar Jan 09 '24 15:01 notsatvrn

Closing this as proposed solution does not provides significant performance gains nor simplifies the code. Feel free to open another issue if you have something to suggest.

ogxd avatar Jun 05 '24 07:06 ogxd