Bug 811596 - Shrink the IPC message buffer after each message is processed. r=cjones.
authorNicholas Nethercote <nnethercote@mozilla.com>
Mon, 19 Nov 2012 20:52:44 -0800
changeset 113758 ac86426a764e8e794253b46e89ca7956466d6be1
parent 113757 a670db3bb81628db11c57054d901b4ed29743f07
child 113759 26544b27ef13ac7172b0fecccf8a90c1c2db8b19
push id18370
push usernnethercote@mozilla.com
push dateTue, 20 Nov 2012 08:56:11 +0000
treeherdermozilla-inbound@ac86426a764e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscjones
bugs811596
milestone20.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 811596 - Shrink the IPC message buffer after each message is processed. r=cjones.
ipc/chromium/src/chrome/common/ipc_channel_posix.cc
--- a/ipc/chromium/src/chrome/common/ipc_channel_posix.cc
+++ b/ipc/chromium/src/chrome/common/ipc_channel_posix.cc
@@ -358,16 +358,26 @@ bool Channel::ChannelImpl::EnqueueHelloM
     Close();
     return false;
   }
 
   output_queue_.push(msg.release());
   return true;
 }
 
+static void
+ClearAndShrink(std::string& s, size_t capacity)
+{
+  // 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);
+}
+
 bool Channel::ChannelImpl::Connect() {
   if (mode_ == MODE_SERVER && uses_fifo_) {
     if (server_listen_pipe_ == -1) {
       return false;
     }
     MessageLoopForIO::current()->WatchFileDescriptor(
         server_listen_pipe_,
         true,
@@ -484,17 +494,17 @@ bool Channel::ChannelImpl::ProcessIncomi
     const char *end;
     if (input_overflow_buf_.empty()) {
       overflowp = NULL;
       p = input_buf_;
       end = p + bytes_read;
     } else {
       if (input_overflow_buf_.size() >
          static_cast<size_t>(kMaximumMessageSize - bytes_read)) {
-        input_overflow_buf_.clear();
+        ClearAndShrink(input_overflow_buf_, Channel::kReadBufferSize);
         LOG(ERROR) << "IPC message is too big";
         return false;
       }
       input_overflow_buf_.append(input_buf_, bytes_read);
       overflowp = p = input_overflow_buf_.data();
       end = p + input_overflow_buf_.size();
     }
 
@@ -568,17 +578,17 @@ bool Channel::ChannelImpl::ProcessIncomi
         }
         p = message_tail;
       } else {
         // Last message is partial.
         break;
       }
     }
     if (end == p) {
-      input_overflow_buf_.clear();
+      ClearAndShrink(input_overflow_buf_, Channel::kReadBufferSize);
     } else if (!overflowp) {
       // p is from input_buf_
       input_overflow_buf_.assign(p, end - p);
     } else if (p > overflowp) {
       // p is from input_overflow_buf_
       input_overflow_buf_.erase(0, p - overflowp);
     }
     input_overflow_fds_ = std::vector<int>(&fds[fds_i], &fds[num_fds]);