AK: Add SentinelOptional

We specialize `Optional<T>` for value types that inherently support some
kind of "empty" value or whose value range allow for a unlikely to be
useful sentinel value that can mean "empty", instead of the boolean flag
a regular Optional<T> needs to store. Because of padding, this often
means saving 4 to 8 bytes per instance.

By extending the new `SentinelOptional<T, Traits>`, these
specializations are significantly simplified to just having to define
what the sentinel value is, and how to identify a sentinel value.
This commit is contained in:
Jelle Raaijmakers
2026-03-18 20:43:56 +01:00
committed by Jelle Raaijmakers
parent 9e245b014c
commit e123d48043
Notes: github-actions[bot] 2026-03-20 11:04:58 +00:00
13 changed files with 215 additions and 988 deletions

View File

@@ -47,99 +47,15 @@ private:
namespace AK {
template<>
class Optional<JS::Bytecode::PropertyKeyTableIndex> : public OptionalBase<JS::Bytecode::PropertyKeyTableIndex> {
template<typename U>
friend class Optional;
struct SentinelOptionalTraits<JS::Bytecode::PropertyKeyTableIndex> {
static constexpr JS::Bytecode::PropertyKeyTableIndex sentinel_value() { return { JS::Bytecode::PropertyKeyTableIndex::invalid }; }
static constexpr bool is_sentinel(JS::Bytecode::PropertyKeyTableIndex const& value) { return !value.is_valid(); }
};
template<>
class Optional<JS::Bytecode::PropertyKeyTableIndex> : public SentinelOptional<JS::Bytecode::PropertyKeyTableIndex> {
public:
using ValueType = JS::Bytecode::PropertyKeyTableIndex;
Optional() = default;
template<SameAs<OptionalNone> V>
Optional(V) { }
Optional(Optional<JS::Bytecode::PropertyKeyTableIndex> const& other)
{
if (other.has_value())
m_value = other.m_value;
}
Optional(Optional&& other)
: m_value(other.m_value)
{
}
template<typename U = JS::Bytecode::PropertyKeyTableIndex>
requires(!IsSame<OptionalNone, RemoveCVReference<U>>)
explicit(!IsConvertible<U&&, JS::Bytecode::PropertyKeyTableIndex>) Optional(U&& value)
requires(!IsSame<RemoveCVReference<U>, Optional<JS::Bytecode::PropertyKeyTableIndex>> && IsConstructible<JS::Bytecode::PropertyKeyTableIndex, 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.value = JS::Bytecode::PropertyKeyTableIndex::invalid;
}
[[nodiscard]] bool has_value() const
{
return m_value.is_valid();
}
[[nodiscard]] JS::Bytecode::PropertyKeyTableIndex& value() &
{
VERIFY(has_value());
return m_value;
}
[[nodiscard]] JS::Bytecode::PropertyKeyTableIndex const& value() const&
{
VERIFY(has_value());
return m_value;
}
[[nodiscard]] JS::Bytecode::PropertyKeyTableIndex value() &&
{
return release_value();
}
[[nodiscard]] JS::Bytecode::PropertyKeyTableIndex release_value()
{
VERIFY(has_value());
JS::Bytecode::PropertyKeyTableIndex released_value = m_value;
clear();
return released_value;
}
private:
JS::Bytecode::PropertyKeyTableIndex m_value { JS::Bytecode::PropertyKeyTableIndex::invalid };
using SentinelOptional::SentinelOptional;
};
}