From acdb61362b5b1d0b197937c22c4cb348a5f3a8f5 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 10 Nov 2024 11:03:54 +0800 Subject: [PATCH] perf: pattern_id using 16-bit instead of 64-bit --- src/core/benchmark/group.cc | 9 +++++---- src/core/group/group.h | 25 ++++++++++++++++++++----- src/core/group/internal/group.inl | 6 +++--- src/core/group/internal/group_union.inl | 2 +- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/core/benchmark/group.cc b/src/core/benchmark/group.cc index 9eebc64..178e3db 100644 --- a/src/core/benchmark/group.cc +++ b/src/core/benchmark/group.cc @@ -253,7 +253,8 @@ static void RangesDerive(benchmark::State &state) { static void SpawnGroups(benchmark::State &state) { - auto gu = GroupUnion::unsafe_create(169); + volatile int type_id = 169; + auto gu = GroupUnion::unsafe_create(type_id); for (auto _ : state) { // volatile auto kk = gu.groups(); @@ -337,13 +338,13 @@ static void ToHorizontalMirror(benchmark::State &state) { // BENCHMARK(RangesDerive)->Unit(benchmark::kMillisecond); -// BENCHMARK(SpawnGroups); +BENCHMARK(SpawnGroups); // BENCHMARK(GroupFromRawCode)->Unit(benchmark::kMillisecond); // BENCHMARK(IsVerticalMirror); // BENCHMARK(IsHorizontalMirror); -BENCHMARK(ToVerticalMirror); -BENCHMARK(ToHorizontalMirror); +// BENCHMARK(ToVerticalMirror); +// BENCHMARK(ToHorizontalMirror); BENCHMARK_MAIN(); diff --git a/src/core/group/group.h b/src/core/group/group.h index 44f69dc..a7f8b27 100644 --- a/src/core/group/group.h +++ b/src/core/group/group.h @@ -117,7 +117,7 @@ public: [[nodiscard]] cases::RangesUnion cases() const; /// Get the group instance with the specified pattern id. - [[nodiscard]] constexpr std::optional groups(uint_fast16_t pattern_id) const; + [[nodiscard]] constexpr std::optional groups(uint_least16_t pattern_id) const; // ------------------------------------------------------------------------------------- // @@ -156,8 +156,23 @@ private: // ------------------------------------------------------------------------------------- // }; +// TODO: using macro for all likely-class + static_assert(std::is_standard_layout_v); static_assert(std::is_trivially_copyable_v); +static_assert(!std::is_default_constructible_v); +static_assert(std::is_trivially_destructible_v); +static_assert(std::is_nothrow_destructible_v); + +static_assert(std::is_nothrow_copy_assignable_v); +static_assert(std::is_nothrow_move_assignable_v); +static_assert(std::is_nothrow_copy_constructible_v); +static_assert(std::is_nothrow_move_constructible_v); + +static_assert(std::is_trivially_copy_assignable_v); +static_assert(std::is_trivially_move_assignable_v); +static_assert(std::is_trivially_copy_constructible_v); +static_assert(std::is_trivially_move_constructible_v); class Group { public: @@ -198,11 +213,11 @@ public: /// Create Group without any check. static constexpr Group unsafe_create(uint_fast8_t type_id, - uint_fast16_t pattern_id, Toward toward); + uint_least16_t pattern_id, Toward toward); /// Create Group with validity check. static constexpr std::optional create(uint_fast8_t type_id, - uint_fast16_t pattern_id, Toward toward); + uint_least16_t pattern_id, Toward toward); // ------------------------------------------------------------------------------------- // @@ -258,13 +273,13 @@ public: private: Toward toward_; uint_fast8_t type_id_; - uint_fast16_t pattern_id_; + uint_least16_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(Toward toward, uint_fast8_t type_id, uint_fast16_t pattern_id); + constexpr Group(Toward toward, uint_fast8_t type_id, uint_least16_t pattern_id); // ------------------------------------------------------------------------------------- // }; diff --git a/src/core/group/internal/group.inl b/src/core/group/internal/group.inl index a7e0d9f..e091ebb 100644 --- a/src/core/group/internal/group.inl +++ b/src/core/group/internal/group.inl @@ -69,11 +69,11 @@ constexpr auto operator==(const Group &lhs, const Group &rhs) { && lhs.pattern_id_ == rhs.pattern_id_; } -constexpr Group Group::unsafe_create(const uint_fast8_t type_id, const uint_fast16_t pattern_id, const Toward toward) { +constexpr Group Group::unsafe_create(const uint_fast8_t type_id, const uint_least16_t pattern_id, const Toward toward) { return {toward, type_id, pattern_id}; } -constexpr std::optional Group::create(const uint_fast8_t type_id, const uint_fast16_t pattern_id, const Toward toward) { +constexpr std::optional Group::create(const uint_fast8_t type_id, const uint_least16_t pattern_id, const Toward toward) { if (type_id >= TYPE_ID_LIMIT) { return std::nullopt; } @@ -100,6 +100,6 @@ inline Group Group::from_common_code(const codec::CommonCode common_code) { return from_raw_code(common_code.to_raw_code()); } -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) {} +constexpr Group::Group(const Toward toward, const uint_fast8_t type_id, const uint_least16_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 4c351ad..68d7f3f 100644 --- a/src/core/group/internal/group_union.inl +++ b/src/core/group/internal/group_union.inl @@ -81,7 +81,7 @@ constexpr auto GroupUnion::groups() const -> Groups { | std::ranges::to(); } -constexpr auto GroupUnion::groups(const uint_fast16_t pattern_id) const -> std::optional { +constexpr auto GroupUnion::groups(const uint_least16_t pattern_id) const -> std::optional { if (pattern_id >= pattern_num()) { return std::nullopt; }