mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
AK: Try to avoid String allocation in FlyString(StringView)
If we're constructing a FlyString from a StringView, and we already have a matching StringImpl in the table, use HashTable::find() to locate the existing string without creating a temporary String.
This commit is contained in:
parent
0f35dfc694
commit
a4099fc756
Notes:
sideshowbarker
2024-07-18 18:06:58 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/a4099fc7564
1 changed files with 15 additions and 2 deletions
|
@ -55,9 +55,22 @@ FlyString::FlyString(const String& string)
|
|||
}
|
||||
}
|
||||
|
||||
FlyString::FlyString(const StringView& string)
|
||||
: FlyString(static_cast<String>(string))
|
||||
FlyString::FlyString(StringView const& string)
|
||||
{
|
||||
if (string.is_null())
|
||||
return;
|
||||
auto it = fly_impls().find(string.hash(), [&](auto& candidate) {
|
||||
return string == candidate;
|
||||
});
|
||||
if (it == fly_impls().end()) {
|
||||
auto new_string = string.to_string();
|
||||
fly_impls().set(new_string.impl());
|
||||
new_string.impl()->set_fly({}, true);
|
||||
m_impl = new_string.impl();
|
||||
} else {
|
||||
VERIFY((*it)->is_fly());
|
||||
m_impl = *it;
|
||||
}
|
||||
}
|
||||
|
||||
FlyString::FlyString(const char* string)
|
||||
|
|
Loading…
Reference in a new issue