From 03a994e198d3dc33351b3f469705af5c460e2647 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 25 Feb 2022 13:53:13 +0300 Subject: [PATCH] Optimize erase_node, part 1. --- tdutils/td/utils/FlatHashMapLinear.h | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/tdutils/td/utils/FlatHashMapLinear.h b/tdutils/td/utils/FlatHashMapLinear.h index da4b70bd5..d24e1d280 100644 --- a/tdutils/td/utils/FlatHashMapLinear.h +++ b/tdutils/td/utils/FlatHashMapLinear.h @@ -602,20 +602,33 @@ class FlatHashTable { void erase_node(NodeT *it) { DCHECK(nodes_ <= it && static_cast(it - nodes_) < bucket_count()); - uint32 empty_i = static_cast(it - nodes_); - auto empty_bucket = empty_i; - nodes_[empty_bucket].clear(); + it->clear(); used_node_count()--; - for (uint32 test_i = empty_i + 1;; test_i++) { - auto test_bucket = test_i & get_bucket_count_mask(); + const auto bucket_count = get_bucket_count(); + auto empty_bucket = static_cast(it - nodes_); + for (uint32 test_bucket = empty_bucket + 1; test_bucket < bucket_count; test_bucket++) { if (nodes_[test_bucket].empty()) { - break; + return; + } + + auto want_bucket = calc_bucket(nodes_[test_bucket].key()); + if (want_bucket <= empty_bucket || want_bucket > test_bucket) { + nodes_[empty_bucket] = std::move(nodes_[test_bucket]); + empty_bucket = test_bucket; + } + } + + auto empty_i = empty_bucket; + for (uint32 test_i = bucket_count;; test_i++) { + auto test_bucket = test_i - get_bucket_count(); + if (nodes_[test_bucket].empty()) { + return; } auto want_i = calc_bucket(nodes_[test_bucket].key()); if (want_i < empty_i) { - want_i += get_bucket_count(); + want_i += bucket_count; } if (want_i <= empty_i || want_i > test_i) {