mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 03:55:24 +00:00
LibJS: Make Optional<IdentifierTableIndex> use less space
We can use the index's invalid state to signal an empty optional. This makes Optional<IdentifierTableIndex> 4 bytes instead of 8, shrinking every bytecode instruction that uses these.
This commit is contained in:
parent
904beef6d2
commit
a29d820f87
Notes:
github-actions[bot]
2025-03-27 19:51:23 +00:00
Author: https://github.com/awesomekling Commit: https://github.com/LadybirdBrowser/ladybird/commit/a29d820f87c Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4122 Reviewed-by: https://github.com/LucasChollet
1 changed files with 101 additions and 1 deletions
|
@ -13,7 +13,8 @@
|
|||
namespace JS::Bytecode {
|
||||
|
||||
struct IdentifierTableIndex {
|
||||
bool is_valid() const { return value != NumericLimits<u32>::max(); }
|
||||
static constexpr u32 invalid = 0xffffffffu;
|
||||
bool is_valid() const { return value != invalid; }
|
||||
u32 value { 0 };
|
||||
};
|
||||
|
||||
|
@ -34,3 +35,102 @@ private:
|
|||
};
|
||||
|
||||
}
|
||||
|
||||
namespace AK {
|
||||
template<>
|
||||
class Optional<JS::Bytecode::IdentifierTableIndex> : public OptionalBase<JS::Bytecode::IdentifierTableIndex> {
|
||||
template<typename U>
|
||||
friend class Optional;
|
||||
|
||||
public:
|
||||
using ValueType = JS::Bytecode::IdentifierTableIndex;
|
||||
|
||||
Optional() = default;
|
||||
|
||||
template<SameAs<OptionalNone> V>
|
||||
Optional(V) { }
|
||||
|
||||
Optional(Optional<JS::Bytecode::IdentifierTableIndex> const& other)
|
||||
{
|
||||
if (other.has_value())
|
||||
m_value = other.m_value;
|
||||
}
|
||||
|
||||
Optional(Optional&& other)
|
||||
: m_value(other.m_value)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U = JS::Bytecode::IdentifierTableIndex>
|
||||
requires(!IsSame<OptionalNone, RemoveCVReference<U>>)
|
||||
explicit(!IsConvertible<U&&, JS::Bytecode::IdentifierTableIndex>) Optional(U&& value)
|
||||
requires(!IsSame<RemoveCVReference<U>, Optional<JS::Bytecode::IdentifierTableIndex>> && IsConstructible<JS::Bytecode::IdentifierTableIndex, 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::IdentifierTableIndex::invalid;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool has_value() const
|
||||
{
|
||||
return m_value.is_valid();
|
||||
}
|
||||
|
||||
[[nodiscard]] JS::Bytecode::IdentifierTableIndex& value() &
|
||||
{
|
||||
VERIFY(has_value());
|
||||
return m_value;
|
||||
}
|
||||
|
||||
[[nodiscard]] JS::Bytecode::IdentifierTableIndex const& value() const&
|
||||
{
|
||||
VERIFY(has_value());
|
||||
return m_value;
|
||||
}
|
||||
|
||||
[[nodiscard]] JS::Bytecode::IdentifierTableIndex value() &&
|
||||
{
|
||||
return release_value();
|
||||
}
|
||||
|
||||
[[nodiscard]] JS::Bytecode::IdentifierTableIndex release_value()
|
||||
{
|
||||
VERIFY(has_value());
|
||||
JS::Bytecode::IdentifierTableIndex released_value = m_value;
|
||||
clear();
|
||||
return released_value;
|
||||
}
|
||||
|
||||
private:
|
||||
JS::Bytecode::IdentifierTableIndex m_value { JS::Bytecode::IdentifierTableIndex::invalid };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue