Bug 1087834 - Reduce heap churn involving |input_overflow_buf_|. r=billm.

--HG--
extra : rebase_source : f842309c5d75479fefc266d405c0b8aceb7c247f
This commit is contained in:
Nicholas Nethercote 2014-10-23 01:34:07 -07:00
parent e396733de9
commit f50b6cd934
2 changed files with 17 additions and 9 deletions

View File

@ -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<size_t>(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);

View File

@ -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);