summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-06-13 12:00:27 +0200
committerAndreas Kling <kling@serenityos.org>2021-06-13 19:11:29 +0200
commit53a8a11973cb4be1dc688fc245775540630bf87d (patch)
treef2b5d952293b80553998251155fe902c5e3fdae1
parentcd12b182ca54627ec708fa303fdae5b8b7a20392 (diff)
downloadserenity-53a8a11973cb4be1dc688fc245775540630bf87d.zip
LibJS: Make StringOrSymbol always be FlyString in the string case
This makes equality checking O(1) instead of O(n).
-rw-r--r--Userland/Libraries/LibJS/Runtime/StringOrSymbol.h12
-rw-r--r--Userland/Utilities/js.cpp2
2 files changed, 6 insertions, 8 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/StringOrSymbol.h b/Userland/Libraries/LibJS/Runtime/StringOrSymbol.h
index 3ee3513efc..a8f5ae6b2a 100644
--- a/Userland/Libraries/LibJS/Runtime/StringOrSymbol.h
+++ b/Userland/Libraries/LibJS/Runtime/StringOrSymbol.h
@@ -18,15 +18,13 @@ public:
StringOrSymbol() = default;
StringOrSymbol(char const* chars)
- : m_ptr(StringImpl::create(chars).leak_ref())
+ : StringOrSymbol(FlyString(chars))
{
}
StringOrSymbol(String const& string)
- : m_ptr(string.impl())
+ : StringOrSymbol(FlyString(string))
{
- VERIFY(!string.is_null());
- as_string_impl().ref();
}
StringOrSymbol(FlyString const& string)
@@ -64,10 +62,10 @@ public:
ALWAYS_INLINE bool is_symbol() const { return is_valid() && (bits() & 1ul); }
ALWAYS_INLINE bool is_string() const { return is_valid() && !(bits() & 1ul); }
- ALWAYS_INLINE String as_string() const
+ ALWAYS_INLINE FlyString as_string() const
{
VERIFY(is_string());
- return as_string_impl();
+ return FlyString::from_fly_impl(as_string_impl());
}
ALWAYS_INLINE Symbol const* as_symbol() const
@@ -103,7 +101,7 @@ public:
ALWAYS_INLINE bool operator==(StringOrSymbol const& other) const
{
if (is_string())
- return other.is_string() && as_string_impl() == other.as_string_impl();
+ return other.is_string() && &as_string_impl() == &other.as_string_impl();
if (is_symbol())
return other.is_symbol() && as_symbol() == other.as_symbol();
return true;
diff --git a/Userland/Utilities/js.cpp b/Userland/Utilities/js.cpp
index 7abce34589..971ebf4cbb 100644
--- a/Userland/Utilities/js.cpp
+++ b/Userland/Utilities/js.cpp
@@ -1003,7 +1003,7 @@ int main(int argc, char** argv)
if (key.view().starts_with(property_pattern)) {
Line::CompletionSuggestion completion { key, Line::CompletionSuggestion::ForSearch };
if (!results.contains_slow(completion)) { // hide duplicates
- results.append(key);
+ results.append(String(key));
}
}
}