Skip to content

Commit

Permalink
LibJS: Transition ModuleEnvironment bindings to HashMap for efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
ananas-dev committed Dec 3, 2024
1 parent a7bd370 commit b7b18f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
28 changes: 10 additions & 18 deletions Libraries/LibJS/Runtime/ModuleEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ ThrowCompletionOr<Value> ModuleEnvironment::get_binding_value(VM& vm, Deprecated
VERIFY(strict);

// 2. Assert: envRec has a binding for N.
auto* indirect_binding = get_indirect_binding(name);
VERIFY(indirect_binding || !DeclarativeEnvironment::has_binding(name).is_error());
auto indirect_binding = get_indirect_binding(name);
VERIFY(indirect_binding.has_value() || !DeclarativeEnvironment::has_binding(name).is_error());

// 3. If the binding for N is an indirect binding, then
if (indirect_binding) {
if (indirect_binding.has_value()) {
// a. Let M and N2 be the indirection values provided when this binding for N was created.

// b. Let targetEnv be M.[[Environment]].
Expand Down Expand Up @@ -68,35 +68,27 @@ ThrowCompletionOr<Value> ModuleEnvironment::get_this_binding(VM&) const
ThrowCompletionOr<void> ModuleEnvironment::create_import_binding(DeprecatedFlyString name, Module* module, DeprecatedFlyString binding_name)
{
// 1. Assert: envRec does not already have a binding for N.
VERIFY(!get_indirect_binding(name));
VERIFY(!get_indirect_binding(name).has_value());
// 2. Assert: When M.[[Environment]] is instantiated it will have a direct binding for N2.
// FIXME: I don't know what this means or how to check it.

// 3. Create an immutable indirect binding in envRec for N that references M and N2 as its target binding and record that the binding is initialized.
// Note: We use the fact that the binding is in this list as it being initialized.
m_indirect_bindings.append({ move(name),
module,
move(binding_name) });
m_indirect_bindings.set(move(name), { module, move(binding_name) });

// 4. Return unused.
return {};
}

ModuleEnvironment::IndirectBinding const* ModuleEnvironment::get_indirect_binding(DeprecatedFlyString const& name) const
Optional<const ModuleEnvironment::IndirectBinding &> ModuleEnvironment::get_indirect_binding(DeprecatedFlyString const& name) const
{
auto binding_or_end = m_indirect_bindings.find_if([&](IndirectBinding const& binding) {
return binding.name == name;
});
if (binding_or_end.is_end())
return nullptr;

return &(*binding_or_end);
return m_indirect_bindings.get(name);;
}

Optional<ModuleEnvironment::BindingAndIndex> ModuleEnvironment::find_binding_and_index(DeprecatedFlyString const& name) const
{
auto* indirect_binding = get_indirect_binding(name);
if (indirect_binding != nullptr) {
auto indirect_binding = get_indirect_binding(name);
if (indirect_binding.has_value()) {
auto* target_env = indirect_binding->module->environment();
if (!target_env)
return {};
Expand Down Expand Up @@ -132,7 +124,7 @@ void ModuleEnvironment::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
for (auto& indirect_binding : m_indirect_bindings)
visitor.visit(indirect_binding.module);
visitor.visit(indirect_binding.value.module);
}

}
6 changes: 2 additions & 4 deletions Libraries/LibJS/Runtime/ModuleEnvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ class ModuleEnvironment final : public DeclarativeEnvironment {
virtual void visit_edges(Visitor&) override;

struct IndirectBinding {
DeprecatedFlyString name;
GC::Ptr<Module> module;
DeprecatedFlyString binding_name;
};
IndirectBinding const* get_indirect_binding(DeprecatedFlyString const& name) const;
Optional<const IndirectBinding &> get_indirect_binding(DeprecatedFlyString const& name) const;

virtual Optional<BindingAndIndex> find_binding_and_index(DeprecatedFlyString const& name) const override;

// FIXME: Since we always access this via the name this could be a map.
Vector<IndirectBinding> m_indirect_bindings;
HashMap<DeprecatedFlyString, IndirectBinding> m_indirect_bindings;
};

}

0 comments on commit b7b18f7

Please sign in to comment.