From 015a1191df5d9658158fbec56170609dccd00fb6 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 2 Nov 2024 15:06:29 +0800 Subject: [PATCH] perf: spawn groups of GroupUnion --- src/core/benchmark/group.cc | 11 +-- src/core/group/group.h | 4 +- src/core/group/internal/group.inl | 8 +- src/core/group/internal/group_union.inl | 119 +++++++++++++++++++----- 4 files changed, 105 insertions(+), 37 deletions(-) diff --git a/src/core/benchmark/group.cc b/src/core/benchmark/group.cc index 2416c88..dd7fbbb 100644 --- a/src/core/benchmark/group.cc +++ b/src/core/benchmark/group.cc @@ -250,17 +250,16 @@ static void RangesDerive(benchmark::State &state) { static void SpawnGroups(benchmark::State &state) { - volatile auto val = 169; - auto group_union = klotski::group::GroupUnion::create(val).value(); + auto gu = GroupUnion::unsafe_create(169); for (auto _ : state) { - volatile auto kk = group_union.groups(); + volatile auto kk = gu.groups(); } } -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); @@ -274,6 +273,6 @@ BENCHMARK(RawCodeToTypeId)->Arg(8)->Arg(64)->Arg(256); // BENCHMARK(RangesDerive)->Unit(benchmark::kMillisecond); -// BENCHMARK(SpawnGroups); +BENCHMARK(SpawnGroups); BENCHMARK_MAIN(); diff --git a/src/core/group/group.h b/src/core/group/group.h index d0a823f..3b5450e 100644 --- a/src/core/group/group.h +++ b/src/core/group/group.h @@ -253,15 +253,15 @@ public: // ------------------------------------------------------------------------------------- // private: - uint_fast8_t type_id_; Toward toward_; + uint_fast8_t type_id_; uint_fast16_t pattern_id_; /// Tiled merge of type_id and pattern_id. [[nodiscard]] constexpr uint32_t flat_id() const; /// Hidden constructor called from unsafe_create. - constexpr Group(uint_fast8_t type_id, Toward toward, uint_fast16_t pattern_id); + constexpr Group(Toward toward, uint_fast8_t type_id, uint_fast16_t pattern_id); // ------------------------------------------------------------------------------------- // }; diff --git a/src/core/group/internal/group.inl b/src/core/group/internal/group.inl index 5352f4e..ce02763 100644 --- a/src/core/group/internal/group.inl +++ b/src/core/group/internal/group.inl @@ -68,7 +68,7 @@ constexpr auto operator==(const Group &lhs, const Group &rhs) { } constexpr Group Group::unsafe_create(uint_fast8_t type_id, uint_fast16_t pattern_id, Toward toward) { - return {type_id, toward, pattern_id}; + return {toward, type_id, pattern_id}; } constexpr std::optional Group::create(uint_fast8_t type_id, uint_fast16_t pattern_id, Toward toward) { @@ -98,10 +98,6 @@ inline Group Group::from_common_code(codec::CommonCode common_code) { return from_raw_code(common_code.to_raw_code()); } -constexpr Group::Group(uint_fast8_t type_id, Toward toward, uint_fast16_t pattern_id) { - type_id_ = type_id; - pattern_id_ = pattern_id; - toward_ = toward; -} +constexpr Group::Group(const Toward toward, const uint_fast8_t type_id, const uint_fast16_t pattern_id) : toward_(toward), type_id_(type_id), pattern_id_(pattern_id) {} } // namespace klotski::group diff --git a/src/core/group/internal/group_union.inl b/src/core/group/internal/group_union.inl index c3a4440..3f7bbf0 100644 --- a/src/core/group/internal/group_union.inl +++ b/src/core/group/internal/group_union.inl @@ -2,6 +2,8 @@ #include +// TODO: include at group header +#include "constant/group.h" #include "constant/group_union.h" namespace klotski::group { @@ -80,29 +82,100 @@ constexpr GroupUnion GroupUnion::from_common_code(const codec::CommonCode common //} constexpr std::vector GroupUnion::groups() const { - std::vector groups; - groups.reserve(group_num()); - for (uint32_t pattern_id = 0; pattern_id < pattern_num(); ++pattern_id) { - auto group = Group::unsafe_create(type_id_, pattern_id, Group::Toward::A); - groups.emplace_back(group); - switch (group.mirror_type()) { - case Group::MirrorType::Full: - continue; - case Group::MirrorType::Horizontal: - groups.emplace_back(Group::unsafe_create(type_id_, pattern_id, Group::Toward::C)); - break; - case Group::MirrorType::Centro: - case Group::MirrorType::Vertical: - groups.emplace_back(Group::unsafe_create(type_id_, pattern_id, Group::Toward::B)); - break; - case Group::MirrorType::Ordinary: - groups.emplace_back(Group::unsafe_create(type_id_, pattern_id, Group::Toward::B)); - groups.emplace_back(Group::unsafe_create(type_id_, pattern_id, Group::Toward::C)); - groups.emplace_back(Group::unsafe_create(type_id_, pattern_id, Group::Toward::D)); - break; - } - } - return groups; + + // std::vector groups; + // groups.reserve(group_num()); + // for (uint32_t pattern_id = 0; pattern_id < pattern_num(); ++pattern_id) { + // auto group = Group::unsafe_create(type_id_, pattern_id, Group::Toward::A); + // groups.emplace_back(group); + // switch (group.mirror_type()) { + // case Group::MirrorType::Full: + // continue; + // case Group::MirrorType::Horizontal: + // groups.emplace_back(Group::unsafe_create(type_id_, pattern_id, Group::Toward::C)); + // break; + // case Group::MirrorType::Centro: + // case Group::MirrorType::Vertical: + // groups.emplace_back(Group::unsafe_create(type_id_, pattern_id, Group::Toward::B)); + // break; + // case Group::MirrorType::Ordinary: + // groups.emplace_back(Group::unsafe_create(type_id_, pattern_id, Group::Toward::B)); + // groups.emplace_back(Group::unsafe_create(type_id_, pattern_id, Group::Toward::C)); + // groups.emplace_back(Group::unsafe_create(type_id_, pattern_id, Group::Toward::D)); + // break; + // } + // } + // return groups; + + + // auto build = [this](const uint32_t pattern_id) -> std::vector { + // auto flat_id = PATTERN_OFFSET[type_id_] + pattern_id; + // auto mirror_type = static_cast(PATTERN_DATA[flat_id] & 0b111); + // + // switch (mirror_type) { + // case Group::MirrorType::Full: + // return { + // Group::unsafe_create(type_id_, pattern_id, Group::Toward::A), + // }; + // case Group::MirrorType::Horizontal: + // return { + // Group::unsafe_create(type_id_, pattern_id, Group::Toward::A), + // Group::unsafe_create(type_id_, pattern_id, Group::Toward::C), + // }; + // case Group::MirrorType::Centro: + // case Group::MirrorType::Vertical: + // return { + // Group::unsafe_create(type_id_, pattern_id, Group::Toward::A), + // Group::unsafe_create(type_id_, pattern_id, Group::Toward::B), + // }; + // case Group::MirrorType::Ordinary: + // return { + // Group::unsafe_create(type_id_, pattern_id, Group::Toward::A), + // Group::unsafe_create(type_id_, pattern_id, Group::Toward::B), + // Group::unsafe_create(type_id_, pattern_id, Group::Toward::C), + // Group::unsafe_create(type_id_, pattern_id, Group::Toward::D), + // }; + // } + // }; + // + // return std::views::iota(0U, pattern_num()) + // | std::views::transform(build) + // | std::views::join + // | std::ranges::to(); + + // std::vector groups; + // groups.reserve(group_num()); + + auto begin = GROUP_OFFSET[type_id_]; + auto end = begin + group_num(); + + auto build = [this](uint32_t kk) { + auto raw = GROUP_DATA[kk]; + uint32_t pattern_id = (raw >> 2) & 0b1111111111; + uint32_t toward = raw & 0b11; + return Group::unsafe_create(type_id_, pattern_id, (Group::Toward)toward); + }; + + return std::views::iota(begin, end) | std::views::transform(build) | std::ranges::to(); + + + // auto build = [this](uint64_t raw) { + // uint32_t pattern_id = (raw >> 2) & 0b1111111111; + // uint32_t toward = raw & 0b11; + // return Group::unsafe_create(type_id_, pattern_id, (Group::Toward)toward); + // }; + // return GROUP_DATA | std::views::drop(GROUP_OFFSET[type_id_]) | std::views::take(group_num()) | std::views::transform(build) | std::ranges::to(); + // return std::ranges::subrange(GROUP_DATA.begin() + begin, GROUP_DATA.begin() + begin + group_num()) | std::views::transform(build) | std::ranges::to(); + + // for (auto i = begin; i < end; ++i) { + // auto raw = GROUP_DATA[i]; + // uint32_t pattern_id = (raw >> 2) & 0b1111111111; + // uint32_t toward = raw & 0b11; + // auto group = Group::unsafe_create(type_id_, pattern_id, (Group::Toward)toward); + // groups.emplace_back(group); + // } + // + // return groups; } constexpr std::optional> GroupUnion::groups(const uint_fast16_t pattern_id) const {