mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK/LEB128: Speed up reading unsigned LEB128 values
Unroll the first byte as a fast path, and remove a branch. This speeds up the instantiation of spidermonkey by 10ms.
This commit is contained in:
parent
fc57f5111d
commit
a6ebd100ec
Notes:
github-actions[bot]
2024-07-27 06:20:41 +00:00
Author: https://github.com/dzfrias Commit: https://github.com/LadybirdBrowser/ladybird/commit/a6ebd100ecd Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/850
1 changed files with 15 additions and 19 deletions
32
AK/LEB128.h
32
AK/LEB128.h
|
@ -27,31 +27,27 @@ public:
|
||||||
static ErrorOr<LEB128<ValueType>> read_from_stream(Stream& stream)
|
static ErrorOr<LEB128<ValueType>> read_from_stream(Stream& stream)
|
||||||
requires(Unsigned<ValueType>)
|
requires(Unsigned<ValueType>)
|
||||||
{
|
{
|
||||||
ValueType result {};
|
// First byte is unrolled for speed
|
||||||
size_t num_bytes = 0;
|
|
||||||
while (true) {
|
|
||||||
if (stream.is_eof())
|
|
||||||
return Error::from_string_literal("Stream reached end-of-file while reading LEB128 value");
|
|
||||||
|
|
||||||
auto byte = TRY(stream.read_value<u8>());
|
auto byte = TRY(stream.read_value<u8>());
|
||||||
|
if ((byte & 0x80) == 0)
|
||||||
|
return LEB128<ValueType> { byte };
|
||||||
|
|
||||||
ValueType masked_byte = byte & ~(1 << 7);
|
ValueType result = byte & 0x7F;
|
||||||
bool const shift_too_large_for_result = num_bytes * 7 > sizeof(ValueType) * 8;
|
size_t num_bytes = 1;
|
||||||
if (shift_too_large_for_result)
|
while (true) {
|
||||||
|
auto byte = TRY(stream.read_value<u8>());
|
||||||
|
ValueType masked = byte & 0x7F;
|
||||||
|
result |= masked << (num_bytes * 7);
|
||||||
|
if (num_bytes * 7 >= sizeof(ValueType) * 8 - 7 && (byte >> (sizeof(ValueType) * 8 - (num_bytes * 7))) != 0) {
|
||||||
|
if ((byte & 0x80) != 0)
|
||||||
return Error::from_string_literal("Read value contains more bits than fit the chosen ValueType");
|
return Error::from_string_literal("Read value contains more bits than fit the chosen ValueType");
|
||||||
|
|
||||||
bool const shift_too_large_for_byte = ((masked_byte << (num_bytes * 7)) >> (num_bytes * 7)) != masked_byte;
|
|
||||||
if (shift_too_large_for_byte)
|
|
||||||
return Error::from_string_literal("Read byte is too large to fit the chosen ValueType");
|
return Error::from_string_literal("Read byte is too large to fit the chosen ValueType");
|
||||||
|
|
||||||
result = (result) | (masked_byte << (num_bytes * 7));
|
|
||||||
if (!(byte & (1 << 7)))
|
|
||||||
break;
|
|
||||||
++num_bytes;
|
|
||||||
}
|
}
|
||||||
|
++num_bytes;
|
||||||
|
if ((byte & 0x80) == 0)
|
||||||
return LEB128<ValueType> { result };
|
return LEB128<ValueType> { result };
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static ErrorOr<LEB128<ValueType>> read_from_stream(Stream& stream)
|
static ErrorOr<LEB128<ValueType>> read_from_stream(Stream& stream)
|
||||||
requires(Signed<ValueType>)
|
requires(Signed<ValueType>)
|
||||||
|
|
Loading…
Reference in a new issue