mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-27 06:48:49 +00:00
LibJS: Let GetGlobal cache module environment lookups when possible
This commit is contained in:
parent
8fcff2fa18
commit
37c7eb14fe
Notes:
github-actions[bot]
2025-03-20 17:52:50 +00:00
Author: https://github.com/awesomekling
Commit: 37c7eb14fe
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4017
2 changed files with 20 additions and 5 deletions
|
@ -1125,8 +1125,13 @@ inline ThrowCompletionOr<Value> get_global(Interpreter& interpreter, IdentifierT
|
|||
|
||||
// OPTIMIZATION: For global lexical bindings, if the global declarative environment hasn't changed,
|
||||
// we can use the cached environment binding index.
|
||||
if (cache.environment_binding_index.has_value())
|
||||
return declarative_record.get_binding_value_direct(vm, cache.environment_binding_index.value());
|
||||
if (cache.has_environment_binding_index) {
|
||||
if (cache.in_module_environment) {
|
||||
auto module = vm.running_execution_context().script_or_module.get_pointer<GC::Ref<Module>>();
|
||||
return (*module)->environment()->get_binding_value_direct(vm, cache.environment_binding_index);
|
||||
}
|
||||
return declarative_record.get_binding_value_direct(vm, cache.environment_binding_index);
|
||||
}
|
||||
}
|
||||
|
||||
cache.environment_serial_number = declarative_record.environment_serial_number();
|
||||
|
@ -1137,8 +1142,14 @@ inline ThrowCompletionOr<Value> get_global(Interpreter& interpreter, IdentifierT
|
|||
// NOTE: GetGlobal is used to access variables stored in the module environment and global environment.
|
||||
// The module environment is checked first since it precedes the global environment in the environment chain.
|
||||
auto& module_environment = *(*module)->environment();
|
||||
if (TRY(module_environment.has_binding(identifier))) {
|
||||
// TODO: Cache offset of binding value
|
||||
Optional<size_t> index;
|
||||
if (TRY(module_environment.has_binding(identifier, &index))) {
|
||||
if (index.has_value()) {
|
||||
cache.environment_binding_index = static_cast<u32>(index.value());
|
||||
cache.has_environment_binding_index = true;
|
||||
cache.in_module_environment = true;
|
||||
return TRY(module_environment.get_binding_value_direct(vm, index.value()));
|
||||
}
|
||||
return TRY(module_environment.get_binding_value(vm, identifier, vm.in_strict_mode()));
|
||||
}
|
||||
}
|
||||
|
@ -1146,6 +1157,8 @@ inline ThrowCompletionOr<Value> get_global(Interpreter& interpreter, IdentifierT
|
|||
Optional<size_t> offset;
|
||||
if (TRY(declarative_record.has_binding(identifier, &offset))) {
|
||||
cache.environment_binding_index = static_cast<u32>(offset.value());
|
||||
cache.has_environment_binding_index = true;
|
||||
cache.in_module_environment = false;
|
||||
return TRY(declarative_record.get_binding_value(vm, identifier, vm.in_strict_mode()));
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue