Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(256)

Unified Diff: src/heap/array-buffer-tracker.cc

Issue 2024063002: Reland "[heap] Fine-grained JSArrayBuffer tracking" (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: bugfix and test added Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/heap/array-buffer-tracker.cc
diff --git a/src/heap/array-buffer-tracker.cc b/src/heap/array-buffer-tracker.cc
index 6e389c1cbf6abe216c782bae30fedcba5b283443..3851a1db10b2fd72d1a47248c9c76f2c8d8ec739 100644
--- a/src/heap/array-buffer-tracker.cc
+++ b/src/heap/array-buffer-tracker.cc
@@ -3,138 +3,193 @@
// found in the LICENSE file.
#include "src/heap/array-buffer-tracker.h"
+#include "src/heap/array-buffer-tracker-inl.h"
#include "src/heap/heap.h"
#include "src/isolate.h"
-#include "src/objects.h"
#include "src/objects-inl.h"
+#include "src/objects.h"
#include "src/v8.h"
namespace v8 {
namespace internal {
-ArrayBufferTracker::~ArrayBufferTracker() {
- Isolate* isolate = heap()->isolate();
+LocalArrayBufferTracker::~LocalArrayBufferTracker() {
size_t freed_memory = 0;
- for (auto& buffer : live_array_buffers_) {
- isolate->array_buffer_allocator()->Free(buffer.first, buffer.second);
- freed_memory += buffer.second;
+ for (auto& buffer : live_) {
+ heap_->isolate()->array_buffer_allocator()->Free(buffer.second.first,
+ buffer.second.second);
+ freed_memory += buffer.second.second;
}
- for (auto& buffer : live_array_buffers_for_scavenge_) {
- isolate->array_buffer_allocator()->Free(buffer.first, buffer.second);
- freed_memory += buffer.second;
+ if (freed_memory > 0) {
+ heap_->update_amount_of_external_allocated_freed_memory(
+ static_cast<intptr_t>(freed_memory));
}
- live_array_buffers_.clear();
- live_array_buffers_for_scavenge_.clear();
- not_yet_discovered_array_buffers_.clear();
- not_yet_discovered_array_buffers_for_scavenge_.clear();
+ live_.clear();
+ not_yet_discovered_.clear();
+}
+
+void LocalArrayBufferTracker::Add(Key key, const Value& value) {
+ live_[key] = value;
+ not_yet_discovered_[key] = value;
+}
+
+void LocalArrayBufferTracker::AddLive(Key key, const Value& value) {
+ live_[key] = value;
+ DCHECK_EQ(not_yet_discovered_.count(key), 0);
+}
+
+void LocalArrayBufferTracker::MarkLive(Key key) {
+ DCHECK_EQ(live_.count(key), 1);
+ not_yet_discovered_.erase(key);
+}
+
+LocalArrayBufferTracker::Value LocalArrayBufferTracker::Remove(Key key) {
+ DCHECK_EQ(live_.count(key), 1);
+ Value value = live_[key];
+ live_.erase(key);
+ not_yet_discovered_.erase(key);
+ return value;
+}
+void LocalArrayBufferTracker::FreeDead() {
+ size_t freed_memory = 0;
+ for (TrackingMap::iterator it = not_yet_discovered_.begin();
+ it != not_yet_discovered_.end();) {
+ heap_->isolate()->array_buffer_allocator()->Free(it->second.first,
+ it->second.second);
+ freed_memory += it->second.second;
+ live_.erase(it->first);
+ not_yet_discovered_.erase(it++);
+ }
if (freed_memory > 0) {
- heap()->update_amount_of_external_allocated_memory(
- -static_cast<int64_t>(freed_memory));
+ heap_->update_amount_of_external_allocated_freed_memory(
+ static_cast<intptr_t>(freed_memory));
+ }
+ started_ = false;
+}
+
+void LocalArrayBufferTracker::Reset() {
+ if (!started_) {
+ not_yet_discovered_ = live_;
+ started_ = true;
}
}
+bool LocalArrayBufferTracker::IsEmpty() {
+ return live_.empty() && not_yet_discovered_.empty();
+}
-void ArrayBufferTracker::RegisterNew(JSArrayBuffer* buffer) {
+void ArrayBufferTracker::RegisterNew(Heap* heap, JSArrayBuffer* buffer) {
void* data = buffer->backing_store();
if (!data) return;
- bool in_new_space = heap()->InNewSpace(buffer);
- size_t length = NumberToSize(heap()->isolate(), buffer->byte_length());
- if (in_new_space) {
- live_array_buffers_for_scavenge_[data] = length;
- } else {
- live_array_buffers_[data] = length;
+ size_t length = NumberToSize(heap->isolate(), buffer->byte_length());
+ Page* page = Page::FromAddress(buffer->address());
+ LocalArrayBufferTracker* tracker =
+ page->local_tracker<Page::kCreateIfNotPresent>();
+ DCHECK_NOT_NULL(tracker);
+ {
+ base::LockGuard<base::Mutex> guard(page->mutex());
+ if (Marking::IsBlack(Marking::MarkBitFrom(buffer))) {
+ tracker->AddLive(buffer, std::make_pair(data, length));
+ } else {
+ tracker->Add(buffer, std::make_pair(data, length));
+ }
}
-
// We may go over the limit of externally allocated memory here. We call the
// api function to trigger a GC in this case.
- reinterpret_cast<v8::Isolate*>(heap()->isolate())
+ reinterpret_cast<v8::Isolate*>(heap->isolate())
->AdjustAmountOfExternalAllocatedMemory(length);
}
-
-void ArrayBufferTracker::Unregister(JSArrayBuffer* buffer) {
+void ArrayBufferTracker::Unregister(Heap* heap, JSArrayBuffer* buffer) {
void* data = buffer->backing_store();
if (!data) return;
- bool in_new_space = heap()->InNewSpace(buffer);
- std::map<void*, size_t>* live_buffers =
- in_new_space ? &live_array_buffers_for_scavenge_ : &live_array_buffers_;
- std::map<void*, size_t>* not_yet_discovered_buffers =
- in_new_space ? &not_yet_discovered_array_buffers_for_scavenge_
- : &not_yet_discovered_array_buffers_;
-
- DCHECK(live_buffers->count(data) > 0);
-
- size_t length = (*live_buffers)[data];
- live_buffers->erase(data);
- not_yet_discovered_buffers->erase(data);
-
- heap()->update_amount_of_external_allocated_memory(
- -static_cast<int64_t>(length));
+ Page* page = Page::FromAddress(buffer->address());
+ LocalArrayBufferTracker* tracker = page->local_tracker<Page::kDontCreate>();
+ DCHECK_NOT_NULL(tracker);
+ size_t length = 0;
+ {
+ base::LockGuard<base::Mutex> guard(page->mutex());
+ length = tracker->Remove(buffer).second;
+ }
+ heap->update_amount_of_external_allocated_memory(
+ -static_cast<intptr_t>(length));
}
-
-void ArrayBufferTracker::MarkLive(JSArrayBuffer* buffer) {
- base::LockGuard<base::Mutex> guard(&mutex_);
- void* data = buffer->backing_store();
-
- // ArrayBuffer might be in the middle of being constructed.
- if (data == heap()->undefined_value()) return;
- if (heap()->InNewSpace(buffer)) {
- not_yet_discovered_array_buffers_for_scavenge_.erase(data);
- } else {
- not_yet_discovered_array_buffers_.erase(data);
+void ArrayBufferTracker::FreeDeadInNewSpace(Heap* heap) {
+ NewSpacePageIterator from_it(heap->new_space()->FromSpaceStart(),
+ heap->new_space()->FromSpaceEnd());
+ while (from_it.has_next()) {
+ ScanAndFreeDeadArrayBuffers<LocalArrayBufferTracker::kForwardingPointer>(
+ from_it.next());
}
+ heap->account_amount_of_external_allocated_freed_memory();
}
+void ArrayBufferTracker::ResetTrackersInOldSpace(Heap* heap) {
+ heap->old_space()->ForAllPages([](Page* p) {
+ LocalArrayBufferTracker* tracker = p->local_tracker<Page::kDontCreate>();
+ if (tracker != nullptr) {
+ tracker->Reset();
+ if (tracker->IsEmpty()) {
+ p->ReleaseLocalTracker();
+ }
+ }
+ });
+}
-void ArrayBufferTracker::FreeDead(bool from_scavenge) {
- size_t freed_memory = 0;
- Isolate* isolate = heap()->isolate();
- for (auto& buffer : not_yet_discovered_array_buffers_for_scavenge_) {
- isolate->array_buffer_allocator()->Free(buffer.first, buffer.second);
- freed_memory += buffer.second;
- live_array_buffers_for_scavenge_.erase(buffer.first);
+void ArrayBufferTracker::MarkLive(Heap* heap, JSArrayBuffer* buffer) {
+ if (buffer->is_external()) return;
+ void* data = buffer->backing_store();
+ if (data == nullptr) return;
+ if (data == heap->undefined_value()) return;
+
+ Page* page = Page::FromAddress(buffer->address());
+ LocalArrayBufferTracker* tracker =
+ page->local_tracker<Page::kCreateIfNotPresent>();
+ DCHECK_NOT_NULL(tracker);
+ {
+ base::LockGuard<base::Mutex> guard(page->mutex());
+ DCHECK(tracker->IsTracked(buffer));
+ tracker->MarkLive((buffer));
}
+}
- if (!from_scavenge) {
- for (auto& buffer : not_yet_discovered_array_buffers_) {
- isolate->array_buffer_allocator()->Free(buffer.first, buffer.second);
- freed_memory += buffer.second;
- live_array_buffers_.erase(buffer.first);
- }
+void ArrayBufferTracker::FreeDead(Page* page) {
+ // Only called from the sweeper, which already holds the page lock.
+ LocalArrayBufferTracker* tracker = page->local_tracker<Page::kDontCreate>();
+ if (tracker != nullptr) {
+ tracker->FreeDead();
}
-
- not_yet_discovered_array_buffers_for_scavenge_ =
- live_array_buffers_for_scavenge_;
- if (!from_scavenge) not_yet_discovered_array_buffers_ = live_array_buffers_;
-
- // Do not call through the api as this code is triggered while doing a GC.
- heap()->update_amount_of_external_allocated_memory(
- -static_cast<int64_t>(freed_memory));
}
-
-void ArrayBufferTracker::PrepareDiscoveryInNewSpace() {
- not_yet_discovered_array_buffers_for_scavenge_ =
- live_array_buffers_for_scavenge_;
+template <LocalArrayBufferTracker::LivenessIndicator liveness_indicator>
+void ArrayBufferTracker::ScanAndFreeDeadArrayBuffers(Page* page) {
+ LocalArrayBufferTracker* tracker = page->local_tracker<Page::kDontCreate>();
+ if (tracker != nullptr) {
+ base::LockGuard<base::Mutex> guard(page->mutex());
+ tracker->ScanAndFreeDead<liveness_indicator>();
+ }
}
-
-void ArrayBufferTracker::Promote(JSArrayBuffer* buffer) {
- base::LockGuard<base::Mutex> guard(&mutex_);
-
- if (buffer->is_external()) return;
- void* data = buffer->backing_store();
- if (!data) return;
- // ArrayBuffer might be in the middle of being constructed.
- if (data == heap()->undefined_value()) return;
- DCHECK(live_array_buffers_for_scavenge_.count(data) > 0);
- live_array_buffers_[data] = live_array_buffers_for_scavenge_[data];
- live_array_buffers_for_scavenge_.erase(data);
- not_yet_discovered_array_buffers_for_scavenge_.erase(data);
+template void ArrayBufferTracker::ScanAndFreeDeadArrayBuffers<
+ LocalArrayBufferTracker::LivenessIndicator::kForwardingPointer>(Page* page);
+template void ArrayBufferTracker::ScanAndFreeDeadArrayBuffers<
+ LocalArrayBufferTracker::LivenessIndicator::kMarkBit>(Page* page);
+template void ArrayBufferTracker::ScanAndFreeDeadArrayBuffers<
+ LocalArrayBufferTracker::LivenessIndicator::kForwardingPointerOrMarkBit>(
+ Page* page);
+
+bool ArrayBufferTracker::IsTracked(JSArrayBuffer* buffer) {
+ Page* page = Page::FromAddress(buffer->address());
+ LocalArrayBufferTracker* tracker =
+ page->local_tracker<Page::kCreateIfNotPresent>();
+ {
+ base::LockGuard<base::Mutex> guard(page->mutex());
+ return tracker->IsTracked(buffer);
+ }
}
} // namespace internal

Powered by Google App Engine
This is Rietveld 408576698