From a3af7ca1a039e87e0740de06c660797fb610dfde Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Wed, 30 Jul 2025 12:10:17 +0200 Subject: [PATCH] LibJS: Skip PrivateEnvironment allocation if possible If class doesn't have any private fields, we could avoid allocating PrivateEnvironment for it. This allows us to skip thousands of unnecessary PrivateEnvironment allocations on Discord. --- Libraries/LibJS/Bytecode/ASTCodegen.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Libraries/LibJS/Bytecode/ASTCodegen.cpp index 32dfc18cc5b..cb376d2e5a5 100644 --- a/Libraries/LibJS/Bytecode/ASTCodegen.cpp +++ b/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -2892,11 +2892,14 @@ Bytecode::CodeGenerationErrorOr> ClassExpression::genera if (m_super_class) super_class = TRY(m_super_class->generate_bytecode(generator)).value(); - generator.emit(); - + bool did_emit_private_environment_allocation = false; for (auto const& element : m_elements) { auto opt_private_name = element->private_bound_identifier(); if (opt_private_name.has_value()) { + if (!did_emit_private_environment_allocation) { + generator.emit(); + did_emit_private_environment_allocation = true; + } generator.emit(generator.intern_identifier(*opt_private_name)); } } @@ -2920,7 +2923,9 @@ Bytecode::CodeGenerationErrorOr> ClassExpression::genera auto dst = choose_dst(generator, preferred_dst); generator.emit_with_extra_slots>(elements.size(), dst, super_class.has_value() ? super_class->operand() : Optional {}, *this, lhs_name, elements); - generator.emit(); + if (did_emit_private_environment_allocation) { + generator.emit(); + } return dst; }