mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +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:
parent
706323beb1
commit
679bde06ed
Notes:
sideshowbarker
2024-07-18 04:20:50 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/679bde06edf Pull-request: https://github.com/SerenityOS/serenity/pull/9922 Reviewed-by: https://github.com/bgianfo ✅
1 changed files with 8 additions and 5 deletions
13
AK/HashMap.h
13
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue