Commit bacdbb56 authored by Eric Roman's avatar Eric Roman Committed by Commit Bot

Optimize generated code for NetLogWithSource.

 (1) Remove null checks for NetLogWithSource::IsCapturing()
 (2) Inline NetLogWithSource::IsCapturing()

This also results in a binary size reduction (546 bytes for Android Arm).

Bug: 1006900
Change-Id: I9a947885b3690f4bd8b6f429b5104998f9a3a346
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1819946
Commit-Queue: Eric Roman <eroman@chromium.org>
Reviewed-by: default avatarRyan Hamilton <rch@chromium.org>
Cr-Commit-Position: refs/heads/master@{#699502}
parent e9922eb6
...@@ -56,11 +56,6 @@ uint32_t NetLog::NextID() { ...@@ -56,11 +56,6 @@ uint32_t NetLog::NextID() {
return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1); return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1);
} }
bool NetLog::IsCapturing() const {
CheckAlive();
return GetObserverCaptureModes() != 0;
}
void NetLog::AddObserver(NetLog::ThreadSafeObserver* observer, void NetLog::AddObserver(NetLog::ThreadSafeObserver* observer,
NetLogCaptureMode capture_mode) { NetLogCaptureMode capture_mode) {
base::AutoLock lock(lock_); base::AutoLock lock(lock_);
...@@ -203,10 +198,6 @@ void NetLog::AddEntryInternal(NetLogEventType type, ...@@ -203,10 +198,6 @@ void NetLog::AddEntryInternal(NetLogEventType type,
} }
} }
NetLogCaptureModeSet NetLog::GetObserverCaptureModes() const {
return base::subtle::NoBarrier_Load(&observer_capture_modes_);
}
void NetLog::AddEntryWithMaterializedParams(NetLogEventType type, void NetLog::AddEntryWithMaterializedParams(NetLogEventType type,
const NetLogSource& source, const NetLogSource& source,
NetLogEventPhase phase, NetLogEventPhase phase,
......
...@@ -228,7 +228,10 @@ class NET_EXPORT NetLog { ...@@ -228,7 +228,10 @@ class NET_EXPORT NetLog {
// //
// TODO(eroman): Survey current callsites; most are probably not necessary, // TODO(eroman): Survey current callsites; most are probably not necessary,
// and may even be harmful. // and may even be harmful.
bool IsCapturing() const; bool IsCapturing() const {
CheckAlive();
return GetObserverCaptureModes() != 0;
}
// Adds an observer and sets its log capture mode. The observer must not be // Adds an observer and sets its log capture mode. The observer must not be
// watching any NetLog, including this one, when this is called. // watching any NetLog, including this one, when this is called.
...@@ -295,7 +298,9 @@ class NET_EXPORT NetLog { ...@@ -295,7 +298,9 @@ class NET_EXPORT NetLog {
const GetParamsInterface* get_params); const GetParamsInterface* get_params);
// Returns the set of all capture modes being observed. // Returns the set of all capture modes being observed.
NetLogCaptureModeSet GetObserverCaptureModes() const; NetLogCaptureModeSet GetObserverCaptureModes() const {
return base::subtle::NoBarrier_Load(&observer_capture_modes_);
}
// Adds an entry using already materialized parameters, when it is already // Adds an entry using already materialized parameters, when it is already
// known that the log is capturing (goes straight to acquiring observer lock). // known that the log is capturing (goes straight to acquiring observer lock).
......
...@@ -33,13 +33,25 @@ base::Value BytesTransferredParams(int byte_count, ...@@ -33,13 +33,25 @@ base::Value BytesTransferredParams(int byte_count,
} // namespace } // namespace
NetLogWithSource::NetLogWithSource() {
// Conceptually, default NetLogWithSource have no NetLog*, and will return
// nullptr when calling |net_log()|. However for performance reasons, we
// always store a non-null member to the NetLog in order to avoid needing
// null checks for critical codepaths.
//
// The "dummy" net log used here will always return false for IsCapturing(),
// and have no sideffects should its method be called. In practice the only
// method that will get called on it is IsCapturing().
static NetLog* dummy = new NetLog();
DCHECK(!dummy->IsCapturing());
non_null_net_log_ = dummy;
}
NetLogWithSource::~NetLogWithSource() {} NetLogWithSource::~NetLogWithSource() {}
void NetLogWithSource::AddEntry(NetLogEventType type, void NetLogWithSource::AddEntry(NetLogEventType type,
NetLogEventPhase phase) const { NetLogEventPhase phase) const {
if (!net_log_) non_null_net_log_->AddEntry(type, source_, phase);
return;
net_log_->AddEntry(type, source_, phase);
} }
void NetLogWithSource::AddEvent(NetLogEventType type) const { void NetLogWithSource::AddEvent(NetLogEventType type) const {
...@@ -138,10 +150,6 @@ void NetLogWithSource::AddByteTransferEvent(NetLogEventType event_type, ...@@ -138,10 +150,6 @@ void NetLogWithSource::AddByteTransferEvent(NetLogEventType event_type,
}); });
} }
bool NetLogWithSource::IsCapturing() const {
return net_log_ && net_log_->IsCapturing();
}
// static // static
NetLogWithSource NetLogWithSource::Make(NetLog* net_log, NetLogWithSource NetLogWithSource::Make(NetLog* net_log,
NetLogSourceType source_type) { NetLogSourceType source_type) {
...@@ -152,4 +160,10 @@ NetLogWithSource NetLogWithSource::Make(NetLog* net_log, ...@@ -152,4 +160,10 @@ NetLogWithSource NetLogWithSource::Make(NetLog* net_log,
return NetLogWithSource(source, net_log); return NetLogWithSource(source, net_log);
} }
NetLog* NetLogWithSource::net_log() const {
if (source_.IsValid())
return non_null_net_log_;
return nullptr;
}
} // namespace net } // namespace net
...@@ -18,7 +18,7 @@ class NetLog; ...@@ -18,7 +18,7 @@ class NetLog;
// output log messages without needing to pass in the source. // output log messages without needing to pass in the source.
class NET_EXPORT NetLogWithSource { class NET_EXPORT NetLogWithSource {
public: public:
NetLogWithSource() : net_log_(nullptr) {} NetLogWithSource();
~NetLogWithSource(); ~NetLogWithSource();
// Adds a log entry to the NetLog for the bound source. // Adds a log entry to the NetLog for the bound source.
...@@ -29,10 +29,7 @@ class NET_EXPORT NetLogWithSource { ...@@ -29,10 +29,7 @@ class NET_EXPORT NetLogWithSource {
void AddEntry(NetLogEventType type, void AddEntry(NetLogEventType type,
NetLogEventPhase phase, NetLogEventPhase phase,
const ParametersCallback& get_params) const { const ParametersCallback& get_params) const {
// TODO(eroman): Should merge the nullity check with non_null_net_log_->AddEntry(type, source_, phase, get_params);
// GetObserverCaptureModes() to reduce expanded code size.
if (net_log_)
net_log_->AddEntry(type, source_, phase, get_params);
} }
// Convenience methods that call AddEntry with a fixed "capture phase" // Convenience methods that call AddEntry with a fixed "capture phase"
...@@ -119,7 +116,7 @@ class NET_EXPORT NetLogWithSource { ...@@ -119,7 +116,7 @@ class NET_EXPORT NetLogWithSource {
int byte_count, int byte_count,
const char* bytes) const; const char* bytes) const;
bool IsCapturing() const; bool IsCapturing() const { return non_null_net_log_->IsCapturing(); }
// Helper to create a NetLogWithSource given a NetLog and a NetLogSourceType. // Helper to create a NetLogWithSource given a NetLog and a NetLogSourceType.
// Takes care of creating a unique source ID, and handles // Takes care of creating a unique source ID, and handles
...@@ -127,14 +124,28 @@ class NET_EXPORT NetLogWithSource { ...@@ -127,14 +124,28 @@ class NET_EXPORT NetLogWithSource {
static NetLogWithSource Make(NetLog* net_log, NetLogSourceType source_type); static NetLogWithSource Make(NetLog* net_log, NetLogSourceType source_type);
const NetLogSource& source() const { return source_; } const NetLogSource& source() const { return source_; }
NetLog* net_log() const { return net_log_; }
// Returns the bound NetLog*, or nullptr.
NetLog* net_log() const;
private: private:
NetLogWithSource(const NetLogSource& source, NetLog* net_log) NetLogWithSource(const NetLogSource& source, NetLog* non_null_net_log)
: source_(source), net_log_(net_log) {} : source_(source), non_null_net_log_(non_null_net_log) {}
NetLogSource source_; NetLogSource source_;
NetLog* net_log_;
// There are two types of NetLogWithSource:
//
// (a) An ordinary NetLogWithSource for which |source().IsValid()| and
// |net_log() != nullptr|
//
// (b) A default constructed NetLogWithSource for which
// |!source().IsValid()| and |net_log() == nullptr|.
//
// As an optimization, both types internally store a non-null NetLog*. This
// way no null checks are needed before dispatching to the (possibly dummy)
// NetLog
NetLog* non_null_net_log_;
}; };
} // namespace net } // namespace net
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment