mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:27:43 +00:00
LibJS: Keep track of PrimitiveStrings and share them
VM now has a string cache which tracks all live PrimitiveStrings and reuses an existing one if possible. This drastically reduces the number of GC-allocated strings in many real-word situations.
This commit is contained in:
parent
ba6e4c7ae1
commit
f290c59dd8
3 changed files with 13 additions and 1 deletions
|
@ -52,6 +52,7 @@ Heap::Heap(VM& vm)
|
||||||
|
|
||||||
Heap::~Heap()
|
Heap::~Heap()
|
||||||
{
|
{
|
||||||
|
vm().string_cache().clear();
|
||||||
collect_garbage(CollectionType::CollectEverything);
|
collect_garbage(CollectionType::CollectEverything);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,7 @@ PrimitiveString::PrimitiveString(Utf16String string)
|
||||||
|
|
||||||
PrimitiveString::~PrimitiveString()
|
PrimitiveString::~PrimitiveString()
|
||||||
{
|
{
|
||||||
|
vm().string_cache().remove(m_utf8_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
String const& PrimitiveString::string() const
|
String const& PrimitiveString::string() const
|
||||||
|
@ -90,7 +91,14 @@ PrimitiveString* js_string(Heap& heap, String string)
|
||||||
return &heap.vm().single_ascii_character_string(ch);
|
return &heap.vm().single_ascii_character_string(ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
return heap.allocate_without_global_object<PrimitiveString>(move(string));
|
auto& string_cache = heap.vm().string_cache();
|
||||||
|
auto it = string_cache.find(string);
|
||||||
|
if (it == string_cache.end()) {
|
||||||
|
auto* new_string = heap.allocate_without_global_object<PrimitiveString>(string);
|
||||||
|
string_cache.set(move(string), new_string);
|
||||||
|
return new_string;
|
||||||
|
}
|
||||||
|
return it->value;
|
||||||
}
|
}
|
||||||
|
|
||||||
PrimitiveString* js_string(VM& vm, String string)
|
PrimitiveString* js_string(VM& vm, String string)
|
||||||
|
|
|
@ -81,6 +81,7 @@ public:
|
||||||
|
|
||||||
Symbol* get_global_symbol(const String& description);
|
Symbol* get_global_symbol(const String& description);
|
||||||
|
|
||||||
|
HashMap<String, PrimitiveString*>& string_cache() { return m_string_cache; }
|
||||||
PrimitiveString& empty_string() { return *m_empty_string; }
|
PrimitiveString& empty_string() { return *m_empty_string; }
|
||||||
PrimitiveString& single_ascii_character_string(u8 character)
|
PrimitiveString& single_ascii_character_string(u8 character)
|
||||||
{
|
{
|
||||||
|
@ -294,6 +295,8 @@ private:
|
||||||
|
|
||||||
Exception* m_exception { nullptr };
|
Exception* m_exception { nullptr };
|
||||||
|
|
||||||
|
HashMap<String, PrimitiveString*> m_string_cache;
|
||||||
|
|
||||||
Heap m_heap;
|
Heap m_heap;
|
||||||
Vector<Interpreter*> m_interpreters;
|
Vector<Interpreter*> m_interpreters;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue