mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-30 04:39:06 +00:00
LibJS: Make Optional<Utf16String> use less space
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
By specializing the template, we can shrink it from 16 to 8 bytes. This makes PrimitiveString a measly 32 bytes. :^)
This commit is contained in:
parent
152691f9eb
commit
4dc63ddf49
Notes:
github-actions[bot]
2025-03-30 06:17:42 +00:00
Author: https://github.com/awesomekling
Commit: 4dc63ddf49
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4149
Reviewed-by: https://github.com/Hendiadyoin1
Reviewed-by: https://github.com/tcl3 ✅
2 changed files with 106 additions and 1 deletions
|
@ -84,6 +84,12 @@ Utf16String Utf16String::create(Utf16View const& string)
|
||||||
return Utf16String { Detail::Utf16StringImpl::create(string) };
|
return Utf16String { Detail::Utf16StringImpl::create(string) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Utf16String Utf16String::invalid()
|
||||||
|
{
|
||||||
|
static auto invalid = Utf16String {};
|
||||||
|
return invalid;
|
||||||
|
}
|
||||||
|
|
||||||
Utf16String::Utf16String(NonnullRefPtr<Detail::Utf16StringImpl> string)
|
Utf16String::Utf16String(NonnullRefPtr<Detail::Utf16StringImpl> string)
|
||||||
: m_string(move(string))
|
: m_string(move(string))
|
||||||
{
|
{
|
||||||
|
|
|
@ -58,6 +58,7 @@ public:
|
||||||
[[nodiscard]] static Utf16String create(Utf16Data);
|
[[nodiscard]] static Utf16String create(Utf16Data);
|
||||||
[[nodiscard]] static Utf16String create(StringView);
|
[[nodiscard]] static Utf16String create(StringView);
|
||||||
[[nodiscard]] static Utf16String create(Utf16View const&);
|
[[nodiscard]] static Utf16String create(Utf16View const&);
|
||||||
|
[[nodiscard]] static Utf16String invalid();
|
||||||
|
|
||||||
Utf16Data const& string() const;
|
Utf16Data const& string() const;
|
||||||
Utf16View view() const;
|
Utf16View view() const;
|
||||||
|
@ -70,6 +71,7 @@ public:
|
||||||
|
|
||||||
size_t length_in_code_units() const;
|
size_t length_in_code_units() const;
|
||||||
bool is_empty() const;
|
bool is_empty() const;
|
||||||
|
bool is_valid() const { return m_string; }
|
||||||
|
|
||||||
[[nodiscard]] u32 hash() const { return m_string->hash(); }
|
[[nodiscard]] u32 hash() const { return m_string->hash(); }
|
||||||
[[nodiscard]] bool operator==(Utf16String const& other) const
|
[[nodiscard]] bool operator==(Utf16String const& other) const
|
||||||
|
@ -80,9 +82,10 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Utf16String() = default;
|
||||||
explicit Utf16String(NonnullRefPtr<Detail::Utf16StringImpl>);
|
explicit Utf16String(NonnullRefPtr<Detail::Utf16StringImpl>);
|
||||||
|
|
||||||
NonnullRefPtr<Detail::Utf16StringImpl> m_string;
|
RefPtr<Detail::Utf16StringImpl> m_string;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -94,4 +97,100 @@ struct Traits<JS::Utf16String> : public DefaultTraits<JS::Utf16String> {
|
||||||
static unsigned hash(JS::Utf16String const& s) { return s.hash(); }
|
static unsigned hash(JS::Utf16String const& s) { return s.hash(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
class Optional<JS::Utf16String> : public OptionalBase<JS::Utf16String> {
|
||||||
|
template<typename U>
|
||||||
|
friend class Optional;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using ValueType = JS::Utf16String;
|
||||||
|
|
||||||
|
Optional() = default;
|
||||||
|
|
||||||
|
template<SameAs<OptionalNone> V>
|
||||||
|
Optional(V) { }
|
||||||
|
|
||||||
|
Optional(Optional<JS::Utf16String> const& other)
|
||||||
|
{
|
||||||
|
if (other.has_value())
|
||||||
|
m_value = other.m_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional(Optional&& other)
|
||||||
|
: m_value(other.m_value)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename U = JS::Utf16String>
|
||||||
|
requires(!IsSame<OptionalNone, RemoveCVReference<U>>)
|
||||||
|
explicit(!IsConvertible<U&&, JS::Utf16String>) Optional(U&& value)
|
||||||
|
requires(!IsSame<RemoveCVReference<U>, Optional<JS::Utf16String>> && IsConstructible<JS::Utf16String, U &&>)
|
||||||
|
: m_value(forward<U>(value))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<SameAs<OptionalNone> V>
|
||||||
|
Optional& operator=(V)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional& operator=(Optional const& other)
|
||||||
|
{
|
||||||
|
if (this != &other) {
|
||||||
|
clear();
|
||||||
|
m_value = other.m_value;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional& operator=(Optional&& other)
|
||||||
|
{
|
||||||
|
if (this != &other) {
|
||||||
|
clear();
|
||||||
|
m_value = other.m_value;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear()
|
||||||
|
{
|
||||||
|
m_value = JS::Utf16String::invalid();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] bool has_value() const
|
||||||
|
{
|
||||||
|
return m_value.is_valid();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] JS::Utf16String& value() &
|
||||||
|
{
|
||||||
|
VERIFY(has_value());
|
||||||
|
return m_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] JS::Utf16String const& value() const&
|
||||||
|
{
|
||||||
|
VERIFY(has_value());
|
||||||
|
return m_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] JS::Utf16String value() &&
|
||||||
|
{
|
||||||
|
return release_value();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] JS::Utf16String release_value()
|
||||||
|
{
|
||||||
|
VERIFY(has_value());
|
||||||
|
JS::Utf16String released_value = m_value;
|
||||||
|
clear();
|
||||||
|
return released_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
JS::Utf16String m_value { JS::Utf16String::invalid() };
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue