-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Fix unaligned load and stores: (#4528) #4531
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,16 +21,18 @@ | |
#define RIPPLE_BASICS_SLABALLOCATOR_H_INCLUDED | ||
|
||
#include <ripple/beast/type_name.h> | ||
|
||
#include <boost/align.hpp> | ||
#include <boost/container/static_vector.hpp> | ||
#include <boost/predef.h> | ||
|
||
#include <algorithm> | ||
#include <atomic> | ||
#include <cassert> | ||
#include <cstdint> | ||
#include <cstring> | ||
#include <mutex> | ||
|
||
#include <boost/align.hpp> | ||
#include <boost/container/static_vector.hpp> | ||
#include <boost/predef.h> | ||
|
||
#if BOOST_OS_LINUX | ||
#include <sys/mman.h> | ||
#endif | ||
|
@@ -76,7 +78,9 @@ class SlabAllocator | |
|
||
while (data + item <= p_ + size_) | ||
{ | ||
*reinterpret_cast<std::uint8_t**>(data) = l_; | ||
// Use memcpy to avoid unaligned UB | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe use the std:: prefix for memcpy There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in a553a13 [fold] Replace memcpy with std::memcpy Edit: I also confirmed the compiler optimized There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks fine. |
||
// (will optimize to equivalent code) | ||
std::memcpy(data, &l_, sizeof(std::uint8_t*)); | ||
l_ = data; | ||
data += item; | ||
} | ||
|
@@ -115,7 +119,11 @@ class SlabAllocator | |
ret = l_; | ||
|
||
if (ret) | ||
l_ = *reinterpret_cast<std::uint8_t**>(ret); | ||
{ | ||
// Use memcpy to avoid unaligned UB | ||
// (will optimize to equivalent code) | ||
std::memcpy(&l_, ret, sizeof(std::uint8_t*)); | ||
} | ||
} | ||
|
||
return ret; | ||
|
@@ -136,7 +144,10 @@ class SlabAllocator | |
assert(own(ptr)); | ||
|
||
std::lock_guard l(m_); | ||
*reinterpret_cast<std::uint8_t**>(ptr) = l_; | ||
|
||
// Use memcpy to avoid unaligned UB | ||
// (will optimize to equivalent code) | ||
std::memcpy(ptr, &l_, sizeof(std::uint8_t*)); | ||
l_ = ptr; | ||
} | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,8 @@ You can contact the author at : | |
|
||
#include <ripple/beast/hash/impl/xxhash.h> | ||
|
||
#include <cstring> | ||
|
||
//************************************** | ||
// Tuning parameters | ||
//************************************** | ||
|
@@ -87,7 +89,7 @@ You can contact the author at : | |
//************************************** | ||
// Includes & Memory related functions | ||
//************************************** | ||
//#include "xxhash.h" | ||
// #include "xxhash.h" | ||
// Modify the local functions below should you wish to use some other memory | ||
// routines for malloc(), free() | ||
#include <stdlib.h> | ||
|
@@ -260,7 +262,13 @@ FORCE_INLINE U64 | |
XXH_readLE64_align(const void* ptr, XXH_endianess endian, XXH_alignment align) | ||
{ | ||
if (align == XXH_unaligned) | ||
return endian == XXH_littleEndian ? A64(ptr) : XXH_swap64(A64(ptr)); | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The XXHASH code has special config for this called XXH_USE_UNALIGNED_ACCESS. You can see it in https://github.com/XRPLF/rippled/blob/develop/src/ripple/beast/hash/impl/xxhash.cpp#L130 and maybe you can set this for compiling without making the changes to the external code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like that would have a larger performance penalty than the change I made (although I admit I haven't looked into it that deeply). If people would rather leave it unmodified we can look at the perf penalty of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally, I'm fine with modifying There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okey There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder whether using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with Nik that hashing something that's already a SHA-256 hash doesn't add value. We could look to see if we could remove XXHASH. I'll make a issue. Edit: Issue is here: #4547 |
||
// Use memcpy to avoid unaligned UB | ||
U64 tmp_aligned; | ||
std::memcpy(&tmp_aligned, ptr, sizeof(U64)); | ||
return endian == XXH_littleEndian ? tmp_aligned | ||
: XXH_swap64(tmp_aligned); | ||
} | ||
else | ||
return endian == XXH_littleEndian ? *(U64*)ptr : XXH_swap64(*(U64*)ptr); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the predef ok to remove from here because the code uses it for BOOST_OS_LINUX on next ligne. Is it maybe better to remove the linux special code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These includes are still here, I just moved them earlier in the file. (We usually put boost includes should come before system includes). This isn't needed for this patch, but I thought I'd clean that up while I was edit this file.
As for removing the linux specific code: It looks like Nik is ambivalent about the hint, and it's possible he may reconsider that, but I don't think we should touch that in this patch.