From ee10ea8a4181dcad806ffb583495766da102528e Mon Sep 17 00:00:00 2001 From: Karl Wiberg Date: Fri, 4 May 2018 13:27:48 +0200 Subject: [PATCH] RTC_LOG(): Internally, pass logging severity as template argument When the logging severity is statically known, passing it as a template argument instead of as a function argument saves space at the call site. In aggregate, this reduces the size of libjingle_peerconnection_so.so by 8 kB. Bug: webrtc:9185 Change-Id: I9ca363845216370e97b230952c86e6d07719962f Reviewed-on: https://webrtc-review.googlesource.com/74480 Reviewed-by: Fredrik Solenberg Reviewed-by: Jonas Olsson Commit-Queue: Karl Wiberg Cr-Commit-Position: refs/heads/master@{#23121} --- rtc_base/BUILD.gn | 1 + rtc_base/logging.h | 29 +++++++++++++++++++++-------- rtc_base/system/BUILD.gn | 6 ++++++ rtc_base/system/no_inline.h | 22 ++++++++++++++++++++++ 4 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 rtc_base/system/no_inline.h diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index 35c9da895a..c3d05465f2 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -218,6 +218,7 @@ rtc_source_set("logging") { "logging.cc", "logging.h", ] + deps += [ "system:no_inline" ] # logging.h needs the deprecation header while downstream projects are # removing code that depends on logging implementation details. diff --git a/rtc_base/logging.h b/rtc_base/logging.h index 82960a48dd..6ff31f3fda 100644 --- a/rtc_base/logging.h +++ b/rtc_base/logging.h @@ -58,6 +58,7 @@ #include "rtc_base/basictypes.h" #include "rtc_base/constructormagic.h" #include "rtc_base/deprecation.h" +#include "rtc_base/system/no_inline.h" #include "rtc_base/thread_annotations.h" #if !defined(NDEBUG) || defined(DLOG_ALWAYS_ON) @@ -142,6 +143,16 @@ class LogMessage { ~LogMessage(); static bool Loggable(LoggingSeverity sev); + + // Same as the above, but using a template argument instead of a function + // argument. (When the logging severity is statically known, passing it as a + // template argument instead of as a function argument saves space at the + // call site.) + template + RTC_NO_INLINE static bool Loggable() { + return Loggable(S); + } + std::ostream& stream(); // Returns the time at which this function was called for the first time. @@ -268,9 +279,11 @@ class LogMessageVoidify { ? (void) 0 \ : rtc::LogMessageVoidify() & -#define RTC_LOG(sev) \ - RTC_LOG_SEVERITY_PRECONDITION(rtc::sev) \ - rtc::LogMessage(__FILE__, __LINE__, rtc::sev).stream() +#define RTC_LOG_SEVERITY_PRECONDITION_C(sev) \ + !(rtc::LogMessage::Loggable()) ? (void)0 : rtc::LogMessageVoidify()& +#define RTC_LOG(sev) \ + RTC_LOG_SEVERITY_PRECONDITION_C(sev) \ + rtc::LogMessage(__FILE__, __LINE__, rtc::sev).stream() // The _V version is for when a variable is passed in. It doesn't do the // namespace concatenation. @@ -297,11 +310,11 @@ inline bool LogCheckLevel(LoggingSeverity sev) { return (LogMessage::GetMinLogSeverity() <= sev); } -#define RTC_LOG_E(sev, ctx, err, ...) \ - RTC_LOG_SEVERITY_PRECONDITION(rtc::sev) \ - rtc::LogMessage(__FILE__, __LINE__, rtc::sev, \ - rtc::ERRCTX_ ## ctx, err , ##__VA_ARGS__) \ - .stream() +#define RTC_LOG_E(sev, ctx, err, ...) \ + RTC_LOG_SEVERITY_PRECONDITION_C(sev) \ + rtc::LogMessage(__FILE__, __LINE__, rtc::sev, rtc::ERRCTX_##ctx, err, \ + ##__VA_ARGS__) \ + .stream() #define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": " diff --git a/rtc_base/system/BUILD.gn b/rtc_base/system/BUILD.gn index 03b57c296b..0e42eba9aa 100644 --- a/rtc_base/system/BUILD.gn +++ b/rtc_base/system/BUILD.gn @@ -42,3 +42,9 @@ rtc_source_set("ignore_warnings") { "ignore_warnings.h", ] } + +rtc_source_set("no_inline") { + sources = [ + "no_inline.h", + ] +} diff --git a/rtc_base/system/no_inline.h b/rtc_base/system/no_inline.h new file mode 100644 index 0000000000..c309e851b1 --- /dev/null +++ b/rtc_base/system/no_inline.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef RTC_BASE_SYSTEM_NO_INLINE_H_ +#define RTC_BASE_SYSTEM_NO_INLINE_H_ + +#if defined(_MSC_VER) +#define RTC_NO_INLINE __declspec(noinline) +#elif defined(__GNUC__) +#define RTC_NO_INLINE __attribute__((__noinline__)) +#else +#define RTC_NO_INLINE +#endif + +#endif // RTC_BASE_SYSTEM_NO_INLINE_H_