Force-inline character array streaming on LogMessage and compute string length out of line. Logging statements are marked cold, so the compiler skips inlining the character array streaming templates and emits a separate function for every distinct array length in each translation unit. This eliminates per-length template instantiations and keeps callsites smaller by computing the string length at runtime, matching the existing trade-off for source filenames. PiperOrigin-RevId: 1015370884
diff --git a/absl/log/internal/log_message.cc b/absl/log/internal/log_message.cc index 8bdbf60..937efbf 100644 --- a/absl/log/internal/log_message.cc +++ b/absl/log/internal/log_message.cc
@@ -426,22 +426,22 @@ } // NOLINTBEGIN(runtime/int) // NOLINTBEGIN(google-runtime-int) -template void LogMessage::Stream(const char& v); -template void LogMessage::Stream(const signed char& v); -template void LogMessage::Stream(const unsigned char& v); -template void LogMessage::Stream(const short& v); -template void LogMessage::Stream(const unsigned short& v); -template void LogMessage::Stream(const int& v); -template void LogMessage::Stream(const unsigned int& v); -template void LogMessage::Stream(const long& v); -template void LogMessage::Stream(const unsigned long& v); -template void LogMessage::Stream(const long long& v); -template void LogMessage::Stream(const unsigned long long& v); -template void LogMessage::Stream(void* const& v); -template void LogMessage::Stream(const void* const& v); -template void LogMessage::Stream(const float& v); -template void LogMessage::Stream(const double& v); -template void LogMessage::Stream(const bool& v); +template void LogMessage::Stream(char v); +template void LogMessage::Stream(signed char v); +template void LogMessage::Stream(unsigned char v); +template void LogMessage::Stream(short v); +template void LogMessage::Stream(unsigned short v); +template void LogMessage::Stream(int v); +template void LogMessage::Stream(unsigned int v); +template void LogMessage::Stream(long v); +template void LogMessage::Stream(unsigned long v); +template void LogMessage::Stream(long long v); +template void LogMessage::Stream(unsigned long long v); +template void LogMessage::Stream(void* absl_nullable v); +template void LogMessage::Stream(const void* absl_nullable v); +template void LogMessage::Stream(float v); +template void LogMessage::Stream(double v); +template void LogMessage::Stream(bool v); // NOLINTEND(google-runtime-int) // NOLINTEND(runtime/int) @@ -612,6 +612,16 @@ template <LogMessage::StringType str_type> ABSL_ATTRIBUTE_NOINLINE void LogMessage::CopyToEncodedBuffer( + const char* absl_nonnull str) { + CopyToEncodedBuffer<str_type>(absl::string_view(str)); +} +template void LogMessage::CopyToEncodedBuffer<LogMessage::StringType::kLiteral>( + const char* absl_nonnull str); +template void LogMessage::CopyToEncodedBuffer< + LogMessage::StringType::kNotLiteral>(const char* absl_nonnull str); + +template <LogMessage::StringType str_type> +ABSL_ATTRIBUTE_NOINLINE void LogMessage::CopyToEncodedBuffer( const std::string& str) { CopyToEncodedBuffer<str_type>(absl::string_view(str)); }
diff --git a/absl/log/internal/log_message.h b/absl/log/internal/log_message.h index 1663827..8e8bb41 100644 --- a/absl/log/internal/log_message.h +++ b/absl/log/internal/log_message.h
@@ -161,57 +161,74 @@ // By-value overloads for small, common types let us overlook common failures // to define globals and static data members (i.e. in a .cc file). + // Values are passed by value to `Stream` so they stay in registers. // NOLINTBEGIN(runtime/int) // NOLINTBEGIN(google-runtime-int) // clang-format off: The CUDA toolchain cannot handle these <<<'s ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(char v) { - return operator<< <char>(v); + Stream(v); + return *this; } ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(signed char v) { - return operator<< <signed char>(v); + Stream(v); + return *this; } ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(unsigned char v) { - return operator<< <unsigned char>(v); + Stream(v); + return *this; } ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(signed short v) { - return operator<< <signed short>(v); + Stream(v); + return *this; } ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(signed int v) { - return operator<< <signed int>(v); + Stream(v); + return *this; } ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(signed long v) { - return operator<< <signed long>(v); + Stream(v); + return *this; } ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(signed long long v) { - return operator<< <signed long long>(v); + Stream(v); + return *this; } ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(unsigned short v) { - return operator<< <unsigned short>(v); + Stream(v); + return *this; } ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(unsigned int v) { - return operator<< <unsigned int>(v); + Stream(v); + return *this; } ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(unsigned long v) { - return operator<< <unsigned long>(v); + Stream(v); + return *this; } ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(unsigned long long v) { - return operator<< <unsigned long long>(v); + Stream(v); + return *this; } ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(void* absl_nullable v) { - return operator<< <void*>(v); + Stream(v); + return *this; } ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<( const void* absl_nullable v) { - return operator<< <const void*>(v); + Stream(v); + return *this; } ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(float v) { - return operator<< <float>(v); + Stream(v); + return *this; } ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(double v) { - return operator<< <double>(v); + Stream(v); + return *this; } ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(bool v) { - return operator<< <bool>(v); + Stream(v); + return *this; } // clang-format on // NOLINTEND(google-runtime-int) @@ -248,23 +265,17 @@ // Literal strings. This allows us to record C string literals as literals in // the logging.proto.Value. // - // Allow this overload to be inlined to prevent generating instantiations of - // this template for every value of `SIZE` encountered in each source code - // file. That significantly increases linker input sizes. Inlining is cheap - // because the argument to this overload is almost always a string literal so - // the call to `strlen` can be replaced at compile time. The overloads for - // `char[]`/`wchar_t[]` below should not be inlined. The compiler typically - // does not have the string at compile time and cannot replace the call to - // `strlen` so inlining it increases the binary size. See the discussion on - // cl/107527369. + // Force-inlined to avoid per-`SIZE` template instantiations in cold blocks; + // passing `const char*` keeps callsites smaller at the cost of doing `strlen` + // at runtime. template <int SIZE> - LogMessage& operator<<(const char (&buf)[SIZE]); + ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(const char (&buf)[SIZE]); template <int SIZE> LogMessage& operator<<(const wchar_t (&buf)[SIZE]); // This prevents non-const `char[]` arrays from looking like literals. template <int SIZE> - ABSL_ATTRIBUTE_NOINLINE LogMessage& operator<<(char (&buf)[SIZE]); + ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(char (&buf)[SIZE]); // `wchar_t[SIZE]` is handled by `operator<< <const wchar_t*>`. // Types that support `AbslStringify()` are serialized that way. @@ -332,7 +343,7 @@ void ToSinkOnlyImpl(absl::LogSink* absl_nonnull sink); template <typename T> - ABSL_ATTRIBUTE_NOINLINE void Stream(const T& v); + ABSL_ATTRIBUTE_NOINLINE void Stream(T v); enum class StringType { kLiteral, @@ -340,6 +351,8 @@ }; template <StringType str_type> void CopyToEncodedBuffer(absl::string_view str); + template <StringType str_type> + void CopyToEncodedBuffer(const char* absl_nonnull str); // Keeps std::string data and size access out of callsites. template <StringType str_type> void CopyToEncodedBuffer(const std::string& str); @@ -418,26 +431,29 @@ template <typename T> inline LogMessage& LogMessage::operator<<(const T& v) { - Stream(v); + Stream<const T&>(v); return *this; } // Note: the following is declared `ABSL_ATTRIBUTE_NOINLINE` template <typename T> -void LogMessage::Stream(const T& v) { - if constexpr (absl::HasAbslStringify<T>::value) { +void LogMessage::Stream(T v) { + // `T` may be `const U&` (from the generic `operator<<`); strip `&` and + // `const` (preserving `volatile`) so trait and `NullGuard` lookups see `U`. + using RawT = std::remove_const_t<std::remove_reference_t<T>>; + if constexpr (absl::HasAbslStringify<RawT>::value) { StringifySink sink(*this); // Replace with public API. AbslStringify(sink, v); } else { OstreamView view(*data_); - view.stream() << log_internal::NullGuard<T>().Guard(v); + view.stream() << log_internal::NullGuard<RawT>().Guard(v); } } template <int SIZE> -LogMessage& LogMessage::operator<<(const char (&buf)[SIZE]) { - CopyToEncodedBuffer<StringType::kLiteral>(absl::string_view(buf)); +inline LogMessage& LogMessage::operator<<(const char (&buf)[SIZE]) { + CopyToEncodedBuffer<StringType::kLiteral>(buf); return *this; } @@ -447,10 +463,9 @@ return *this; } -// Note: the following is declared `ABSL_ATTRIBUTE_NOINLINE` template <int SIZE> -LogMessage& LogMessage::operator<<(char (&buf)[SIZE]) { - CopyToEncodedBuffer<StringType::kNotLiteral>(absl::string_view(buf)); +inline LogMessage& LogMessage::operator<<(char (&buf)[SIZE]) { + CopyToEncodedBuffer<StringType::kNotLiteral>(buf); return *this; } // We instantiate these specializations in the library's TU to save space in @@ -458,22 +473,22 @@ // emitting a function call either way. // NOLINTBEGIN(runtime/int) // NOLINTBEGIN(google-runtime-int) -extern template void LogMessage::Stream(const char& v); -extern template void LogMessage::Stream(const signed char& v); -extern template void LogMessage::Stream(const unsigned char& v); -extern template void LogMessage::Stream(const short& v); -extern template void LogMessage::Stream(const unsigned short& v); -extern template void LogMessage::Stream(const int& v); -extern template void LogMessage::Stream(const unsigned int& v); -extern template void LogMessage::Stream(const long& v); -extern template void LogMessage::Stream(const unsigned long& v); -extern template void LogMessage::Stream(const long long& v); -extern template void LogMessage::Stream(const unsigned long long& v); -extern template void LogMessage::Stream(void* absl_nullable const& v); -extern template void LogMessage::Stream(const void* absl_nullable const& v); -extern template void LogMessage::Stream(const float& v); -extern template void LogMessage::Stream(const double& v); -extern template void LogMessage::Stream(const bool& v); +extern template void LogMessage::Stream(char v); +extern template void LogMessage::Stream(signed char v); +extern template void LogMessage::Stream(unsigned char v); +extern template void LogMessage::Stream(short v); +extern template void LogMessage::Stream(unsigned short v); +extern template void LogMessage::Stream(int v); +extern template void LogMessage::Stream(unsigned int v); +extern template void LogMessage::Stream(long v); +extern template void LogMessage::Stream(unsigned long v); +extern template void LogMessage::Stream(long long v); +extern template void LogMessage::Stream(unsigned long long v); +extern template void LogMessage::Stream(void* absl_nullable v); +extern template void LogMessage::Stream(const void* absl_nullable v); +extern template void LogMessage::Stream(float v); +extern template void LogMessage::Stream(double v); +extern template void LogMessage::Stream(bool v); // NOLINTEND(google-runtime-int) // NOLINTEND(runtime/int) @@ -482,6 +497,10 @@ extern template void LogMessage::CopyToEncodedBuffer< LogMessage::StringType::kNotLiteral>(absl::string_view str); extern template void LogMessage::CopyToEncodedBuffer< + LogMessage::StringType::kLiteral>(const char* absl_nonnull str); +extern template void LogMessage::CopyToEncodedBuffer< + LogMessage::StringType::kNotLiteral>(const char* absl_nonnull str); +extern template void LogMessage::CopyToEncodedBuffer< LogMessage::StringType::kLiteral>(const std::string& str); extern template void LogMessage::CopyToEncodedBuffer< LogMessage::StringType::kNotLiteral>(const std::string& str);