From ee27786cb347c3fdc6379b55c4bb8ebd38d64e38 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 31 Mar 2024 17:54:28 +0800 Subject: [PATCH] update: using xxh3 algorithm --- src/core_test/CMakeLists.txt | 2 +- src/core_test/cases/all_cases.cc | 25 +++++++------- src/core_test/ffi/all_cases.cc | 6 ++-- src/core_test/{utils => utility}/exposer.h | 0 src/core_test/utility/hash.h | 40 ++++++++++++++++++++++ src/core_test/utils/xxhsum.h | 27 --------------- 6 files changed, 56 insertions(+), 44 deletions(-) rename src/core_test/{utils => utility}/exposer.h (100%) create mode 100644 src/core_test/utility/hash.h delete mode 100644 src/core_test/utils/xxhsum.h diff --git a/src/core_test/CMakeLists.txt b/src/core_test/CMakeLists.txt index d6d1fb6..6cc19c5 100644 --- a/src/core_test/CMakeLists.txt +++ b/src/core_test/CMakeLists.txt @@ -8,7 +8,7 @@ set(KLOTSKI_TEST_DEPS klotski_core # ------------------------------------------------------------------------------------ # -include_directories(utils) +include_directories(utility) include_directories(${KLOTSKI_ROOT_DIR}/src/core/ffi) diff --git a/src/core_test/cases/all_cases.cc b/src/core_test/cases/all_cases.cc index 9aae092..c4b45c3 100644 --- a/src/core_test/cases/all_cases.cc +++ b/src/core_test/cases/all_cases.cc @@ -1,14 +1,11 @@ -#include #include -#include "xxhsum.h" +#include "hash.h" #include "exposer.h" #include "all_cases.h" #include "gtest/gtest.h" #include "BS_thread_pool.hpp" -using xxhash::xxhsum; - using klotski::cases::AllCases; using klotski::cases::BasicRanges; @@ -16,10 +13,16 @@ using klotski::cases::ALL_CASES_NUM; using klotski::cases::ALL_CASES_NUM_; using klotski::cases::BASIC_RANGES_NUM; -static const auto TEST_THREAD_NUM = 256; +static constexpr int TEST_THREAD_NUM = 256; + +static constexpr uint64_t BASIC_RANGES_XXH3 = 0x82b040060044e336; -static const std::string ALL_CASES_XXHASH = "d589c8a45983ebb6"; -static const std::string BASIC_RANGES_XXHASH = "5e7f633b7bd8af37"; +static constexpr std::array ALL_CASES_XXH3 = { + 0x71c8ff7a71c93da0, 0x2a5247ee8bfed666, 0xf4efc8fc692d58e2, 0x2d06800538d394c2, + 0xb3f7cc1b962d6944, 0x7e2792f8ab777faa, 0x4b8e78026cca8a27, 0x2d06800538d394c2, + 0x8acd688c5ab93c42, 0xedca5101ed81cc77, 0xe8dc9d30c91ce682, 0x2d06800538d394c2, + 0x2cdf6c14a7ce3e9a, 0xb9dd04a315583f5c, 0x19046e49c44ae90d, 0x2d06800538d394c2, +}; /// Forcibly modify private variables to reset state. PRIVATE_ACCESS(AllCases, available_, bool) @@ -39,12 +42,12 @@ void all_cases_reset() { void basic_ranges_verify() { auto &basic_ranges = BasicRanges::instance().fetch(); EXPECT_EQ(basic_ranges.size(), BASIC_RANGES_NUM); // verify basic ranges size - EXPECT_EQ(xxhsum(basic_ranges), BASIC_RANGES_XXHASH); // verify basic ranges checksum + EXPECT_EQ(hash::xxh3(basic_ranges), BASIC_RANGES_XXH3); // verify basic ranges checksum } /// Verify that whether all cases data is correct. void all_cases_verify() { - auto &all_cases = AllCases::instance().fetch(); + const auto &all_cases = AllCases::instance().fetch(); for (int head = 0; head < 16; ++head) { EXPECT_EQ(all_cases[head].size(), ALL_CASES_NUM[head]); // verify all cases size } @@ -55,11 +58,9 @@ void all_cases_verify() { }); EXPECT_EQ(all_cases_num, ALL_CASES_NUM_); // verify all cases global size - std::string all_cases_xxh; for (uint64_t head = 0; head < 16; ++head) { - all_cases_xxh += xxhsum(AllCases::instance().fetch()[head]); + EXPECT_EQ(hash::xxh3(all_cases[head]), ALL_CASES_XXH3[head]); // verify all cases checksum } - EXPECT_EQ(xxhsum(all_cases_xxh), ALL_CASES_XXHASH); // verify all cases checksum } std::unique_ptr race_test(int parallel, const std::function &item) { diff --git a/src/core_test/ffi/all_cases.cc b/src/core_test/ffi/all_cases.cc index 3c4a984..5a4638b 100644 --- a/src/core_test/ffi/all_cases.cc +++ b/src/core_test/ffi/all_cases.cc @@ -2,14 +2,12 @@ #include #include -#include "md5.h" +#include "hash.h" #include "exposer.h" #include "klotski.h" #include "all_cases.h" #include "gtest/gtest.h" -using md5::MD5; - using klotski::cases::AllCases; using klotski::cases::BasicRanges; @@ -179,5 +177,5 @@ TEST(AllCases, all_cases_export) { all_cases_str += tmp; } } - EXPECT_EQ(MD5::Hash(all_cases_str), ALL_CASES_MD5); + EXPECT_EQ(hash::md5(all_cases_str), ALL_CASES_MD5); } diff --git a/src/core_test/utils/exposer.h b/src/core_test/utility/exposer.h similarity index 100% rename from src/core_test/utils/exposer.h rename to src/core_test/utility/exposer.h diff --git a/src/core_test/utility/hash.h b/src/core_test/utility/hash.h new file mode 100644 index 0000000..a2cfcf0 --- /dev/null +++ b/src/core_test/utility/hash.h @@ -0,0 +1,40 @@ +#pragma once + +/// Provides XXH3 and MD5 hash calculation support. The former is far ahead in +/// speed, but the latter is more of a commemorative significance. + +#include "md5.h" +#include "xxh3.h" + +#include +#include + +namespace hash { + +inline std::string md5(const void *data, const uint64_t size) { + return ::md5::MD5::Hash(data, size); +} + +inline uint64_t xxh3(const void *data, const uint64_t size) { + return XXH_INLINE_XXH3_64bits(data, size); +} + +inline std::string md5(const std::string_view &data) { + return md5(data.data(), data.size()); +} + +inline uint64_t xxh3(const std::string_view &data) { + return xxh3(data.data(), data.size()); +} + +template +inline std::string md5(const std::vector &data) { + return md5(data.data(), data.size() * sizeof(T)); +} + +template +inline uint64_t xxh3(const std::vector &data) { + return xxh3(data.data(), data.size() * sizeof(T)); +} + +} // namespace hash diff --git a/src/core_test/utils/xxhsum.h b/src/core_test/utils/xxhsum.h deleted file mode 100644 index 05ca4a9..0000000 --- a/src/core_test/utils/xxhsum.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include -#include "xxhash.h" - -namespace xxhash { - -std::string xxhsum(const void *data, size_t size) { - char *hash; - auto state = XXH64_createState(); - XXH64_reset(state, 0); - XXH64_update(state, data, size); - asprintf(&hash, "%016llx", XXH64_digest(state)); - XXH64_freeState(state); - return hash; -} - -std::string xxhsum(const std::string &data) { - return xxhsum(data.c_str(), data.length()); -} - -template -std::string xxhsum(const std::vector &data) { - return xxhsum(data.data(), data.size() * sizeof(T)); -} - -} // namespace xxhsum