Skip to content

Truly independent web browser

LibWeb: Count only unused programs against the compiled script cache

Aliaksandr Kalenik54fc4627c928
diff --git a/Libraries/LibWeb/HTML/Scripting/CompiledScriptCache.cpp b/Libraries/LibWeb/HTML/Scripting/CompiledScriptCache.cpp
index ce38d6da2d9..e7f3c756720 100644
--- a/Libraries/LibWeb/HTML/Scripting/CompiledScriptCache.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/CompiledScriptCache.cpp
@@ -28,7 +28,7 @@ Optional<CompiledScriptCache::Hit> CompiledScriptCache::find(CompiledScriptCache
Hit hit { entry->program.clone(), entry->source_text };
// NB: Entries grow as their functions compile, which no store sees.
- evict_until_within_limit();
+ evict_until_within_limit(key);
return hit;
}
@@ -37,10 +37,20 @@ void CompiledScriptCache::store(CompiledScriptCacheKey key, Utf16String source_t
if (auto entry = m_entries.take(key); entry.has_value())
release(*entry);
- if (program.memory_size() <= compiled_script_cache_size_limit) {
- program.set_retains_compiled_functions(true);
- m_entries.set(move(key), Entry { move(source_text), program.clone(), ++m_next_touch_serial });
- }
+ // NB: However large the program is, it costs nothing while a document runs it, which the one that stores it is about
+ // to, and goes once none does.
+ program.set_retains_compiled_functions(true);
+ m_entries.set(key, Entry { move(source_text), program.clone(), ++m_next_touch_serial, {} });
+ evict_until_within_limit(key);
+}
+
+void CompiledScriptCache::note_linked(CompiledScriptCacheKey const& key, JS::SourceCode const& source_code)
+{
+ auto entry = m_entries.get(key);
+ if (!entry.has_value())
+ return;
+ entry->linked_source_codes.remove_all_matching([](auto const& linked) { return !linked.is_alive(); });
+ entry->linked_source_codes.append(JS::WeakSourceCode { source_code });
evict_until_within_limit();
}
@@ -57,17 +67,31 @@ void CompiledScriptCache::release(Entry& entry)
entry.program.set_retains_compiled_functions(false);
}
-void CompiledScriptCache::evict_until_within_limit()
+bool CompiledScriptCache::is_linked(Entry& entry)
{
+ entry.linked_source_codes.remove_all_matching([](auto const& linked) { return !linked.is_alive(); });
+ return !entry.linked_source_codes.is_empty();
+}
+
+void CompiledScriptCache::evict_until_within_limit(Optional<CompiledScriptCacheKey const&> about_to_be_linked)
+{
+ auto counts_against_limit = [&](auto& it) {
+ if (about_to_be_linked.has_value() && it.key == *about_to_be_linked)
+ return false;
+ return !is_linked(it.value);
+ };
+
size_t size = 0;
- for (auto const& it : m_entries)
- size += it.value.program.memory_size();
+ for (auto& it : m_entries) {
+ if (counts_against_limit(it))
+ size += it.value.program.memory_size();
+ }
while (size > compiled_script_cache_size_limit) {
Optional<CompiledScriptCacheKey const&> least_recently_used_key;
u64 least_recently_used_serial = NumericLimits<u64>::max();
- for (auto const& it : m_entries) {
- if (it.value.touch_serial >= least_recently_used_serial)
+ for (auto& it : m_entries) {
+ if (it.value.touch_serial >= least_recently_used_serial || !counts_against_limit(it))
continue;
least_recently_used_key = it.key;
least_recently_used_serial = it.value.touch_serial;
diff --git a/Libraries/LibWeb/HTML/Scripting/CompiledScriptCache.h b/Libraries/LibWeb/HTML/Scripting/CompiledScriptCache.h
index 08041cf44e5..6312209e205 100644
--- a/Libraries/LibWeb/HTML/Scripting/CompiledScriptCache.h
+++ b/Libraries/LibWeb/HTML/Scripting/CompiledScriptCache.h
@@ -11,7 +11,9 @@
#include <AK/String.h>
#include <AK/Traits.h>
#include <AK/Utf16String.h>
+#include <AK/Vector.h>
#include <LibJS/ScriptCompilation.h>
+#include <LibJS/SourceCode.h>
#include <LibWeb/Export.h>
namespace Web::HTML {
@@ -30,6 +32,10 @@ struct CompiledScriptCacheKey {
// The compiled scripts of this agent, which a script that loads again links into its own realm instead of being
// parsed and compiled again. Entries are found after a fetch completed, by the decoded source text the fetch came up
// with, so they answer nothing that the checks of a fetch decide, and can not stand in for different source text.
+//
+// The size limit is for the programs that only the cache keeps alive. A program that a live document runs stays in
+// memory with or without its entry, so it counts for nothing until no document runs it anymore, and an entry that the
+// limit evicts is the one that no document runs and that was used the longest time ago.
class WEB_API CompiledScriptCache {
public:
struct Hit {
@@ -44,6 +50,10 @@ public:
// From here on, the program keeps the functions that get compiled for it. Replaces what the key held before.
void store(CompiledScriptCacheKey, Utf16String source_text, JS::CompiledProgram const&);
+ // The program of the key is linked into a record of the source code, which keeps the entry from counting against
+ // the limit for as long as anything holds the source code.
+ void note_linked(CompiledScriptCacheKey const&, JS::SourceCode const&);
+
bool contains(CompiledScriptCacheKey const& key) const { return m_entries.contains(key); }
void clear();
@@ -53,11 +63,15 @@ private:
Utf16String source_text;
JS::CompiledProgram program;
u64 touch_serial { 0 };
+ // The source code of the records that the program was linked into.
+ Vector<JS::WeakSourceCode> linked_source_codes;
};
static void release(Entry&);
+ static bool is_linked(Entry&);
- void evict_until_within_limit();
+ // An entry that is about to be linked, which no record holds the source code of yet, counts as linked.
+ void evict_until_within_limit(Optional<CompiledScriptCacheKey const&> about_to_be_linked = {});
HashMap<CompiledScriptCacheKey, Entry> m_entries;
u64 m_next_touch_serial { 0 };
diff --git a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
index ec59531d9b1..b9c814b2496 100644
--- a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
@@ -103,8 +103,11 @@ static void compile_off_thread(EnvironmentSettingsObject& settings_object, Utf16
// The script runs in a task of the settings object's global, so never beside a rendering update in flight that holds
// back the tasks of its document, and the microtasks it queues (e.g. promise reactions from react_to_promise during
// module linking) are drained after it.
- auto run_callback = [settings_object = GC::make_root(settings_object), on_compiled = GC::make_root(on_compiled), source_filename = move(source_filename)](OffThreadCompiledProgram result, Utf16String source_text) mutable {
+ auto run_callback = [settings_object = GC::make_root(settings_object), on_compiled = GC::make_root(on_compiled), source_filename = move(source_filename), cache_key](OffThreadCompiledProgram result, Utf16String source_text) mutable {
auto source_code = JS::SourceCode::create(move(source_filename), move(source_text));
+ // NB: The record that the task creates holds the source code, and so does every function of it.
+ if (cache_key.has_value() && result.compiled)
+ CompiledScriptCache::the().note_linked(*cache_key, *source_code);
queue_global_task(Task::Source::Networking, settings_object->global_object(), GC::create_function(GC::Heap::the(), GC::captures(on_compiled, move(source_code), move(result)), [](auto& on_compiled, auto& source_code, auto& result) {
on_compiled->function()(move(result), move(source_code));
}));