Files
sdk/runtime/vm/heap/pointer_block.cc
T
Matthew Dempsky 6d5f763952 [vm] Eliminate Mutex/Monitor indirection where possible
In many cases, the Mutexes and Monitors have to be marked "mutable"
because they're used to synchronize const accessor methods.

Small text segment improvement for Product builds:

$ size dart.{arm,x64}.{before,after}
   text	   data	    bss	    dec	    hex	filename
19726069	 409960	 392332	20528361	1393ce9	dart.arm.before
19725525	 409960	 392332	20527817	1393ac9	dart.arm.after
22576021	 600376	1782824	24959221	17cd8f5	dart.x64.before
22574821	 600376	1782824	24958021	17cd445	dart.x64.after

Change-Id: I68f5cd5ad452044df8bfebd160910496036a3e6b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/101745
Commit-Queue: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Vyacheslav Egorov <vegorov@google.com>
2019-05-13 21:17:51 +00:00

216 lines
5.5 KiB
C++

// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/heap/pointer_block.h"
#include "platform/assert.h"
#include "vm/lockers.h"
#include "vm/runtime_entry.h"
namespace dart {
DEFINE_LEAF_RUNTIME_ENTRY(void, StoreBufferBlockProcess, 1, Thread* thread) {
thread->StoreBufferBlockProcess(StoreBuffer::kCheckThreshold);
}
END_LEAF_RUNTIME_ENTRY
DEFINE_LEAF_RUNTIME_ENTRY(void, MarkingStackBlockProcess, 1, Thread* thread) {
thread->MarkingStackBlockProcess();
}
END_LEAF_RUNTIME_ENTRY
template <int BlockSize>
typename BlockStack<BlockSize>::List* BlockStack<BlockSize>::global_empty_ =
NULL;
template <int BlockSize>
Mutex* BlockStack<BlockSize>::global_mutex_ = NULL;
template <int BlockSize>
void BlockStack<BlockSize>::Init() {
global_empty_ = new List();
if (global_mutex_ == NULL) {
global_mutex_ = new Mutex();
}
}
template <int BlockSize>
void BlockStack<BlockSize>::Cleanup() {
delete global_empty_;
global_empty_ = NULL;
}
template <int BlockSize>
BlockStack<BlockSize>::BlockStack() : mutex_() {}
template <int BlockSize>
BlockStack<BlockSize>::~BlockStack() {
Reset();
}
template <int BlockSize>
void BlockStack<BlockSize>::Reset() {
MutexLocker local_mutex_locker(&mutex_);
{
// Empty all blocks and move them to the global cache.
MutexLocker global_mutex_locker(global_mutex_);
while (!full_.IsEmpty()) {
Block* block = full_.Pop();
block->Reset();
global_empty_->Push(block);
}
while (!partial_.IsEmpty()) {
Block* block = partial_.Pop();
block->Reset();
global_empty_->Push(block);
}
TrimGlobalEmpty();
}
}
template <int BlockSize>
typename BlockStack<BlockSize>::Block* BlockStack<BlockSize>::Blocks() {
MutexLocker ml(&mutex_);
while (!partial_.IsEmpty()) {
full_.Push(partial_.Pop());
}
return full_.PopAll();
}
template <int BlockSize>
void BlockStack<BlockSize>::PushBlockImpl(Block* block) {
ASSERT(block->next() == NULL); // Should be just a single block.
if (block->IsFull()) {
MutexLocker ml(&mutex_);
full_.Push(block);
} else if (block->IsEmpty()) {
MutexLocker ml(global_mutex_);
global_empty_->Push(block);
TrimGlobalEmpty();
} else {
MutexLocker ml(&mutex_);
partial_.Push(block);
}
}
template <int Size>
void PointerBlock<Size>::VisitObjectPointers(ObjectPointerVisitor* visitor) {
// Generated code appends to store buffers; tell MemorySanitizer.
MSAN_UNPOISON(this, sizeof(*this));
visitor->VisitPointers(&pointers_[0], top_);
}
void StoreBuffer::PushBlock(Block* block, ThresholdPolicy policy) {
BlockStack<Block::kSize>::PushBlockImpl(block);
if ((policy == kCheckThreshold) && Overflowed()) {
MutexLocker ml(&mutex_);
Thread* thread = Thread::Current();
// Sanity check: it makes no sense to schedule the GC in another isolate.
// (If Isolate ever gets multiple store buffers, we should avoid this
// coupling by passing in an explicit callback+parameter at construction.)
ASSERT(thread->isolate()->store_buffer() == this);
thread->ScheduleInterrupts(Thread::kVMInterrupt);
}
}
template <int BlockSize>
typename BlockStack<BlockSize>::Block*
BlockStack<BlockSize>::PopNonFullBlock() {
{
MutexLocker ml(&mutex_);
if (!partial_.IsEmpty()) {
return partial_.Pop();
}
}
return PopEmptyBlock();
}
template <int BlockSize>
typename BlockStack<BlockSize>::Block* BlockStack<BlockSize>::PopEmptyBlock() {
{
MutexLocker ml(global_mutex_);
if (!global_empty_->IsEmpty()) {
return global_empty_->Pop();
}
}
return new Block();
}
template <int BlockSize>
typename BlockStack<BlockSize>::Block*
BlockStack<BlockSize>::PopNonEmptyBlock() {
MutexLocker ml(&mutex_);
if (!full_.IsEmpty()) {
return full_.Pop();
} else if (!partial_.IsEmpty()) {
return partial_.Pop();
} else {
return NULL;
}
}
template <int BlockSize>
bool BlockStack<BlockSize>::IsEmpty() {
MutexLocker ml(&mutex_);
return full_.IsEmpty() && partial_.IsEmpty();
}
template <int BlockSize>
BlockStack<BlockSize>::List::~List() {
while (!IsEmpty()) {
delete Pop();
}
}
template <int BlockSize>
typename BlockStack<BlockSize>::Block* BlockStack<BlockSize>::List::Pop() {
Block* result = head_;
head_ = head_->next_;
--length_;
result->next_ = NULL;
return result;
}
template <int BlockSize>
typename BlockStack<BlockSize>::Block* BlockStack<BlockSize>::List::PopAll() {
Block* result = head_;
head_ = NULL;
length_ = 0;
return result;
}
template <int BlockSize>
void BlockStack<BlockSize>::List::Push(Block* block) {
ASSERT(block->next_ == NULL);
block->next_ = head_;
head_ = block;
++length_;
}
bool StoreBuffer::Overflowed() {
MutexLocker ml(&mutex_);
return (full_.length() + partial_.length()) > kMaxNonEmpty;
}
void StoreBuffer::VisitObjectPointers(ObjectPointerVisitor* visitor) {
for (Block* block = full_.Peek(); block != NULL; block = block->next()) {
block->VisitObjectPointers(visitor);
}
for (Block* block = partial_.Peek(); block != NULL; block = block->next()) {
block->VisitObjectPointers(visitor);
}
}
template <int BlockSize>
void BlockStack<BlockSize>::TrimGlobalEmpty() {
DEBUG_ASSERT(global_mutex_->IsOwnedByCurrentThread());
while (global_empty_->length() > kMaxGlobalEmpty) {
delete global_empty_->Pop();
}
}
template class BlockStack<kStoreBufferBlockSize>;
template class BlockStack<kMarkingStackBlockSize>;
} // namespace dart