mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-07 00:29:15 +00:00
LibJS: Store bytecode VM program counter in ExecutionContext
This way it's always automatically correct, and we don't have to manually flush it in push_execution_context(). ~7% speedup on the MicroBench/call* tests :^)
This commit is contained in:
parent
e7ae9c8ebf
commit
4d17707b26
Notes:
github-actions[bot]
2025-04-28 19:13:46 +00:00
Author: https://github.com/awesomekling
Commit: 4d17707b26
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4509
Reviewed-by: https://github.com/gmta ✅
6 changed files with 22 additions and 33 deletions
|
@ -381,9 +381,8 @@ FLATTEN_ON_CLANG void Interpreter::run_bytecode(size_t entry_point)
|
||||||
auto& executable = current_executable();
|
auto& executable = current_executable();
|
||||||
auto const* bytecode = executable.bytecode.data();
|
auto const* bytecode = executable.bytecode.data();
|
||||||
|
|
||||||
size_t program_counter = entry_point;
|
size_t& program_counter = running_execution_context.program_counter;
|
||||||
|
program_counter = entry_point;
|
||||||
TemporaryChange change(m_program_counter, Optional<size_t&>(program_counter));
|
|
||||||
|
|
||||||
// Declare a lookup table for computed goto with each of the `handle_*` labels
|
// Declare a lookup table for computed goto with each of the `handle_*` labels
|
||||||
// to avoid the overhead of a switch statement.
|
// to avoid the overhead of a switch statement.
|
||||||
|
|
|
@ -76,7 +76,6 @@ public:
|
||||||
|
|
||||||
Executable& current_executable() { return *m_current_executable; }
|
Executable& current_executable() { return *m_current_executable; }
|
||||||
Executable const& current_executable() const { return *m_current_executable; }
|
Executable const& current_executable() const { return *m_current_executable; }
|
||||||
Optional<size_t> program_counter() const { return m_program_counter; }
|
|
||||||
Span<Value> allocate_argument_values(size_t argument_count)
|
Span<Value> allocate_argument_values(size_t argument_count)
|
||||||
{
|
{
|
||||||
m_argument_values_buffer.resize_and_keep_capacity(argument_count);
|
m_argument_values_buffer.resize_and_keep_capacity(argument_count);
|
||||||
|
@ -100,7 +99,6 @@ private:
|
||||||
GC::Ptr<Realm> m_realm { nullptr };
|
GC::Ptr<Realm> m_realm { nullptr };
|
||||||
GC::Ptr<Object> m_global_object { nullptr };
|
GC::Ptr<Object> m_global_object { nullptr };
|
||||||
GC::Ptr<DeclarativeEnvironment> m_global_declarative_environment { nullptr };
|
GC::Ptr<DeclarativeEnvironment> m_global_declarative_environment { nullptr };
|
||||||
Optional<size_t&> m_program_counter;
|
|
||||||
Span<Value> m_registers_and_constants_and_locals_arguments;
|
Span<Value> m_registers_and_constants_and_locals_arguments;
|
||||||
Vector<Value> m_argument_values_buffer;
|
Vector<Value> m_argument_values_buffer;
|
||||||
ExecutionContext* m_running_execution_context { nullptr };
|
ExecutionContext* m_running_execution_context { nullptr };
|
||||||
|
|
|
@ -58,7 +58,7 @@ public:
|
||||||
// Non-standard: This points at something that owns this ExecutionContext, in case it needs to be protected from GC.
|
// Non-standard: This points at something that owns this ExecutionContext, in case it needs to be protected from GC.
|
||||||
GC::Ptr<Cell> context_owner;
|
GC::Ptr<Cell> context_owner;
|
||||||
|
|
||||||
Optional<size_t> program_counter;
|
size_t program_counter { 0 };
|
||||||
|
|
||||||
mutable RefPtr<CachedSourceRange> cached_source_range;
|
mutable RefPtr<CachedSourceRange> cached_source_range;
|
||||||
|
|
||||||
|
|
|
@ -480,8 +480,8 @@ void VM::dump_backtrace() const
|
||||||
{
|
{
|
||||||
for (ssize_t i = m_execution_context_stack.size() - 1; i >= 0; --i) {
|
for (ssize_t i = m_execution_context_stack.size() - 1; i >= 0; --i) {
|
||||||
auto& frame = m_execution_context_stack[i];
|
auto& frame = m_execution_context_stack[i];
|
||||||
if (frame->executable && frame->program_counter.has_value()) {
|
if (frame->executable) {
|
||||||
auto source_range = frame->executable->source_range_at(frame->program_counter.value()).realize();
|
auto source_range = frame->executable->source_range_at(frame->program_counter).realize();
|
||||||
dbgln("-> {} @ {}:{},{}", frame->function_name ? frame->function_name->utf8_string() : ""_string, source_range.filename(), source_range.start.line, source_range.start.column);
|
dbgln("-> {} @ {}:{},{}", frame->function_name ? frame->function_name->utf8_string() : ""_string, source_range.filename(), source_range.start.line, source_range.start.column);
|
||||||
} else {
|
} else {
|
||||||
dbgln("-> {}", frame->function_name ? frame->function_name->utf8_string() : ""_string);
|
dbgln("-> {}", frame->function_name ? frame->function_name->utf8_string() : ""_string);
|
||||||
|
@ -746,34 +746,17 @@ void VM::load_imported_module(ImportedModuleReferrer referrer, ModuleRequest con
|
||||||
finish_loading_imported_module(referrer, module_request, payload, module);
|
finish_loading_imported_module(referrer, module_request, payload, module);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VM::push_execution_context(ExecutionContext& context)
|
|
||||||
{
|
|
||||||
if (!m_execution_context_stack.is_empty())
|
|
||||||
m_execution_context_stack.last()->program_counter = bytecode_interpreter().program_counter();
|
|
||||||
m_execution_context_stack.append(&context);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VM::pop_execution_context()
|
|
||||||
{
|
|
||||||
m_execution_context_stack.take_last();
|
|
||||||
if (m_execution_context_stack.is_empty() && on_call_stack_emptied)
|
|
||||||
on_call_stack_emptied();
|
|
||||||
}
|
|
||||||
|
|
||||||
static RefPtr<CachedSourceRange> get_source_range(ExecutionContext const* context)
|
static RefPtr<CachedSourceRange> get_source_range(ExecutionContext const* context)
|
||||||
{
|
{
|
||||||
// native function
|
// native function
|
||||||
if (!context->executable)
|
if (!context->executable)
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
if (!context->program_counter.has_value())
|
|
||||||
return {};
|
|
||||||
|
|
||||||
if (!context->cached_source_range
|
if (!context->cached_source_range
|
||||||
|| context->cached_source_range->program_counter != context->program_counter.value()) {
|
|| context->cached_source_range->program_counter != context->program_counter) {
|
||||||
auto unrealized_source_range = context->executable->source_range_at(context->program_counter.value());
|
auto unrealized_source_range = context->executable->source_range_at(context->program_counter);
|
||||||
context->cached_source_range = adopt_ref(*new CachedSourceRange(
|
context->cached_source_range = adopt_ref(*new CachedSourceRange(
|
||||||
context->program_counter.value(),
|
context->program_counter,
|
||||||
move(unrealized_source_range)));
|
move(unrealized_source_range)));
|
||||||
}
|
}
|
||||||
return context->cached_source_range;
|
return context->cached_source_range;
|
||||||
|
|
|
@ -115,12 +115,21 @@ public:
|
||||||
if (did_reach_stack_space_limit()) [[unlikely]] {
|
if (did_reach_stack_space_limit()) [[unlikely]] {
|
||||||
return throw_completion<InternalError>(ErrorType::CallStackSizeExceeded);
|
return throw_completion<InternalError>(ErrorType::CallStackSizeExceeded);
|
||||||
}
|
}
|
||||||
push_execution_context(context);
|
m_execution_context_stack.append(&context);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void push_execution_context(ExecutionContext&);
|
void push_execution_context(ExecutionContext& context)
|
||||||
void pop_execution_context();
|
{
|
||||||
|
m_execution_context_stack.append(&context);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pop_execution_context()
|
||||||
|
{
|
||||||
|
m_execution_context_stack.take_last();
|
||||||
|
if (m_execution_context_stack.is_empty() && on_call_stack_emptied)
|
||||||
|
on_call_stack_emptied();
|
||||||
|
}
|
||||||
|
|
||||||
// https://tc39.es/ecma262/#running-execution-context
|
// https://tc39.es/ecma262/#running-execution-context
|
||||||
// At any point in time, there is at most one execution context per agent that is actually executing code.
|
// At any point in time, there is at most one execution context per agent that is actually executing code.
|
||||||
|
|
|
@ -913,8 +913,8 @@ static ErrorInformation extract_error_information(JS::VM& vm, JS::Value exceptio
|
||||||
else {
|
else {
|
||||||
for (ssize_t i = vm.execution_context_stack().size() - 1; i >= 0; --i) {
|
for (ssize_t i = vm.execution_context_stack().size() - 1; i >= 0; --i) {
|
||||||
auto& frame = vm.execution_context_stack()[i];
|
auto& frame = vm.execution_context_stack()[i];
|
||||||
if (frame->executable && frame->program_counter.has_value()) {
|
if (frame->executable) {
|
||||||
auto source_range = frame->executable->source_range_at(frame->program_counter.value()).realize();
|
auto source_range = frame->executable->source_range_at(frame->program_counter).realize();
|
||||||
attributes.filename = MUST(String::from_byte_string(source_range.filename()));
|
attributes.filename = MUST(String::from_byte_string(source_range.filename()));
|
||||||
attributes.lineno = source_range.start.line;
|
attributes.lineno = source_range.start.line;
|
||||||
attributes.colno = source_range.start.column;
|
attributes.colno = source_range.start.column;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue