@@ -1780,7 +1780,7 @@ namespace hud
17801780 /* * Compute the size of the allocation needed for the given slot count. */
17811781 [[nodiscard]] constexpr usize current_allocation_size () const noexcept
17821782 {
1783- const usize control_size {max_slot_count_ + 1 + control::COUNT_CLONED_BYTE };
1783+ const usize control_size {control_size_for_max_count ( max_slot_count_) };
17841784 const uptr aligned_control_size {hud::memory::align_address (control_size, sizeof (slot_type))};
17851785 return aligned_control_size + max_slot_count_ * sizeof (slot_type);
17861786 }
@@ -1880,11 +1880,9 @@ namespace hud
18801880
18811881 constexpr usize allocate_control_and_slot (usize max_slot_count) noexcept
18821882 {
1883- const usize ctrl_size {control_size_for_max_count (max_slot_count)};
1883+ const usize control_size {control_size_for_max_count (max_slot_count)};
18841884 const usize slot_size {max_slot_count * sizeof (slot_type)};
1885-
1886- const uptr aligned_control_size {hud::memory::align_address (ctrl_size, sizeof (slot_type))};
1887- const usize aligned_allocation_size {aligned_control_size + slot_size};
1885+ const uptr aligned_control_size {hud::memory::align_address (control_size, sizeof (slot_type))};
18881886
18891887 if (hud::is_constant_evaluated ())
18901888 {
@@ -1893,32 +1891,33 @@ namespace hud
18931891 }
18941892 else
18951893 {
1894+ const usize aligned_allocation_size {aligned_control_size + slot_size};
18961895 control_ptr_ = allocator_.template allocate <control_type>(aligned_allocation_size).data ();
1897- slot_ptr_ = reinterpret_cast <slot_type *>(hud::memory::align_address (reinterpret_cast <const uptr>(control_ptr_ + ctrl_size ), alignof (slot_type)));
1896+ slot_ptr_ = reinterpret_cast <slot_type *>(hud::memory::align_address (reinterpret_cast <const uptr>(control_ptr_ + control_size ), alignof (slot_type)));
18981897 hud::check (hud::memory::is_pointer_aligned (slot_ptr_, alignof (slot_type)));
18991898 }
1900- return ctrl_size ;
1899+ return control_size ;
19011900 }
19021901
19031902 constexpr void free_control_and_slot (control_type *control_ptr, slot_type *slot_ptr, usize max_slot_count) noexcept
19041903 {
19051904 if (max_slot_count > 0 )
19061905 {
1906+ const usize control_size {control_size_for_max_count (max_slot_count)};
1907+ const usize slot_size {max_slot_count * sizeof (slot_type)};
1908+ const uptr aligned_control_size {hud::memory::align_address (control_size, sizeof (slot_type))};
1909+
19071910 // In a constant-evaluated context, bit_cast cannot be used with pointers
19081911 // and allocation is done in two separate allocation
19091912 if (hud::is_constant_evaluated ())
19101913 {
1911- // Control size is the number of slot + sentinel + number of cloned bytes
1912- const usize control_size {control_size_for_max_count (max_slot_count)};
1913- const usize slot_size {max_slot_count * sizeof (slot_type)};
1914- const uptr aligned_control_size {hud::memory::align_address (control_size, sizeof (slot_type))};
1915-
19161914 allocator_.template free <control_type>({control_ptr, aligned_control_size});
19171915 allocator_.template free <slot_type>({slot_ptr, slot_size});
19181916 }
19191917 else
19201918 {
1921- allocator_.template free <slot_type>({hud::bit_cast<slot_type *>(control_ptr), current_allocation_size ()});
1919+ const usize aligned_allocation_size {aligned_control_size + slot_size};
1920+ allocator_.template free <control_type>({control_ptr, aligned_allocation_size});
19221921 }
19231922 }
19241923 }
@@ -2146,10 +2145,20 @@ namespace hud
21462145 return false ;
21472146 }
21482147
2149- for (const auto &elem : left)
2148+ // Speed of find is dependent of the max_slot_count_
2149+ // We want to find in the smallest max_slot_count and iterate on the bigger only once
2150+ const hashset<key_t , hasher_t , key_equal_t , allocator_t > *biggest_capacity = &left;
2151+ const hashset<key_t , hasher_t , key_equal_t , allocator_t > *smallest_capacity = &right;
2152+ if (smallest_capacity->max_count () > biggest_capacity->max_count ())
2153+ {
2154+ hud::swap (biggest_capacity, smallest_capacity);
2155+ }
2156+
2157+ // Iterate over biggest capacity and find in the smallest each elements
2158+ for (const auto &elem : *biggest_capacity)
21502159 {
2151- const auto &it = right. find (elem.key ());
2152- if (it == right. end () && !(it-> value () == elem. value () ))
2160+ const auto &it = smallest_capacity-> find (elem.key ());
2161+ if (it == smallest_capacity-> end ( ))
21532162 {
21542163 return false ;
21552164 }
0 commit comments