From 3ba6d129df607b3f114e9e61259611f9883412e2 Mon Sep 17 00:00:00 2001 From: Julien Le Bras Date: Sat, 24 May 2025 15:28:46 +0200 Subject: [PATCH] LibJS: Cache string constants in Generator::add_constant This mirrors the existing caching logic for int32 constants. Avoids duplication of string constants in m_constants which could result in stack overflows for large scripts with a lot of similar strings. --- Libraries/LibJS/Bytecode/Generator.cpp | 6 ++++++ Libraries/LibJS/Bytecode/Generator.h | 1 + 2 files changed, 7 insertions(+) diff --git a/Libraries/LibJS/Bytecode/Generator.cpp b/Libraries/LibJS/Bytecode/Generator.cpp index dd07edb60eb..5fb900aa101 100644 --- a/Libraries/LibJS/Bytecode/Generator.cpp +++ b/Libraries/LibJS/Bytecode/Generator.cpp @@ -1344,6 +1344,12 @@ ScopedOperand Generator::add_constant(Value value) return append_new_constant(); }); } + if (value.is_string()) { + auto as_string = value.as_string().utf8_string(); + return m_string_constants.ensure(as_string, [&] { + return append_new_constant(); + }); + } return append_new_constant(); } diff --git a/Libraries/LibJS/Bytecode/Generator.h b/Libraries/LibJS/Bytecode/Generator.h index 980375a589d..e0cb070ac7d 100644 --- a/Libraries/LibJS/Bytecode/Generator.h +++ b/Libraries/LibJS/Bytecode/Generator.h @@ -397,6 +397,7 @@ private: mutable Optional m_undefined_constant; mutable Optional m_empty_constant; mutable HashMap m_int32_constants; + mutable HashMap m_string_constants; ScopedOperand m_accumulator; ScopedOperand m_this_value;