Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align boost::uuid to 16 bytes #148

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

Lastique
Copy link
Member

This improves performance of operations on UUIDs as the UUID object now does not(*) cross page boundary. It may also improve code generation by compilers, which can now assume that loads and stores of UUIDs are not as expensive and therefore it may be more preferable to use vector instructions for that.

(*) It is technically possible that the boost::uuid object is still not properly aligned under some conditions. For example, on some 32-bit targets the stack is not aligned to 16 bytes, so placing the UUID object on the unaligned stack may result in unaligned UUID. For this reason, this commit does not change the SIMD intrinsics used in boost::uuid operations to their aligned equivalents. Using unaligned load/store instructions on actually aligned memory location on modern CPUs has the same performance as using aligned instructions, so the performance benefit is still there.

Closes #139.

@Lastique Lastique force-pushed the feature/alignas16 branch 3 times, most recently from a21d9bc to e8b1914 Compare April 23, 2024 19:40
@Lastique
Copy link
Member Author

I've updated the PR to only require alignment of 4 on 32-bit platforms. At least, on Windows, 32-bit processes only get 4-byte aligned stack.

@Lastique
Copy link
Member Author

@pdimov What's the purpose of test_alignment? Is it ok to just test that offsetof % alignof(uuid) == 0?

@pdimov
Copy link
Member

pdimov commented May 14, 2024

It's something that works today.

@Lastique
Copy link
Member Author

Do you mean that alignof(uuid) is guaranteed (i.e. documented) to be 1? I don't remember seeing this in the docs.

In any case, my question was what was the intended tested feature in this test. IOW, are you testing that alignof(uuid) is exactly 1 or are you testing that the compiler is able to align uuid in a struct by adding padding? Depending on what is it you're testing, the test could be updated differently, or the PR could be rejected altogether.

On 64-bit targets, this improves performance of operations on UUIDs
as the UUID object now does not cross page boundary. It may also
improve code generation by compilers, which can now assume that loads
and stores of UUIDs are not as expensive and therefore it may be more
preferable to use vector instructions for that.

On 32-bit targets, we cannot guarantee that boost::uuid is always aligned
to 16 bytes because stack and dynamic memory allocations typically have
lower default alignment (typically 4 or 8). For this reason, we only
require alignment of 4, and also do not change SIMD intrinsics used in
boost::uuid operations to their aligned equivalents. Using unaligned
load/store instructions on actually aligned memory location on modern
CPUs has the same performance as using aligned instructions, so the
performance benefit is still there on 64-bit targets.

Closes boostorg#139.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Align uuid to 16 bytes
3 participants