BLI: optimize Map/Set/VectorSet.clear methods

Previously, those methods would destruct and reconstruct
the data structure. While that was more simple in initial
implementation, it has some downsides which are not resolved:
* Already allocated memory is lost. So new memory would have
  to be allocated when the data structure is refilled.
* The clearing process itself was slower because it did unnecessary
  work.
This commit is contained in:
Jacques Lucke 2022-03-29 10:40:47 +02:00
parent 29d9bb89ce
commit a264dff4fa
3 changed files with 22 additions and 4 deletions

View File

@ -962,7 +962,13 @@ class Map {
*/
void clear()
{
this->noexcept_reset();
for (Slot &slot : slots_) {
slot.~Slot();
new (&slot) Slot();
}
removed_slots_ = 0;
occupied_and_removed_slots_ = 0;
}
/**

View File

@ -515,8 +515,13 @@ class Set {
*/
void clear()
{
this->~Set();
new (this) Set();
for (Slot &slot : slots_) {
slot.~Slot();
new (&slot) Slot();
}
removed_slots_ = 0;
occupied_and_removed_slots_ = 0;
}
/**

View File

@ -531,7 +531,14 @@ class VectorSet {
*/
void clear()
{
this->noexcept_reset();
destruct_n(keys_, this->size());
for (Slot &slot : slots_) {
slot.~Slot();
new (&slot) Slot();
}
removed_slots_ = 0;
occupied_and_removed_slots_ = 0;
}
/**