Skip to content

Commit

Permalink
AK: Remove a redundant double find-call in HashMap::ensure
Browse files Browse the repository at this point in the history
If the value was found there's no reason to search for it again.
  • Loading branch information
IdanHo committed Sep 10, 2021
1 parent 706323b commit 679bde0
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions AK/HashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,21 @@ class HashMap {
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;
}

template<typename Callback>
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;
}

Expand Down

0 comments on commit 679bde0

Please sign in to comment.