From f50b6cd93438638fccf7d7b7ceb6129f074bddc5 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 23 Oct 2014 01:34:07 -0700 Subject: [PATCH] Bug 1087834 - Reduce heap churn involving |input_overflow_buf_|. r=billm. --HG-- extra : rebase_source : f842309c5d75479fefc266d405c0b8aceb7c247f --- .../src/chrome/common/ipc_channel_posix.cc | 24 ++++++++++++------- .../src/chrome/common/ipc_channel_posix.h | 2 ++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/ipc/chromium/src/chrome/common/ipc_channel_posix.cc b/ipc/chromium/src/chrome/common/ipc_channel_posix.cc index 697cec5b4608..66d87ccc22c4 100644 --- a/ipc/chromium/src/chrome/common/ipc_channel_posix.cc +++ b/ipc/chromium/src/chrome/common/ipc_channel_posix.cc @@ -383,14 +383,20 @@ bool Channel::ChannelImpl::EnqueueHelloMessage() { return true; } -static void -ClearAndShrink(std::string& s, size_t capacity) +void Channel::ChannelImpl::ClearAndShrinkInputOverflowBuf() { - // This swap trick is the closest thing C++ has to a guaranteed way to - // shrink the capacity of a string. - std::string tmp; - tmp.reserve(capacity); - s.swap(tmp); + // If input_overflow_buf_ has grown, shrink it back to its normal size. + static size_t previousCapacityAfterClearing = 0; + if (input_overflow_buf_.capacity() > previousCapacityAfterClearing) { + // This swap trick is the closest thing C++ has to a guaranteed way + // to shrink the capacity of a string. + std::string tmp; + tmp.reserve(Channel::kReadBufferSize); + input_overflow_buf_.swap(tmp); + previousCapacityAfterClearing = input_overflow_buf_.capacity(); + } else { + input_overflow_buf_.clear(); + } } bool Channel::ChannelImpl::Connect() { @@ -519,7 +525,7 @@ bool Channel::ChannelImpl::ProcessIncomingMessages() { } else { if (input_overflow_buf_.size() > static_cast(kMaximumMessageSize - bytes_read)) { - ClearAndShrink(input_overflow_buf_, Channel::kReadBufferSize); + ClearAndShrinkInputOverflowBuf(); CHROMIUM_LOG(ERROR) << "IPC message is too big"; return false; } @@ -628,7 +634,7 @@ bool Channel::ChannelImpl::ProcessIncomingMessages() { } } if (end == p) { - ClearAndShrink(input_overflow_buf_, Channel::kReadBufferSize); + ClearAndShrinkInputOverflowBuf(); } else if (!overflowp) { // p is from input_buf_ input_overflow_buf_.assign(p, end - p); diff --git a/ipc/chromium/src/chrome/common/ipc_channel_posix.h b/ipc/chromium/src/chrome/common/ipc_channel_posix.h index c97e2e6ae47b..25ce3907087b 100644 --- a/ipc/chromium/src/chrome/common/ipc_channel_posix.h +++ b/ipc/chromium/src/chrome/common/ipc_channel_posix.h @@ -59,6 +59,8 @@ class Channel::ChannelImpl : public MessageLoopForIO::Watcher { bool ProcessIncomingMessages(); bool ProcessOutgoingMessages(); + void ClearAndShrinkInputOverflowBuf(); + // MessageLoopForIO::Watcher implementation. virtual void OnFileCanReadWithoutBlocking(int fd); virtual void OnFileCanWriteWithoutBlocking(int fd);