Commit a402dd63 authored by Michael Lippautz's avatar Michael Lippautz Committed by Commit Bot

[heap] Simplify array buffer tracker

Retrieve length from the buffer instead of storing in as value in the
map.

Change-Id: Icdc87bb46c29fff779bf6e98139324662ced5bf6
Reviewed-on: https://chromium-review.googlesource.com/518171
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: 's avatarJochen Eisinger <jochen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#45600}
parent f314b3cd
...@@ -14,7 +14,7 @@ void ArrayBufferTracker::RegisterNew(Heap* heap, JSArrayBuffer* buffer) { ...@@ -14,7 +14,7 @@ void ArrayBufferTracker::RegisterNew(Heap* heap, JSArrayBuffer* buffer) {
void* data = buffer->backing_store(); void* data = buffer->backing_store();
if (!data) return; if (!data) return;
size_t length = NumberToSize(buffer->byte_length()); size_t length = buffer->allocation_length();
Page* page = Page::FromAddress(buffer->address()); Page* page = Page::FromAddress(buffer->address());
{ {
base::LockGuard<base::RecursiveMutex> guard(page->mutex()); base::LockGuard<base::RecursiveMutex> guard(page->mutex());
...@@ -24,7 +24,7 @@ void ArrayBufferTracker::RegisterNew(Heap* heap, JSArrayBuffer* buffer) { ...@@ -24,7 +24,7 @@ void ArrayBufferTracker::RegisterNew(Heap* heap, JSArrayBuffer* buffer) {
tracker = page->local_tracker(); tracker = page->local_tracker();
} }
DCHECK_NOT_NULL(tracker); DCHECK_NOT_NULL(tracker);
tracker->Add(buffer, length); tracker->Add(buffer);
} }
// We may go over the limit of externally allocated memory here. We call the // We may go over the limit of externally allocated memory here. We call the
// api function to trigger a GC in this case. // api function to trigger a GC in this case.
...@@ -37,31 +37,29 @@ void ArrayBufferTracker::Unregister(Heap* heap, JSArrayBuffer* buffer) { ...@@ -37,31 +37,29 @@ void ArrayBufferTracker::Unregister(Heap* heap, JSArrayBuffer* buffer) {
if (!data) return; if (!data) return;
Page* page = Page::FromAddress(buffer->address()); Page* page = Page::FromAddress(buffer->address());
size_t length = 0; size_t length = buffer->allocation_length();
{ {
base::LockGuard<base::RecursiveMutex> guard(page->mutex()); base::LockGuard<base::RecursiveMutex> guard(page->mutex());
LocalArrayBufferTracker* tracker = page->local_tracker(); LocalArrayBufferTracker* tracker = page->local_tracker();
DCHECK_NOT_NULL(tracker); DCHECK_NOT_NULL(tracker);
length = tracker->Remove(buffer); tracker->Remove(buffer);
} }
heap->update_external_memory(-static_cast<intptr_t>(length)); heap->update_external_memory(-static_cast<intptr_t>(length));
} }
void LocalArrayBufferTracker::Add(Key key, const Value& value) { void LocalArrayBufferTracker::Add(JSArrayBuffer* buffer) {
auto ret = array_buffers_.insert(std::make_pair(key, value)); auto ret = array_buffers_.insert(buffer);
USE(ret); USE(ret);
// Check that we indeed inserted a new value and did not overwrite an existing // Check that we indeed inserted a new value and did not overwrite an existing
// one (which would be a bug). // one (which would be a bug).
DCHECK(ret.second); DCHECK(ret.second);
} }
LocalArrayBufferTracker::Value LocalArrayBufferTracker::Remove(Key key) { void LocalArrayBufferTracker::Remove(JSArrayBuffer* buffer) {
TrackingData::iterator it = array_buffers_.find(key); TrackingData::iterator it = array_buffers_.find(buffer);
// Check that we indeed find a key to remove. // Check that we indeed find a key to remove.
DCHECK(it != array_buffers_.end()); DCHECK(it != array_buffers_.end());
Value value = it->second;
array_buffers_.erase(it); array_buffers_.erase(it);
return value;
} }
} // namespace internal } // namespace internal
......
...@@ -19,9 +19,9 @@ void LocalArrayBufferTracker::Free(Callback should_free) { ...@@ -19,9 +19,9 @@ void LocalArrayBufferTracker::Free(Callback should_free) {
size_t freed_memory = 0; size_t freed_memory = 0;
for (TrackingData::iterator it = array_buffers_.begin(); for (TrackingData::iterator it = array_buffers_.begin();
it != array_buffers_.end();) { it != array_buffers_.end();) {
JSArrayBuffer* buffer = reinterpret_cast<JSArrayBuffer*>(it->first); JSArrayBuffer* buffer = reinterpret_cast<JSArrayBuffer*>(*it);
if (should_free(buffer)) { if (should_free(buffer)) {
const size_t len = it->second; const size_t len = buffer->allocation_length();
buffer->FreeBackingStore(); buffer->FreeBackingStore();
freed_memory += len; freed_memory += len;
...@@ -42,7 +42,7 @@ void LocalArrayBufferTracker::Process(Callback callback) { ...@@ -42,7 +42,7 @@ void LocalArrayBufferTracker::Process(Callback callback) {
size_t freed_memory = 0; size_t freed_memory = 0;
for (TrackingData::iterator it = array_buffers_.begin(); for (TrackingData::iterator it = array_buffers_.begin();
it != array_buffers_.end();) { it != array_buffers_.end();) {
const CallbackResult result = callback(it->first, &new_buffer); const CallbackResult result = callback(*it, &new_buffer);
if (result == kKeepEntry) { if (result == kKeepEntry) {
++it; ++it;
} else if (result == kUpdateEntry) { } else if (result == kUpdateEntry) {
...@@ -57,12 +57,13 @@ void LocalArrayBufferTracker::Process(Callback callback) { ...@@ -57,12 +57,13 @@ void LocalArrayBufferTracker::Process(Callback callback) {
tracker = target_page->local_tracker(); tracker = target_page->local_tracker();
} }
DCHECK_NOT_NULL(tracker); DCHECK_NOT_NULL(tracker);
tracker->Add(new_buffer, it->second); tracker->Add(new_buffer);
if (target_page->InNewSpace()) target_page->mutex()->Unlock(); if (target_page->InNewSpace()) target_page->mutex()->Unlock();
it = array_buffers_.erase(it); it = array_buffers_.erase(it);
} else if (result == kRemoveEntry) { } else if (result == kRemoveEntry) {
const size_t len = it->second; JSArrayBuffer* buffer = reinterpret_cast<JSArrayBuffer*>(*it);
it->first->FreeBackingStore(); const size_t len = buffer->allocation_length();
buffer->FreeBackingStore();
freed_memory += len; freed_memory += len;
it = array_buffers_.erase(it); it = array_buffers_.erase(it);
} else { } else {
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#ifndef V8_HEAP_ARRAY_BUFFER_TRACKER_H_ #ifndef V8_HEAP_ARRAY_BUFFER_TRACKER_H_
#define V8_HEAP_ARRAY_BUFFER_TRACKER_H_ #define V8_HEAP_ARRAY_BUFFER_TRACKER_H_
#include <unordered_map> #include <unordered_set>
#include "src/allocation.h" #include "src/allocation.h"
#include "src/base/platform/mutex.h" #include "src/base/platform/mutex.h"
...@@ -60,17 +60,14 @@ class ArrayBufferTracker : public AllStatic { ...@@ -60,17 +60,14 @@ class ArrayBufferTracker : public AllStatic {
// Never use directly but instead always call through |ArrayBufferTracker|. // Never use directly but instead always call through |ArrayBufferTracker|.
class LocalArrayBufferTracker { class LocalArrayBufferTracker {
public: public:
typedef JSArrayBuffer* Key;
typedef size_t Value;
enum CallbackResult { kKeepEntry, kUpdateEntry, kRemoveEntry }; enum CallbackResult { kKeepEntry, kUpdateEntry, kRemoveEntry };
enum FreeMode { kFreeDead, kFreeAll }; enum FreeMode { kFreeDead, kFreeAll };
explicit LocalArrayBufferTracker(Heap* heap) : heap_(heap) {} explicit LocalArrayBufferTracker(Heap* heap) : heap_(heap) {}
~LocalArrayBufferTracker(); ~LocalArrayBufferTracker();
inline void Add(Key key, const Value& value); inline void Add(JSArrayBuffer* buffer);
inline Value Remove(Key key); inline void Remove(JSArrayBuffer* buffer);
// Frees up array buffers. // Frees up array buffers.
// //
...@@ -92,14 +89,16 @@ class LocalArrayBufferTracker { ...@@ -92,14 +89,16 @@ class LocalArrayBufferTracker {
bool IsEmpty() { return array_buffers_.empty(); } bool IsEmpty() { return array_buffers_.empty(); }
bool IsTracked(Key key) { bool IsTracked(JSArrayBuffer* buffer) {
return array_buffers_.find(key) != array_buffers_.end(); return array_buffers_.find(buffer) != array_buffers_.end();
} }
private: private:
typedef std::unordered_map<Key, Value> TrackingData; typedef std::unordered_set<JSArrayBuffer*> TrackingData;
Heap* heap_; Heap* heap_;
// The set contains raw heap pointers which are removed by the GC upon
// processing the tracker through its owning page.
TrackingData array_buffers_; TrackingData array_buffers_;
}; };
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "src/heap/mark-compact.h" #include "src/heap/mark-compact.h"
#include <unordered_map>
#include "src/base/atomicops.h" #include "src/base/atomicops.h"
#include "src/base/bits.h" #include "src/base/bits.h"
#include "src/base/sys-info.h" #include "src/base/sys-info.h"
......
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