diff options
author | primiano <primiano@chromium.org> | 2015-07-24 13:13:32 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-07-24 20:14:12 +0000 |
commit | 32a7f50559fbaf7319a32048383d4c790f2a3c20 (patch) | |
tree | 8939e99b7854edf13333191e09f5e21f57e539db /base/pickle.cc | |
parent | 7ffc57805f72faabad89d096cca46a70954b8b12 (diff) | |
download | chromium_src-32a7f50559fbaf7319a32048383d4c790f2a3c20.zip chromium_src-32a7f50559fbaf7319a32048383d4c790f2a3c20.tar.gz chromium_src-32a7f50559fbaf7319a32048383d4c790f2a3c20.tar.bz2 |
Align base::Pickle allocations to 4k boundaries
Currently the growth logic of base::Pickle (the one that kicks
in exceeding the current capacity when writing) is as follows:
new_payload_capacity = current_payload_capacity * 2
with an initial payload capacity of 64 bytes.
However, the size of the heap held by Pickle (the one which gets
realloc-ated on each growth) is:
payload_capacity + header_size (typically 4-20 bytes).
In practice, this means that when the Pickle is expanded, the
size of its underlying heap becomes a power of two plus some extra.
This causes funky behaviors of realloc, which makes extremely
difficult to estimate the size of a pickle (for the sake of
memory profiling, Pickle is used to store TracedValue(s)).
This CL adds a small adjustment to the Pickle growth strategy,
making the underlying realloc sizes close to a full page size.
This in turn makes the realloc behavior much more predictable.
As a bonus this CL refactors the use of "Align", moving it to
base/bits.h
BUG=512383
Review URL: https://codereview.chromium.org/1249643007
Cr-Commit-Position: refs/heads/master@{#340318}
Diffstat (limited to 'base/pickle.cc')
-rw-r--r-- | base/pickle.cc | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/base/pickle.cc b/base/pickle.cc index cf4a865..d4487d4 100644 --- a/base/pickle.cc +++ b/base/pickle.cc @@ -8,6 +8,9 @@ #include <algorithm> // for max() +#include "base/bits.h" +#include "base/macros.h" + namespace base { // static @@ -34,7 +37,7 @@ inline bool PickleIterator::ReadBuiltinType(Type* result) { } inline void PickleIterator::Advance(size_t size) { - size_t aligned_size = AlignInt(size, sizeof(uint32_t)); + size_t aligned_size = bits::Align(size, sizeof(uint32_t)); if (end_index_ - read_index_ < aligned_size) { read_index_ = end_index_; } else { @@ -210,13 +213,15 @@ Pickle::Pickle() header_size_(sizeof(Header)), capacity_after_header_(0), write_offset_(0) { + static_assert((Pickle::kPayloadUnit & (Pickle::kPayloadUnit - 1)) == 0, + "Pickle::kPayloadUnit must be a power of two"); Resize(kPayloadUnit); header_->payload_size = 0; } Pickle::Pickle(int header_size) : header_(NULL), - header_size_(AlignInt(header_size, sizeof(uint32))), + header_size_(bits::Align(header_size, sizeof(uint32))), capacity_after_header_(0), write_offset_(0) { DCHECK_GE(static_cast<size_t>(header_size), sizeof(Header)); @@ -236,7 +241,7 @@ Pickle::Pickle(const char* data, int data_len) if (header_size_ > static_cast<unsigned int>(data_len)) header_size_ = 0; - if (header_size_ != AlignInt(header_size_, sizeof(uint32))) + if (header_size_ != bits::Align(header_size_, sizeof(uint32))) header_size_ = 0; // If there is anything wrong with the data, we're not going to use it. @@ -305,7 +310,7 @@ bool Pickle::WriteBytes(const void* data, int length) { } void Pickle::Reserve(size_t length) { - size_t data_len = AlignInt(length, sizeof(uint32)); + size_t data_len = bits::Align(length, sizeof(uint32)); DCHECK_GE(data_len, length); #ifdef ARCH_CPU_64_BITS DCHECK_LE(data_len, kuint32max); @@ -318,7 +323,7 @@ void Pickle::Reserve(size_t length) { void Pickle::Resize(size_t new_capacity) { CHECK_NE(capacity_after_header_, kCapacityReadOnly); - capacity_after_header_ = AlignInt(new_capacity, kPayloadUnit); + capacity_after_header_ = bits::Align(new_capacity, kPayloadUnit); void* p = realloc(header_, GetTotalAllocatedSize()); CHECK(p); header_ = reinterpret_cast<Header*>(p); @@ -334,7 +339,7 @@ size_t Pickle::GetTotalAllocatedSize() const { const char* Pickle::FindNext(size_t header_size, const char* start, const char* end) { - DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32))); + DCHECK_EQ(header_size, bits::Align(header_size, sizeof(uint32))); DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit)); size_t length = static_cast<size_t>(end - start); @@ -359,7 +364,7 @@ inline void Pickle::WriteBytesCommon(const void* data, size_t length) { DCHECK_NE(kCapacityReadOnly, capacity_after_header_) << "oops: pickle is readonly"; MSAN_CHECK_MEM_IS_INITIALIZED(data, length); - size_t data_len = AlignInt(length, sizeof(uint32)); + size_t data_len = bits::Align(length, sizeof(uint32)); DCHECK_GE(data_len, length); #ifdef ARCH_CPU_64_BITS DCHECK_LE(data_len, kuint32max); @@ -367,7 +372,11 @@ inline void Pickle::WriteBytesCommon(const void* data, size_t length) { DCHECK_LE(write_offset_, kuint32max - data_len); size_t new_size = write_offset_ + data_len; if (new_size > capacity_after_header_) { - Resize(std::max(capacity_after_header_ * 2, new_size)); + size_t new_capacity = capacity_after_header_ * 2; + const size_t kPickleHeapAlign = 4096; + if (new_capacity > kPickleHeapAlign) + new_capacity = bits::Align(new_capacity, kPickleHeapAlign) - kPayloadUnit; + Resize(std::max(new_capacity, new_size)); } char* write = mutable_payload() + write_offset_; |