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.
This commit is contained in:
Julien Le Bras 2025-05-24 15:28:46 +02:00 committed by Alexander Kalenik
commit 3ba6d129df
Notes: github-actions[bot] 2025-06-01 16:27:18 +00:00
2 changed files with 7 additions and 0 deletions

View file

@ -1344,6 +1344,12 @@ ScopedOperand Generator::add_constant(Value value)
return append_new_constant(); 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(); return append_new_constant();
} }

View file

@ -397,6 +397,7 @@ private:
mutable Optional<ScopedOperand> m_undefined_constant; mutable Optional<ScopedOperand> m_undefined_constant;
mutable Optional<ScopedOperand> m_empty_constant; mutable Optional<ScopedOperand> m_empty_constant;
mutable HashMap<i32, ScopedOperand> m_int32_constants; mutable HashMap<i32, ScopedOperand> m_int32_constants;
mutable HashMap<String, ScopedOperand> m_string_constants;
ScopedOperand m_accumulator; ScopedOperand m_accumulator;
ScopedOperand m_this_value; ScopedOperand m_this_value;