1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:17:34 +00:00

LibJS: Make StringOrSymbol always be FlyString in the string case

This makes equality checking O(1) instead of O(n).
This commit is contained in:
Andreas Kling 2021-06-13 12:00:27 +02:00
parent cd12b182ca
commit 53a8a11973
2 changed files with 6 additions and 8 deletions

View file

@ -18,15 +18,13 @@ public:
StringOrSymbol() = default; StringOrSymbol() = default;
StringOrSymbol(char const* chars) StringOrSymbol(char const* chars)
: m_ptr(StringImpl::create(chars).leak_ref()) : StringOrSymbol(FlyString(chars))
{ {
} }
StringOrSymbol(String const& string) StringOrSymbol(String const& string)
: m_ptr(string.impl()) : StringOrSymbol(FlyString(string))
{ {
VERIFY(!string.is_null());
as_string_impl().ref();
} }
StringOrSymbol(FlyString const& string) StringOrSymbol(FlyString const& string)
@ -64,10 +62,10 @@ public:
ALWAYS_INLINE bool is_symbol() const { return is_valid() && (bits() & 1ul); } ALWAYS_INLINE bool is_symbol() const { return is_valid() && (bits() & 1ul); }
ALWAYS_INLINE bool is_string() 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()); VERIFY(is_string());
return as_string_impl(); return FlyString::from_fly_impl(as_string_impl());
} }
ALWAYS_INLINE Symbol const* as_symbol() const ALWAYS_INLINE Symbol const* as_symbol() const
@ -103,7 +101,7 @@ public:
ALWAYS_INLINE bool operator==(StringOrSymbol const& other) const ALWAYS_INLINE bool operator==(StringOrSymbol const& other) const
{ {
if (is_string()) 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()) if (is_symbol())
return other.is_symbol() && as_symbol() == other.as_symbol(); return other.is_symbol() && as_symbol() == other.as_symbol();
return true; return true;

View file

@ -1003,7 +1003,7 @@ int main(int argc, char** argv)
if (key.view().starts_with(property_pattern)) { if (key.view().starts_with(property_pattern)) {
Line::CompletionSuggestion completion { key, Line::CompletionSuggestion::ForSearch }; Line::CompletionSuggestion completion { key, Line::CompletionSuggestion::ForSearch };
if (!results.contains_slow(completion)) { // hide duplicates if (!results.contains_slow(completion)) { // hide duplicates
results.append(key); results.append(String(key));
} }
} }
} }