Skip to content

Commit

Permalink
Optimize unique_hash and make portable.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Sep 6, 2024
1 parent 2e2929d commit ce8cb51
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
10 changes: 4 additions & 6 deletions include/bitcoin/system/impl/hash/functions.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <utility>
#include <bitcoin/system/data/data.hpp>
#include <bitcoin/system/define.hpp>
#include <bitcoin/system/endian/endian.hpp>
#include <bitcoin/system/hash/accumulator.hpp>
#include <bitcoin/system/hash/algorithms.hpp>
#include <bitcoin/system/hash/scrypt.hpp>
Expand Down Expand Up @@ -212,13 +213,10 @@ struct unique_hash_t
template <size_t Size>
INLINE constexpr size_t unique_hash(const data_array<Size>& key) NOEXCEPT
{
constexpr auto size = std::min(Size, sizeof(size_t));
constexpr auto size = sizeof(size_t);

// This optimization breaks data portability (by endianness).
// Could be modified to use an endian conversion vs. simple copy.
size_t value{};
std::copy_n(key.begin(), size, system::byte_cast(value).begin());
return value;
// Conversion is nop on LE architecture, preserves data portability for BE.
return native_to_little_end(byte_cast(array_cast<uint8_t, size>(key)));
}

// Value will vary with sizeof(size_t).
Expand Down
4 changes: 2 additions & 2 deletions test/hash/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,11 @@ BOOST_AUTO_TEST_CASE(functions__unique_hash__some_hash__expected)

if constexpr (sizeof(size_t) == sizeof(uint32_t))
{
BOOST_REQUIRE_EQUAL(hash, 0x60989953_u32);
BOOST_REQUIRE_EQUAL(hash, native_to_little_end(0x60989953_u32));
}
else
{
BOOST_REQUIRE_EQUAL(hash, 0x6636626660989953_u64);
BOOST_REQUIRE_EQUAL(hash, native_to_little_end(0x6636626660989953_u64));
}
}

Expand Down

0 comments on commit ce8cb51

Please sign in to comment.