Skip to content

Commit 3d4b13a

Browse files
kalenikaliaksandrawesomekling
authored andcommitted
LibJS: Ensure capacity for created lexical and variable environments
If the minimal amount of required bindings is known in advance, it could be used to ensure capacity to avoid resizing the internal vector that holds bindings.
1 parent a4f7098 commit 3d4b13a

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

Userland/Libraries/LibJS/Bytecode/Generator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ CodeGenerationErrorOr<void> Generator::emit_function_declaration_instantiation(E
120120
}
121121
}
122122
} else {
123-
emit<Op::CreateVariableEnvironment>();
123+
emit<Op::CreateVariableEnvironment>(function.m_var_environment_bindings_count);
124124

125125
if (scope_body) {
126126
for (auto const& variable_to_initialize : function.m_var_names_to_initialize_binding) {
@@ -158,7 +158,7 @@ CodeGenerationErrorOr<void> Generator::emit_function_declaration_instantiation(E
158158
if (!function.m_strict) {
159159
bool can_elide_declarative_environment = !function.m_contains_direct_call_to_eval && (!scope_body || !scope_body->has_non_local_lexical_declarations());
160160
if (!can_elide_declarative_environment) {
161-
emit<Op::CreateLexicalEnvironment>();
161+
emit<Op::CreateLexicalEnvironment>(function.m_lex_environment_bindings_count);
162162
}
163163
}
164164

Userland/Libraries/LibJS/Bytecode/Interpreter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,9 @@ ThrowCompletionOr<void> DeleteVariable::execute_impl(Bytecode::Interpreter& inte
12561256
void CreateLexicalEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
12571257
{
12581258
auto make_and_swap_envs = [&](auto& old_environment) {
1259-
GCPtr<Environment> environment = new_declarative_environment(*old_environment).ptr();
1259+
auto declarative_environment = new_declarative_environment(*old_environment).ptr();
1260+
declarative_environment->ensure_capacity(m_capacity);
1261+
GCPtr<Environment> environment = declarative_environment;
12601262
swap(old_environment, environment);
12611263
return environment;
12621264
};
@@ -1268,6 +1270,7 @@ ThrowCompletionOr<void> CreateVariableEnvironment::execute_impl(Bytecode::Interp
12681270
{
12691271
auto& running_execution_context = interpreter.vm().running_execution_context();
12701272
auto var_environment = new_declarative_environment(*running_execution_context.lexical_environment);
1273+
var_environment->ensure_capacity(m_capacity);
12711274
running_execution_context.variable_environment = var_environment;
12721275
running_execution_context.lexical_environment = var_environment;
12731276
return {};

Userland/Libraries/LibJS/Bytecode/Op.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,24 +443,32 @@ enum class EnvironmentMode {
443443

444444
class CreateLexicalEnvironment final : public Instruction {
445445
public:
446-
explicit CreateLexicalEnvironment()
446+
explicit CreateLexicalEnvironment(u32 capacity = 0)
447447
: Instruction(Type::CreateLexicalEnvironment)
448+
, m_capacity(capacity)
448449
{
449450
}
450451

451452
void execute_impl(Bytecode::Interpreter&) const;
452453
ByteString to_byte_string_impl(Bytecode::Executable const&) const;
454+
455+
private:
456+
u32 m_capacity { 0 };
453457
};
454458

455459
class CreateVariableEnvironment final : public Instruction {
456460
public:
457-
explicit CreateVariableEnvironment()
461+
explicit CreateVariableEnvironment(u32 capacity = 0)
458462
: Instruction(Type::CreateVariableEnvironment)
463+
, m_capacity(capacity)
459464
{
460465
}
461466

462467
ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
463468
ByteString to_byte_string_impl(Bytecode::Executable const&) const;
469+
470+
private:
471+
u32 m_capacity { 0 };
464472
};
465473

466474
class EnterObjectEnvironment final : public Instruction {

0 commit comments

Comments
 (0)