summaryrefslogtreecommitdiff
path: root/AK/FlyString.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-05-15 09:50:51 +0200
committerAndreas Kling <kling@serenityos.org>2021-05-15 11:01:05 +0200
commita4099fc7564d7a4030f16fcacd5cfd542a593e56 (patch)
tree13857eb7f29e48601868ea30e851d339c6a47e0d /AK/FlyString.cpp
parent0f35dfc69468e292c92259e1d694e004999b3f06 (diff)
downloadserenity-a4099fc7564d7a4030f16fcacd5cfd542a593e56.zip
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.
Diffstat (limited to 'AK/FlyString.cpp')
-rw-r--r--AK/FlyString.cpp17
1 files changed, 15 insertions, 2 deletions
diff --git a/AK/FlyString.cpp b/AK/FlyString.cpp
index 128abcc6f7..8acc94d67a 100644
--- a/AK/FlyString.cpp
+++ b/AK/FlyString.cpp
@@ -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)