Browse Source

perf: pattern_id using 16-bit instead of 64-bit

master
Dnomd343 4 days ago
parent
commit
acdb61362b
  1. 9
      src/core/benchmark/group.cc
  2. 25
      src/core/group/group.h
  3. 6
      src/core/group/internal/group.inl
  4. 2
      src/core/group/internal/group_union.inl

9
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();

25
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> groups(uint_fast16_t pattern_id) const;
[[nodiscard]] constexpr std::optional<Groups> 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<GroupUnion>);
static_assert(std::is_trivially_copyable_v<GroupUnion>);
static_assert(!std::is_default_constructible_v<GroupUnion>);
static_assert(std::is_trivially_destructible_v<GroupUnion>);
static_assert(std::is_nothrow_destructible_v<GroupUnion>);
static_assert(std::is_nothrow_copy_assignable_v<GroupUnion>);
static_assert(std::is_nothrow_move_assignable_v<GroupUnion>);
static_assert(std::is_nothrow_copy_constructible_v<GroupUnion>);
static_assert(std::is_nothrow_move_constructible_v<GroupUnion>);
static_assert(std::is_trivially_copy_assignable_v<GroupUnion>);
static_assert(std::is_trivially_move_assignable_v<GroupUnion>);
static_assert(std::is_trivially_copy_constructible_v<GroupUnion>);
static_assert(std::is_trivially_move_constructible_v<GroupUnion>);
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<Group> 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);
// ------------------------------------------------------------------------------------- //
};

6
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> Group::create(const uint_fast8_t type_id, const uint_fast16_t pattern_id, const Toward toward) {
constexpr std::optional<Group> 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

2
src/core/group/internal/group_union.inl

@ -81,7 +81,7 @@ constexpr auto GroupUnion::groups() const -> Groups {
| std::ranges::to<std::vector>();
}
constexpr auto GroupUnion::groups(const uint_fast16_t pattern_id) const -> std::optional<Groups> {
constexpr auto GroupUnion::groups(const uint_least16_t pattern_id) const -> std::optional<Groups> {
if (pattern_id >= pattern_num()) {
return std::nullopt;
}

Loading…
Cancel
Save