1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:07:35 +00:00

LibJS: Add an explicit DataBlock::size method

This avoids visiting the underlying buffer twice from ArrayBuffer's
byte_length.

On https://cyxx.github.io/another_js, this reduces the runtime of
IsValidIntegerIndex from 9.9% to 8.9%.
This commit is contained in:
Timothy Flynn 2024-02-27 11:00:44 -05:00 committed by Andreas Kling
parent 72cee4c88b
commit 17e2f751c5

View file

@ -42,6 +42,14 @@ struct DataBlock {
}
ByteBuffer const& buffer() const { return const_cast<DataBlock*>(this)->buffer(); }
size_t size() const
{
return byte_buffer.visit(
[](Empty) -> size_t { return 0u; },
[](ByteBuffer const& buffer) { return buffer.size(); },
[](ByteBuffer const* buffer) { return buffer->size(); });
}
Variant<Empty, ByteBuffer, ByteBuffer*> byte_buffer;
Shared is_shared = { Shared::No };
};
@ -57,13 +65,7 @@ public:
virtual ~ArrayBuffer() override = default;
size_t byte_length() const
{
if (is_detached())
return 0;
return m_data_block.buffer().size();
}
size_t byte_length() const { return m_data_block.size(); }
// [[ArrayBufferData]]
ByteBuffer& buffer() { return m_data_block.buffer(); }