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:
Andreas Kling 2021-05-15 09:50:51 +02:00
parent 0f35dfc694
commit a4099fc756

View file

@ -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)