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
Notes: sideshowbarker 2024-07-18 12:18:18 +09:00
2 changed files with 6 additions and 8 deletions

View file

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

View file

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