1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:37:36 +00:00

LibJS: Combine DeclarativeEnvironment's bindings and names into one list

This reduces the size of the DeclarativeEnvironment from 72 bytes to 48
bytes. This savings helps in the context of nested for-loops which use
'let' to bind the initial variable declarations. In this case, the spec
dicates we create a new environment for each loop iteration by way of
the CreatePerIterationEnvironment AO.

In particular, test262's generated RegExp tests contains many loops of
the form:

    for (let i = 0; i < a_number_on_the_order_of_10; ++i)
        for (let j = 0; j < a_number_on_the_order_of_10_thousand; ++j)

This results in creating hundreds of thousands of environments.
This commit is contained in:
Timothy Flynn 2022-03-08 11:56:42 -05:00 committed by Linus Groh
parent 8d784310e0
commit 533170fbfa
2 changed files with 35 additions and 16 deletions

View file

@ -34,7 +34,16 @@ public:
ThrowCompletionOr<void> initialize_or_set_mutable_binding(GlobalObject& global_object, FlyString const& name, Value value);
// This is not a method defined in the spec! Do not use this in any LibJS (or other spec related) code.
[[nodiscard]] Vector<FlyString> const& bindings() const { return m_names; }
[[nodiscard]] Vector<FlyString> bindings() const
{
Vector<FlyString> names;
names.ensure_capacity(m_bindings.size());
for (auto const& binding : m_bindings)
names.unchecked_append(binding.name);
return names;
}
ThrowCompletionOr<Value> get_binding_value_direct(GlobalObject&, size_t index, bool strict);
ThrowCompletionOr<void> set_mutable_binding_direct(GlobalObject&, size_t index, Value, bool strict);
@ -45,7 +54,19 @@ protected:
private:
virtual bool is_declarative_environment() const override { return true; }
Optional<size_t> find_binding_index(FlyString const& name) const
{
auto it = m_bindings.find_if([&](auto const& binding) {
return binding.name == name;
});
if (it == m_bindings.end())
return {};
return it.index();
}
struct Binding {
FlyString name;
Value value;
bool strict { false };
bool mutable_ { false };
@ -53,7 +74,6 @@ private:
bool initialized { false };
};
Vector<FlyString> m_names;
Vector<Binding> m_bindings;
};