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

Unified Diff: cc/base/contiguous_container.cc

Issue 2277433002: Defer allocation in cc::ContiguousContainerBase::Buffer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 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
« no previous file with comments | « no previous file | cc/base/contiguous_container_unittest.cc » ('j') | cc/base/contiguous_container_unittest.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cc/base/contiguous_container.cc
diff --git a/cc/base/contiguous_container.cc b/cc/base/contiguous_container.cc
index d6280b43290b8d4d9547f56a1a62b070caf63e66..3fdb213d29bed89dec6d228dbfa7392eeabfcaf2 100644
--- a/cc/base/contiguous_container.cc
+++ b/cc/base/contiguous_container.cc
@@ -16,18 +16,22 @@ static const unsigned kDefaultInitialBufferSize = 32;
class ContiguousContainerBase::Buffer {
public:
- explicit Buffer(size_t buffer_size)
- : data_(new char[buffer_size]), end_(begin()), capacity_(buffer_size) {}
+ explicit Buffer(size_t buffer_size) : end_(nullptr), capacity_(buffer_size) {}
~Buffer() {}
size_t Capacity() const { return capacity_; }
size_t UsedCapacity() const { return end_ - begin(); }
size_t UnusedCapacity() const { return Capacity() - UsedCapacity(); }
+ size_t MemoryUsage() const { return begin() ? capacity_ : 0; }
bool empty() const { return UsedCapacity() == 0; }
void* Allocate(size_t object_size) {
DCHECK_GE(UnusedCapacity(), object_size);
+ if (!data_) {
+ data_.reset(new char[capacity_]);
+ end_ = begin();
+ }
void* result = end_;
end_ += object_size;
return result;
@@ -40,8 +44,8 @@ class ContiguousContainerBase::Buffer {
}
private:
- char* begin() { return &data_[0]; }
- const char* begin() const { return &data_[0]; }
+ char* begin() { return data_.get(); }
+ const char* begin() const { return data_.get(); }
// begin() <= end_ <= begin() + capacity_
std::unique_ptr<char[]> data_;
@@ -76,7 +80,11 @@ size_t ContiguousContainerBase::UsedCapacityInBytes() const {
}
size_t ContiguousContainerBase::MemoryUsageInBytes() const {
- return sizeof(*this) + GetCapacityInBytes() +
+ size_t memory_usage = 0;
+ for (const auto& buffer : buffers_) {
danakj 2016/08/23 18:16:00 nit: no {}
Dmitry Skiba 2016/08/24 17:58:46 Done.
+ memory_usage += buffer->MemoryUsage();
+ }
+ return sizeof(*this) + memory_usage +
elements_.capacity() * sizeof(elements_[0]);
}
« no previous file with comments | « no previous file | cc/base/contiguous_container_unittest.cc » ('j') | cc/base/contiguous_container_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698