1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:07:45 +00:00

AK: Remove a redundant double find-call in HashMap::ensure

If the value was found there's no reason to search for it again.
This commit is contained in:
Idan Horowitz 2021-09-10 01:41:40 +03:00
parent 706323beb1
commit 679bde06ed

View file

@ -134,8 +134,10 @@ public:
V& ensure(const K& key) V& ensure(const K& key)
{ {
auto it = find(key); auto it = find(key);
if (it == end()) if (it != end())
set(key, V()); return it->value;
auto result = set(key, V());
VERIFY(result == HashSetResult::InsertedNewEntry);
return find(key)->value; return find(key)->value;
} }
@ -143,9 +145,10 @@ public:
V& ensure(K const& key, Callback initialization_callback) V& ensure(K const& key, Callback initialization_callback)
{ {
auto it = find(key); auto it = find(key);
if (it == end()) { if (it != end())
set(key, initialization_callback()); return it->value;
} auto result = set(key, initialization_callback());
VERIFY(result == HashSetResult::InsertedNewEntry);
return find(key)->value; return find(key)->value;
} }