Truly independent web browser
LibGC+WebContent: Leave the stack out of timer-driven collections
Andreas Kling844e2d59c25c
diff --git a/Libraries/LibGC/Heap.cpp b/Libraries/LibGC/Heap.cppindex 36bf0fc054c..ec580dc70d7 100644--- a/Libraries/LibGC/Heap.cpp+++ b/Libraries/LibGC/Heap.cpp@@ -27,6 +27,7 @@#include <LibCore/File.h>#include <LibCore/StandardPaths.h>#include <LibCore/StatsLog.h>+#include <LibCore/ThreadEventQueue.h>#include <LibCore/Timer.h>#include <LibGC/BlockAllocator.h>#include <LibGC/CellAllocator.h>@@ -2003,7 +2004,7 @@ void Heap::idle_gc_on_timer()return;case IdleCollectionPolicy::Decision::Collect:m_allocated_bytes_since_last_gc = 0;- collect_garbage();+ collect_garbage_from_timer();m_idle_gc_timer->stop();return;}@@ -2053,6 +2054,19 @@ void Heap::on_abandoned_object_graph_timer()}m_allocated_bytes_since_last_gc = 0;+ collect_garbage_from_timer();+}++void Heap::collect_garbage_from_timer()+{+ // A timer fires from the event loop. In its outermost turn the only frames on the stack are the event loop's and the+ // embedder's, so if the embedder vouches for its own, there is no root to find there. Words that only look like a+ // cell pointer are still there, and some never change, so scanning them could keep a cell alive for good. A nested+ // event loop has its caller's frames further up the stack, and those need the scan.+ if (m_outermost_event_loop_turn_holds_no_cells && !Core::ThreadEventQueue::current().is_processing_nested()) {+ collect_garbage_without_scanning_the_stack();+ return;+ }collect_garbage();}diff --git a/Libraries/LibGC/Heap.h b/Libraries/LibGC/Heap.hindex b36bc947f09..bae6d3c60d2 100644--- a/Libraries/LibGC/Heap.h+++ b/Libraries/LibGC/Heap.h@@ -90,6 +90,11 @@ public:// cell alive. Only safe where every cell the stack refers to is rooted some other way, as in the outermost turn of// the event loop.void collect_garbage_without_scanning_the_stack();++ // Tells the heap that no frame on the stack of the outermost turn of this thread's event loop holds the only+ // reference to a cell. The collections that the heap starts from its own timers then leave the stack out when they+ // run in that turn. Only the embedder can promise this, since those frames include its own, like main().+ void set_outermost_event_loop_turn_holds_no_cells(bool value) { m_outermost_event_loop_turn_holds_no_cells = value; }NEVER_INLINE AK::JsonObject dump_graph();// A cell on a retaining path, which reaches the next one through a field it visits. The cell is live until the next@@ -388,6 +393,8 @@ private:RefPtr<Core::Timer> m_incremental_sweep_timer;RefPtr<Core::Timer> m_idle_gc_timer;+ bool m_outermost_event_loop_turn_holds_no_cells { false };+ void collect_garbage_from_timer();enum class AbandonedObjectGraphState : u8 {None,WaitingToCollect,diff --git a/Services/WebContent/main.cpp b/Services/WebContent/main.cppindex 2fcca8741b5..e45c9ec4ba7 100644--- a/Services/WebContent/main.cpp+++ b/Services/WebContent/main.cpp@@ -289,6 +289,8 @@ NEVER_INLINE static ErrorOr<ProcessState> initialize_process(Main::Arguments argWeb::Bindings::initialize_agent(Web::HTML::AgentType::SimilarOriginWindow);auto& agent = Web::HTML::surrounding_agent();agent.fetch_executor = Web::Fetch::Fetching::create_inline_fetch_executor(agent.network_activity_observers);+ // See initialize_process() and ladybird_main() for what keeps this true.+ Web::Bindings::main_thread_vm().heap().set_outermost_event_loop_turn_holds_no_cells(true);if (stats_log)Web::Bindings::main_thread_vm().heap().enable_stats();