Skip to content

Commit

Permalink
Align boost::uuid to 16 bytes on 64-bit targets, to 4 on 32-bit.
Browse files Browse the repository at this point in the history
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 #139.
  • Loading branch information
Lastique committed May 14, 2024
1 parent 2848e33 commit 9dcdf15
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions include/boost/uuid/uuid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ struct uuid

// data

// Align uuid objects to 16 bytes on 64-bit platforms for performance.
// On most 32-bit platforms, default stack and dynamic memory alignment is lower than 16
// (typically 4 or 8), which makes it problematic to require uuid alignment of 16.
#if (defined(__SIZEOF_POINTER__) && (__SIZEOF_POINTER__ >= 8)) || \
defined(__x86_64__) /* This covers x86-x32 */ || \
defined(_M_AMD64) || defined(_M_ARM64)
BOOST_ALIGNMENT(16)
#else
BOOST_ALIGNMENT(4)
#endif
std::uint8_t data[ 16 ];

public:
Expand Down
4 changes: 2 additions & 2 deletions test/test_alignment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ struct X2

int main()
{
BOOST_TEST_EQ( offsetof(X1, b), 1 );
BOOST_TEST_EQ( offsetof(X2, b), 2 );
BOOST_TEST_EQ( offsetof(X1, b) % alignof(uuid), 0 );
BOOST_TEST_EQ( offsetof(X2, b) % alignof(uuid), 0 );

return boost::report_errors();
}

0 comments on commit 9dcdf15

Please sign in to comment.