mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 07:44:59 +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
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…
Add table
Add a link
Reference in a new issue