From 03d06321152eeb5549d396f547ae8d43d1deb6ca Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 27 Oct 2024 18:09:40 +0800 Subject: [PATCH] perf: obtain type_id from RawCode or CommonCode --- src/core/benchmark/group.cc | 28 ++++++++++++++------------ src/core/group/group.h | 4 ++-- src/core/group/internal/group_union.cc | 9 ++++++--- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/core/benchmark/group.cc b/src/core/benchmark/group.cc index 0615376..2416c88 100644 --- a/src/core/benchmark/group.cc +++ b/src/core/benchmark/group.cc @@ -13,6 +13,9 @@ using klotski::cases::AllCases; +using klotski::group::Group; +using klotski::group::GroupUnion; + /// Build all valid CommonCodes. static std::vector all_common_codes() { std::vector codes; @@ -70,8 +73,7 @@ static void CommonCodeToTypeId(benchmark::State &state) { for (auto _ : state) { for (auto code : samples) { - - volatile auto ret = klotski::cases::GroupUnion::type_id(code); + volatile auto ret = GroupUnion::type_id(code); } } @@ -93,7 +95,7 @@ static void RawCodeToTypeId(benchmark::State &state) { for (auto _ : state) { for (auto code : samples) { - volatile auto ret = klotski::cases::GroupUnion::type_id(code); + volatile auto ret = GroupUnion::type_id(code); } } @@ -108,7 +110,7 @@ static void GroupExtend(benchmark::State &state) { for (auto _ : state) { - volatile auto ret = klotski::cases::Group::extend(src, 0); + volatile auto ret = klotski::group::Group_extend(src, 0); // std::cout << ret.size() << std::endl; } @@ -222,12 +224,12 @@ static void RangesDerive(benchmark::State &state) { // klotski::cases::Ranges results; // results.reserve(klotski::cases::ALL_CASES_NUM_); - auto group_union = klotski::cases::GroupUnion::unsafe_create(169); + auto group_union = klotski::group::GroupUnion::unsafe_create(169); - std::vector unions; - unions.reserve(klotski::cases::TYPE_ID_LIMIT); - for (int type_id = 0; type_id < klotski::cases::TYPE_ID_LIMIT; ++type_id) { - unions.emplace_back(klotski::cases::GroupUnion::create(type_id).value()); + std::vector unions; + unions.reserve(klotski::group::TYPE_ID_LIMIT); + for (int type_id = 0; type_id < klotski::group::TYPE_ID_LIMIT; ++type_id) { + unions.emplace_back(klotski::group::GroupUnion::create(type_id).value()); } for (auto _ : state) { @@ -249,7 +251,7 @@ static void RangesDerive(benchmark::State &state) { static void SpawnGroups(benchmark::State &state) { volatile auto val = 169; - auto group_union = klotski::cases::GroupUnion::create(val).value(); + auto group_union = klotski::group::GroupUnion::create(val).value(); for (auto _ : state) { volatile auto kk = group_union.groups(); @@ -257,10 +259,10 @@ static void SpawnGroups(benchmark::State &state) { } -// BENCHMARK(CommonCodeToTypeId)->Arg(8)->Arg(64)->Arg(256); -// BENCHMARK(RawCodeToTypeId)->Arg(8)->Arg(64)->Arg(256); +BENCHMARK(CommonCodeToTypeId)->Arg(8)->Arg(64)->Arg(256); +BENCHMARK(RawCodeToTypeId)->Arg(8)->Arg(64)->Arg(256); -BENCHMARK(GroupExtend)->Unit(benchmark::kMillisecond); +// BENCHMARK(GroupExtend)->Unit(benchmark::kMillisecond); // BENCHMARK(FilterFromAllCases)->Unit(benchmark::kMillisecond); diff --git a/src/core/group/group.h b/src/core/group/group.h index ecd76cb..0c188e4 100644 --- a/src/core/group/group.h +++ b/src/core/group/group.h @@ -150,10 +150,10 @@ private: // ------------------------------------------------------------------------------------- // /// Get the type id of RawCode. - static KLSK_INLINE uint32_t type_id(codec::RawCode raw_code); + static KLSK_INLINE uint_fast8_t type_id(codec::RawCode raw_code); /// Get the type id of CommonCode. - static KLSK_INLINE uint32_t type_id(codec::CommonCode common_code); + static KLSK_INLINE uint_fast8_t type_id(codec::CommonCode common_code); // ------------------------------------------------------------------------------------- // }; diff --git a/src/core/group/internal/group_union.cc b/src/core/group/internal/group_union.cc index 9ecacfe..c9b7ea0 100644 --- a/src/core/group/internal/group_union.cc +++ b/src/core/group/internal/group_union.cc @@ -13,19 +13,22 @@ using klotski::cases::BASIC_RANGES_NUM; #define RANGE_DERIVE(HEAD) ranges.derive(HEAD, cases[HEAD]) -static KLSK_INLINE uint32_t to_type_id(const int n, const int n_2x1, const int n_1x1) { +static KLSK_INLINE uint_fast8_t to_type_id(const int n, const int n_2x1, const int n_1x1) { + KLSK_ASSUME(n >= 0 && n <= 7); + KLSK_ASSUME(n_2x1 >= 0 && n_2x1 <= n); + KLSK_ASSUME(n_1x1 >= 0 && n_1x1 <= (14 - n * 2)); constexpr int offset[8] = {0, 15, 41, 74, 110, 145, 175, 196}; return offset[n] + (15 - n * 2) * n_2x1 + n_1x1; } -uint32_t GroupUnion::type_id(const CommonCode common_code) { +uint_fast8_t GroupUnion::type_id(const CommonCode common_code) { const auto range = static_cast(common_code.unwrap()); const auto n_1x1 = std::popcount((range >> 1) & range & 0x55555555); const auto n_2x1 = std::popcount((range >> 1) & ~range & 0x55555555); return to_type_id(std::popcount(range) - n_1x1 * 2, n_2x1, n_1x1); } -uint32_t GroupUnion::type_id(const RawCode raw_code) { +uint_fast8_t GroupUnion::type_id(const RawCode raw_code) { const auto code = raw_code.unwrap(); const auto n = std::popcount(((code >> 1) ^ code) & 0x0249249249249249); const auto n_2x1 = std::popcount((code >> 1) & ~code & 0x0249249249249249);