Browse Source

LibJS: Shrink DeclarativeEnvironment bindings vector to fit

After setting up all the bindings in function_declaration_instantiation,
we now ask DeclarativeEnvironment to do a shrink_to_fit() on its vector
of bindings.

This ends up saving 5.6 MiB on twitter.com/awesomekling :^)
Andreas Kling 2 years ago
parent
commit
2e98c17347

+ 5 - 0
Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.cpp

@@ -231,4 +231,9 @@ void DeclarativeEnvironment::initialize_or_set_mutable_binding(Badge<ScopeNode>,
     MUST(initialize_or_set_mutable_binding(vm, name, value));
 }
 
+void DeclarativeEnvironment::shrink_to_fit()
+{
+    m_bindings.shrink_to_fit();
+}
+
 }

+ 2 - 0
Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.h

@@ -57,6 +57,8 @@ public:
     ThrowCompletionOr<void> set_mutable_binding_direct(VM&, size_t index, Value, bool strict);
     ThrowCompletionOr<Value> get_binding_value_direct(VM&, size_t index, bool strict);
 
+    void shrink_to_fit();
+
 private:
     ThrowCompletionOr<void> initialize_binding_direct(VM&, Binding&, Value);
     ThrowCompletionOr<Value> get_binding_value_direct(VM&, Binding&, bool strict);

+ 5 - 0
Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp

@@ -577,6 +577,11 @@ ThrowCompletionOr<void> ECMAScriptFunctionObject::function_declaration_instantia
         MUST(var_environment->set_mutable_binding(vm, declaration.name(), function, false));
     }
 
+    if (is<DeclarativeEnvironment>(*lex_environment))
+        static_cast<DeclarativeEnvironment*>(lex_environment)->shrink_to_fit();
+    if (is<DeclarativeEnvironment>(*var_environment))
+        static_cast<DeclarativeEnvironment*>(var_environment)->shrink_to_fit();
+
     return {};
 }