Commit d99c0dfd authored by Michael Lippautz's avatar Michael Lippautz Committed by V8 LUCI CQ

cppgc: Bail out on sweeping on allocation when space is empty

Add an approximate bailout for sweeping on allocation to avoid
reporting scopes when no work is being done.

Bug: v8:12286
Change-Id: I7589429805af702c35d51f9a37eae596268dedc5
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3306983Reviewed-by: 's avatarOmer Katz <omerkatz@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78166}
parent e24deb89
...@@ -136,11 +136,15 @@ class ThreadSafeStack { ...@@ -136,11 +136,15 @@ class ThreadSafeStack {
void Push(T t) { void Push(T t) {
v8::base::LockGuard<v8::base::Mutex> lock(&mutex_); v8::base::LockGuard<v8::base::Mutex> lock(&mutex_);
vector_.push_back(std::move(t)); vector_.push_back(std::move(t));
is_empty_.store(false, std::memory_order_relaxed);
} }
Optional<T> Pop() { Optional<T> Pop() {
v8::base::LockGuard<v8::base::Mutex> lock(&mutex_); v8::base::LockGuard<v8::base::Mutex> lock(&mutex_);
if (vector_.empty()) return v8::base::nullopt; if (vector_.empty()) {
is_empty_.store(true, std::memory_order_relaxed);
return v8::base::nullopt;
}
T top = std::move(vector_.back()); T top = std::move(vector_.back());
vector_.pop_back(); vector_.pop_back();
// std::move is redundant but is needed to avoid the bug in gcc-7. // std::move is redundant but is needed to avoid the bug in gcc-7.
...@@ -151,16 +155,15 @@ class ThreadSafeStack { ...@@ -151,16 +155,15 @@ class ThreadSafeStack {
void Insert(It begin, It end) { void Insert(It begin, It end) {
v8::base::LockGuard<v8::base::Mutex> lock(&mutex_); v8::base::LockGuard<v8::base::Mutex> lock(&mutex_);
vector_.insert(vector_.end(), begin, end); vector_.insert(vector_.end(), begin, end);
is_empty_.store(false, std::memory_order_relaxed);
} }
bool IsEmpty() const { bool IsEmpty() const { return is_empty_.load(std::memory_order_relaxed); }
v8::base::LockGuard<v8::base::Mutex> lock(&mutex_);
return vector_.empty();
}
private: private:
std::vector<T> vector_; std::vector<T> vector_;
mutable v8::base::Mutex mutex_; mutable v8::base::Mutex mutex_;
std::atomic<bool> is_empty_{true};
}; };
struct SpaceState { struct SpaceState {
...@@ -748,14 +751,21 @@ class Sweeper::SweeperImpl final { ...@@ -748,14 +751,21 @@ class Sweeper::SweeperImpl final {
// allocate new memory. // allocate new memory.
if (is_sweeping_on_mutator_thread_) return false; if (is_sweeping_on_mutator_thread_) return false;
SpaceState& space_state = space_states_[space->index()];
// Bail out if there's no pages to be processed for the space at this
// moment.
if (space_state.swept_unfinalized_pages.IsEmpty() &&
space_state.unswept_pages.IsEmpty()) {
return false;
}
StatsCollector::EnabledScope stats_scope(stats_collector_, StatsCollector::EnabledScope stats_scope(stats_collector_,
StatsCollector::kIncrementalSweep); StatsCollector::kIncrementalSweep);
StatsCollector::EnabledScope inner_scope( StatsCollector::EnabledScope inner_scope(
stats_collector_, StatsCollector::kSweepOnAllocation); stats_collector_, StatsCollector::kSweepOnAllocation);
MutatorThreadSweepingScope sweeping_in_progresss(*this); MutatorThreadSweepingScope sweeping_in_progresss(*this);
SpaceState& space_state = space_states_[space->index()];
{ {
// First, process unfinalized pages as finalizing a page is faster than // First, process unfinalized pages as finalizing a page is faster than
// sweeping. // sweeping.
......
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