Commit 09dc239a authored by Omer Katz's avatar Omer Katz Committed by V8 LUCI CQ

cppgc-js: Reuse mutator thread marking state

On concurrent threads, CppMarkingState allocates its own
cppgc::internal::MarkingStateBase.
On the mutator thread, CppMarkingState reuses the same MarkingStateBase
as CppHeap's mutator thread visitor.
That means the mutator thread doesn't need to rely on publishing
segments to push object from V8 to CppHeap.

Bug: v8:12407
Change-Id: I161adf8dcdc9aa960de65b47feb2abd3b605df7c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3295454Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Commit-Queue: Omer Katz <omerkatz@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78449}
parent b536f37a
......@@ -171,6 +171,14 @@ UnifiedHeapConcurrentMarker::CreateConcurrentMarkingVisitor(
heap(), marking_state, unified_heap_marking_state_);
}
void FatalOutOfMemoryHandlerImpl(const std::string& reason,
const SourceLocation&, HeapBase* heap) {
FatalProcessOutOfMemory(static_cast<v8::internal::CppHeap*>(heap)->isolate(),
reason.c_str());
}
} // namespace
class UnifiedHeapMarker final : public cppgc::internal::MarkerBase {
public:
UnifiedHeapMarker(Heap* v8_heap, cppgc::internal::HeapBase& cpp_heap,
......@@ -184,6 +192,10 @@ class UnifiedHeapMarker final : public cppgc::internal::MarkerBase {
return marking_worklists_;
}
cppgc::internal::MarkingStateBase& GetMutatorMarkingState() {
return marking_visitor_.marking_state_;
}
protected:
cppgc::Visitor& visitor() final { return marking_visitor_; }
cppgc::internal::ConservativeTracingVisitor& conservative_visitor() final {
......@@ -219,14 +231,6 @@ void UnifiedHeapMarker::AddObject(void* object) {
cppgc::internal::HeapObjectHeader::FromObject(object));
}
void FatalOutOfMemoryHandlerImpl(const std::string& reason,
const SourceLocation&, HeapBase* heap) {
FatalProcessOutOfMemory(static_cast<v8::internal::CppHeap*>(heap)->isolate(),
reason.c_str());
}
} // namespace
void CppHeap::MetricRecorderAdapter::AddMainThreadEvent(
const FullCycle& cppgc_event) {
last_full_gc_event_ = cppgc_event;
......@@ -718,8 +722,18 @@ void CppHeap::FinishSweepingIfRunning() { sweeper_.FinishIfRunning(); }
std::unique_ptr<CppMarkingState> CppHeap::CreateCppMarkingState() {
DCHECK(IsMarking());
return std::make_unique<CppMarkingState>(
*this, wrapper_descriptor_,
static_cast<UnifiedHeapMarker*>(marker())->GetMarkingWorklists());
isolate(), wrapper_descriptor_,
std::make_unique<cppgc::internal::MarkingStateBase>(
AsBase(),
static_cast<UnifiedHeapMarker*>(marker())->GetMarkingWorklists()));
}
std::unique_ptr<CppMarkingState>
CppHeap::CreateCppMarkingStateForMutatorThread() {
DCHECK(IsMarking());
return std::make_unique<CppMarkingState>(
isolate(), wrapper_descriptor_,
static_cast<UnifiedHeapMarker*>(marker())->GetMutatorMarkingState());
}
} // namespace internal
......
......@@ -136,6 +136,7 @@ class V8_EXPORT_PRIVATE CppHeap final
Isolate* isolate() const { return isolate_; }
std::unique_ptr<CppMarkingState> CreateCppMarkingState();
std::unique_ptr<CppMarkingState> CreateCppMarkingStateForMutatorThread();
private:
void FinalizeIncrementalGarbageCollectionIfNeeded(
......
......@@ -5,6 +5,8 @@
#ifndef V8_HEAP_CPPGC_JS_CPP_MARKING_STATE_H_
#define V8_HEAP_CPPGC_JS_CPP_MARKING_STATE_H_
#include <memory>
#include "src/heap/cppgc-js/cpp-heap.h"
#include "src/heap/cppgc/marking-state.h"
#include "src/heap/cppgc/marking-worklists.h"
......@@ -16,13 +18,20 @@ class JSObject;
class CppMarkingState {
public:
CppMarkingState(CppHeap& cpp_heap,
const WrapperDescriptor& wrapper_descriptor,
cppgc::internal::MarkingWorklists& marking_worklists)
: isolate_(reinterpret_cast<Isolate*>(cpp_heap.isolate())),
CppMarkingState(Isolate* isolate, const WrapperDescriptor& wrapper_descriptor,
cppgc::internal::MarkingStateBase& main_thread_marking_state)
: isolate_(isolate),
wrapper_descriptor_(wrapper_descriptor),
marking_state_(cpp_heap.AsBase(), marking_worklists) {}
owned_marking_state_(nullptr),
marking_state_(main_thread_marking_state) {}
CppMarkingState(Isolate* isolate, const WrapperDescriptor& wrapper_descriptor,
std::unique_ptr<cppgc::internal::MarkingStateBase>
concurrent_marking_state)
: isolate_(isolate),
wrapper_descriptor_(wrapper_descriptor),
owned_marking_state_(std::move(concurrent_marking_state)),
marking_state_(*owned_marking_state_) {}
CppMarkingState(const CppMarkingState&) = delete;
CppMarkingState& operator=(const CppMarkingState&) = delete;
......@@ -38,7 +47,8 @@ class CppMarkingState {
Isolate* const isolate_;
const WrapperDescriptor& wrapper_descriptor_;
cppgc::internal::MarkingStateBase marking_state_;
std::unique_ptr<cppgc::internal::MarkingStateBase> owned_marking_state_;
cppgc::internal::MarkingStateBase& marking_state_;
};
} // namespace internal
......
......@@ -31,6 +31,8 @@ using cppgc::WeakCallback;
using cppgc::internal::HeapBase;
using cppgc::internal::MutatorMarkingState;
class UnifiedHeapMarker;
class V8_EXPORT_PRIVATE UnifiedHeapMarkingVisitorBase : public JSVisitor {
public:
UnifiedHeapMarkingVisitorBase(HeapBase&, cppgc::internal::BasicMarkingState&,
......@@ -53,6 +55,8 @@ class V8_EXPORT_PRIVATE UnifiedHeapMarkingVisitorBase : public JSVisitor {
cppgc::internal::BasicMarkingState& marking_state_;
UnifiedHeapMarkingState& unified_heap_marking_state_;
friend class UnifiedHeapMarker;
};
class V8_EXPORT_PRIVATE MutatorUnifiedHeapMarkingVisitor final
......
......@@ -574,9 +574,9 @@ void MarkCompactCollector::StartMarking() {
marking_worklists()->CreateContextWorklists(contexts);
auto* cpp_heap = CppHeap::From(heap_->cpp_heap());
local_marking_worklists_ = std::make_unique<MarkingWorklists::Local>(
marking_worklists(), cpp_heap
? cpp_heap->CreateCppMarkingState()
: MarkingWorklists::Local::kNoCppMarkingState);
marking_worklists(),
cpp_heap ? cpp_heap->CreateCppMarkingStateForMutatorThread()
: MarkingWorklists::Local::kNoCppMarkingState);
local_weak_objects_ = std::make_unique<WeakObjects::Local>(weak_objects());
marking_visitor_ = std::make_unique<MarkingVisitor>(
marking_state(), local_marking_worklists(), local_weak_objects_.get(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment