From 679bde06edf301e920f2fc3ede3be37a910a3ebd Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Fri, 10 Sep 2021 01:41:40 +0300 Subject: [PATCH] AK: Remove a redundant double find-call in HashMap::ensure If the value was found there's no reason to search for it again. --- AK/HashMap.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/AK/HashMap.h b/AK/HashMap.h index 3eb103abdd..89e6586648 100644 --- a/AK/HashMap.h +++ b/AK/HashMap.h @@ -134,8 +134,10 @@ public: V& ensure(const K& key) { auto it = find(key); - if (it == end()) - set(key, V()); + if (it != end()) + return it->value; + auto result = set(key, V()); + VERIFY(result == HashSetResult::InsertedNewEntry); return find(key)->value; } @@ -143,9 +145,10 @@ public: V& ensure(K const& key, Callback initialization_callback) { auto it = find(key); - if (it == end()) { - set(key, initialization_callback()); - } + if (it != end()) + return it->value; + auto result = set(key, initialization_callback()); + VERIFY(result == HashSetResult::InsertedNewEntry); return find(key)->value; }