summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-09-10 01:41:40 +0300
committerIdan Horowitz <idan.horowitz@gmail.com>2021-09-10 15:26:41 +0300
commit679bde06edf301e920f2fc3ede3be37a910a3ebd (patch)
tree26f4c5b4066eb04e14f0bb95db006115bcfad05e /AK
parent706323beb1d336f57c84e9d04ca12d541a82dcd8 (diff)
downloadserenity-679bde06edf301e920f2fc3ede3be37a910a3ebd.zip
AK: Remove a redundant double find-call in HashMap::ensure
If the value was found there's no reason to search for it again.
Diffstat (limited to 'AK')
-rw-r--r--AK/HashMap.h13
1 files 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;
}