Bug 1641614 - Use mozilla::Queue for ChannelImpl::output_queue_h_. r=froydnj

The goal of this patch is to reduce memory usage. On at least OSX, std::Queue
can use 4kb of memory even with nothing in it. This can be around 52kb of
memory per content process.

The segment size of 64 is fairly arbitrary, but these queues didn't have
more than a few hundred items in them, so it seemed like a reasonable
trade off between space for mostly-empty queues and segment overhead.

Differential Revision: https://phabricator.services.mozilla.com/D87325
This commit is contained in:
Andrew McCreight 2020-08-18 17:03:55 +00:00
parent fd9ca77e37
commit 681e8793ff
4 changed files with 24 additions and 24 deletions

View File

@ -520,17 +520,17 @@ bool Channel::ChannelImpl::ProcessOutgoingMessages() {
// no connection?
is_blocked_on_write_ = false;
if (output_queue_.empty()) return true;
if (output_queue_.IsEmpty()) return true;
if (pipe_ == -1) return false;
// Write out all the messages we can till the write blocks or there are no
// more outgoing messages.
while (!output_queue_.empty()) {
while (!output_queue_.IsEmpty()) {
#ifdef FUZZING
mozilla::ipc::Faulty::instance().MaybeCollectAndClosePipe(pipe_);
#endif
Message* msg = output_queue_.front().get();
Message* msg = output_queue_.FirstElement().get();
struct msghdr msgh = {0};
@ -709,8 +709,8 @@ bool Channel::ChannelImpl::ProcessOutgoingMessages() {
bool Channel::ChannelImpl::Send(mozilla::UniquePtr<Message> message) {
#ifdef IPC_MESSAGE_DEBUG_EXTRA
DLOG(INFO) << "sending message @" << message.get() << " on channel @" << this
<< " with type " << message->type() << " (" << output_queue_.size()
<< " in queue)";
<< " with type " << message->type() << " ("
<< output_queue_.Count() << " in queue)";
#endif
#ifdef FUZZING
@ -787,7 +787,7 @@ void Channel::ChannelImpl::OutputQueuePush(mozilla::UniquePtr<Message> msg) {
MOZ_DIAGNOSTIC_ASSERT(!closed_);
msg->AssertAsLargeAsHeader();
output_queue_.push(std::move(msg));
output_queue_.Push(std::move(msg));
output_queue_length_++;
}
@ -795,7 +795,7 @@ void Channel::ChannelImpl::OutputQueuePop() {
// Clear any reference to the front of output_queue_ before we destroy it.
partial_write_iter_.reset();
output_queue_.pop();
mozilla::UniquePtr<Message> message = output_queue_.Pop();
output_queue_length_--;
}
@ -831,7 +831,7 @@ void Channel::ChannelImpl::Close() {
client_pipe_ = -1;
}
while (!output_queue_.empty()) {
while (!output_queue_.IsEmpty()) {
OutputQueuePop();
}

View File

@ -11,7 +11,6 @@
#include <sys/socket.h> // for CMSG macros
#include <queue>
#include <string>
#include <vector>
#include <list>
@ -21,6 +20,7 @@
#include "chrome/common/file_descriptor_set_posix.h"
#include "mozilla/Maybe.h"
#include "mozilla/Queue.h"
#include "mozilla/UniquePtr.h"
namespace IPC {
@ -97,7 +97,7 @@ class Channel::ChannelImpl : public MessageLoopForIO::Watcher {
Listener* listener_;
// Messages to be sent are queued here.
std::queue<mozilla::UniquePtr<Message>> output_queue_;
mozilla::Queue<mozilla::UniquePtr<Message>, 64> output_queue_;
// We read from the pipe into these buffers.
size_t input_buf_offset_;
@ -158,7 +158,7 @@ class Channel::ChannelImpl : public MessageLoopForIO::Watcher {
uint32_t last_pending_fd_id_;
#endif
// This variable is updated so it matches output_queue_.size(), except we can
// This variable is updated so it matches output_queue_.Count(), except we can
// read output_queue_length_ from any thread (if we're OK getting an
// occasional out-of-date or bogus value). We use output_queue_length_ to
// implement Unsound_NumQueuedMessages.

View File

@ -116,12 +116,12 @@ void Channel::ChannelImpl::Init(Mode mode, Listener* listener) {
void Channel::ChannelImpl::OutputQueuePush(mozilla::UniquePtr<Message> msg) {
mozilla::LogIPCMessage::LogDispatchWithPid(msg.get(), other_pid_);
output_queue_.push(std::move(msg));
output_queue_.Push(std::move(msg));
output_queue_length_++;
}
void Channel::ChannelImpl::OutputQueuePop() {
output_queue_.pop();
mozilla::UniquePtr<Message> message = output_queue_.Pop();
output_queue_length_--;
}
@ -147,7 +147,7 @@ void Channel::ChannelImpl::Close() {
MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this);
}
while (!output_queue_.empty()) {
while (!output_queue_.IsEmpty()) {
OutputQueuePop();
}
@ -162,8 +162,8 @@ bool Channel::ChannelImpl::Send(mozilla::UniquePtr<Message> message) {
#ifdef IPC_MESSAGE_DEBUG_EXTRA
DLOG(INFO) << "sending message @" << message.get() << " on channel @" << this
<< " with type " << message->type() << " (" << output_queue_.size()
<< " in queue)";
<< " with type " << message->type() << " ("
<< output_queue_.Count() << " in queue)";
#endif
#ifdef FUZZING
@ -487,8 +487,8 @@ bool Channel::ChannelImpl::ProcessOutgoingMessages(
return false;
}
// Message was sent.
DCHECK(!output_queue_.empty());
Message* m = output_queue_.front().get();
DCHECK(!output_queue_.IsEmpty());
Message* m = output_queue_.FirstElement().get();
MOZ_RELEASE_ASSERT(partial_write_iter_.isSome());
Pickle::BufferList::IterImpl& iter = partial_write_iter_.ref();
@ -504,12 +504,12 @@ bool Channel::ChannelImpl::ProcessOutgoingMessages(
}
}
if (output_queue_.empty()) return true;
if (output_queue_.IsEmpty()) return true;
if (INVALID_HANDLE_VALUE == pipe_) return false;
// Write to pipe...
Message* m = output_queue_.front().get();
Message* m = output_queue_.FirstElement().get();
if (partial_write_iter_.isNothing()) {
Pickle::BufferList::IterImpl iter(m->Buffers());
@ -564,7 +564,7 @@ void Channel::ChannelImpl::OnIOCompleted(MessageLoopForIO::IOContext* context,
if (waiting_connect_) {
if (!ProcessConnection()) return;
// We may have some messages queued up to send...
if (!output_queue_.empty() && !output_state_.is_pending)
if (!output_queue_.IsEmpty() && !output_state_.is_pending)
ProcessOutgoingMessages(NULL, 0);
if (input_state_.is_pending) return;
// else, fall-through and look for incoming messages...

View File

@ -9,7 +9,6 @@
#include "chrome/common/ipc_channel.h"
#include <queue>
#include <string>
#include "base/message_loop.h"
@ -17,6 +16,7 @@
#include "nsISupportsImpl.h"
#include "mozilla/Maybe.h"
#include "mozilla/Queue.h"
#include "mozilla/UniquePtr.h"
namespace IPC {
@ -85,7 +85,7 @@ class Channel::ChannelImpl : public MessageLoopForIO::IOHandler {
Listener* listener_;
// Messages to be sent are queued here.
std::queue<mozilla::UniquePtr<Message>> output_queue_;
mozilla::Queue<mozilla::UniquePtr<Message>, 64> output_queue_;
// If sending a message blocks then we use this iterator to keep track of
// where in the message we are. It gets reset when the message is finished
@ -117,7 +117,7 @@ class Channel::ChannelImpl : public MessageLoopForIO::IOHandler {
// record this when generating logs of IPC messages.
int32_t other_pid_ = -1;
// This variable is updated so it matches output_queue_.size(), except we can
// This variable is updated so it matches output_queue_.Count(), except we can
// read output_queue_length_ from any thread (if we're OK getting an
// occasional out-of-date or bogus value). We use output_queue_length_ to
// implement Unsound_NumQueuedMessages.