mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-04 16:46:08 +00:00
LibWasm: Avoid memory copy in read_value<T>() if possible
If the address is already aligned properly, just read a T from it; otherwise copy it to a local aligned array. This was a bottleneck on memory-heavy benchmarks.
This commit is contained in:
parent
5c6f223f48
commit
bd7c188b86
Notes:
github-actions[bot]
2025-08-08 10:57:15 +00:00
Author: https://github.com/alimpfard
Commit: bd7c188b86
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5060
Reviewed-by: https://github.com/Hendiadyoin1
Reviewed-by: https://github.com/R-Goc
Reviewed-by: https://github.com/awesomekling
Reviewed-by: https://github.com/gmta
1 changed files with 8 additions and 18 deletions
|
@ -370,35 +370,25 @@ void BytecodeInterpreter::store_to_memory(Configuration& configuration, Instruct
|
|||
template<typename T>
|
||||
T BytecodeInterpreter::read_value(ReadonlyBytes data)
|
||||
{
|
||||
FixedMemoryStream stream { data };
|
||||
auto value_or_error = stream.read_value<LittleEndian<T>>();
|
||||
if (value_or_error.is_error()) {
|
||||
dbgln("Read from {} failed", data.data());
|
||||
m_trap = Trap::from_string("Read from memory failed");
|
||||
VERIFY(sizeof(T) <= data.size());
|
||||
if (bit_cast<FlatPtr>(data.data()) % alignof(T)) {
|
||||
alignas(T) u8 buf[sizeof(T)];
|
||||
memcpy(buf, data.data(), sizeof(T));
|
||||
return bit_cast<LittleEndian<T>>(buf);
|
||||
}
|
||||
return value_or_error.release_value();
|
||||
return *bit_cast<LittleEndian<T> const*>(data.data());
|
||||
}
|
||||
|
||||
template<>
|
||||
float BytecodeInterpreter::read_value<float>(ReadonlyBytes data)
|
||||
{
|
||||
FixedMemoryStream stream { data };
|
||||
auto raw_value_or_error = stream.read_value<LittleEndian<u32>>();
|
||||
if (raw_value_or_error.is_error())
|
||||
m_trap = Trap::from_string("Read from memory failed");
|
||||
auto raw_value = raw_value_or_error.release_value();
|
||||
return bit_cast<float>(static_cast<u32>(raw_value));
|
||||
return bit_cast<float>(read_value<u32>(data));
|
||||
}
|
||||
|
||||
template<>
|
||||
double BytecodeInterpreter::read_value<double>(ReadonlyBytes data)
|
||||
{
|
||||
FixedMemoryStream stream { data };
|
||||
auto raw_value_or_error = stream.read_value<LittleEndian<u64>>();
|
||||
if (raw_value_or_error.is_error())
|
||||
m_trap = Trap::from_string("Read from memory failed");
|
||||
auto raw_value = raw_value_or_error.release_value();
|
||||
return bit_cast<double>(static_cast<u64>(raw_value));
|
||||
return bit_cast<double>(read_value<u64>(data));
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void BytecodeInterpreter::interpret_instruction(Configuration& configuration, InstructionPointer& ip, Instruction const& instruction)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue