diff options
author | amistry <amistry@chromium.org> | 2016-03-14 15:50:24 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-03-14 22:51:58 +0000 |
commit | 33016d5a9b93156147fbcbd32a0922e89de91f5e (patch) | |
tree | 430cf98c4199debf232644f6e7d6ce4d79a342f7 /mojo | |
parent | 1693cacfdda556be0d56381104055f12829b6772 (diff) | |
download | chromium_src-33016d5a9b93156147fbcbd32a0922e89de91f5e.zip chromium_src-33016d5a9b93156147fbcbd32a0922e89de91f5e.tar.gz chromium_src-33016d5a9b93156147fbcbd32a0922e89de91f5e.tar.bz2 |
[mojo-sdk] Cache the connector's weak self to avoid a malloc/free on every incoming message.
BUG=None
Review URL: https://codereview.chromium.org/1802443002
Cr-Commit-Position: refs/heads/master@{#381100}
Diffstat (limited to 'mojo')
-rw-r--r-- | mojo/public/cpp/bindings/lib/connector.cc | 7 | ||||
-rw-r--r-- | mojo/public/cpp/bindings/lib/connector.h | 3 |
2 files changed, 7 insertions, 3 deletions
diff --git a/mojo/public/cpp/bindings/lib/connector.cc b/mojo/public/cpp/bindings/lib/connector.cc index c84a93d..065c176 100644 --- a/mojo/public/cpp/bindings/lib/connector.cc +++ b/mojo/public/cpp/bindings/lib/connector.cc @@ -57,6 +57,7 @@ Connector::Connector(ScopedMessagePipeHandle message_pipe, registered_with_sync_handle_watcher_(false), sync_handle_watcher_callback_count_(0), weak_factory_(this) { + weak_self_ = weak_factory_.GetWeakPtr(); // Even though we don't have an incoming receiver, we still want to monitor // the message pipe to know if is closed or encounters an error. WaitToReadMore(); @@ -253,7 +254,7 @@ void Connector::OnWatcherHandleReady(MojoResult result) { } void Connector::OnSyncHandleWatcherHandleReady(MojoResult result) { - base::WeakPtr<Connector> weak_self(weak_factory_.GetWeakPtr()); + base::WeakPtr<Connector> weak_self(weak_self_); sync_handle_watcher_callback_count_++; OnHandleReadyInternal(result); @@ -287,7 +288,7 @@ void Connector::WaitToReadMore() { // no longer be met, we signal the error asynchronously to avoid reentry. base::ThreadTaskRunnerHandle::Get()->PostTask( FROM_HERE, base::Bind(&Connector::OnWatcherHandleReady, - weak_factory_.GetWeakPtr(), rv)); + weak_self_, rv)); } if (register_sync_handle_watch_count_ > 0 && @@ -307,7 +308,7 @@ bool Connector::ReadSingleMessage(MojoResult* read_result) { // Detect if |this| was destroyed during message dispatch. Allow for the // possibility of re-entering ReadMore() through message dispatch. - base::WeakPtr<Connector> weak_self = weak_factory_.GetWeakPtr(); + base::WeakPtr<Connector> weak_self = weak_self_; Message message; const MojoResult rv = ReadMessage(message_pipe_.get(), &message); diff --git a/mojo/public/cpp/bindings/lib/connector.h b/mojo/public/cpp/bindings/lib/connector.h index 2989d70..e00936c 100644 --- a/mojo/public/cpp/bindings/lib/connector.h +++ b/mojo/public/cpp/bindings/lib/connector.h @@ -192,6 +192,9 @@ class Connector : public MessageReceiver { base::ThreadChecker thread_checker_; + // Create a single weak ptr and use it everywhere, to avoid the malloc/free + // cost of creating a new weak ptr whenever it is needed. + base::WeakPtr<Connector> weak_self_; base::WeakPtrFactory<Connector> weak_factory_; DISALLOW_COPY_AND_ASSIGN(Connector); |