diff options
| author | pechatnov <[email protected]> | 2026-07-03 08:57:28 +0300 |
|---|---|---|
| committer | pechatnov <[email protected]> | 2026-07-03 09:26:55 +0300 |
| commit | 5feb5fe821a7b9db366fcc56b9ce4a4ea3500c03 (patch) | |
| tree | 7c5e72c58e26e66203618a5c8dcfa20e4a8beed3 /library/cpp | |
| parent | 9ee725581e8d8fe81cadd3d093e0477120c84328 (diff) | |
Optimize TLogger::WithTag/WithRawTag
Make TLogger::WithTag use a compile-time format string
commit_hash:0a119b8eb278bceb411b7d071bbefe0b41e6233d
Diffstat (limited to 'library/cpp')
| -rw-r--r-- | library/cpp/yt/logging/benchmark/logger_tag.cpp | 32 | ||||
| -rw-r--r-- | library/cpp/yt/logging/benchmark/ya.make | 14 | ||||
| -rw-r--r-- | library/cpp/yt/logging/logger-inl.h | 8 | ||||
| -rw-r--r-- | library/cpp/yt/logging/logger.cpp | 8 | ||||
| -rw-r--r-- | library/cpp/yt/logging/logger.h | 12 | ||||
| -rw-r--r-- | library/cpp/yt/logging/ya.make | 1 |
6 files changed, 61 insertions, 14 deletions
diff --git a/library/cpp/yt/logging/benchmark/logger_tag.cpp b/library/cpp/yt/logging/benchmark/logger_tag.cpp new file mode 100644 index 00000000000..cb6208db2a3 --- /dev/null +++ b/library/cpp/yt/logging/benchmark/logger_tag.cpp @@ -0,0 +1,32 @@ +#include <benchmark/benchmark.h> + +#include <library/cpp/yt/logging/logger.h> + +#include <library/cpp/yt/string/format.h> + +#include <library/cpp/yt/misc/guid.h> + +namespace NYT::NLogging { +namespace { + +//////////////////////////////////////////////////////////////////////////////// + +const TGuid RequestId(0x12345678, 0x9abcdef0, 0x11223344, 0x55667788); +const std::string User = "robot-benchmark-user"; +const std::string Realm = "some-realm"; + +void BM_WithTag(benchmark::State& state) +{ + TLogger logger("Benchmark"); + for (auto _ : state) { + auto tagged = logger.WithTag("RequestId: %v, User: %v, Realm: %v", RequestId, User, Realm); + benchmark::DoNotOptimize(tagged); + } +} + +BENCHMARK(BM_WithTag); + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace +} // namespace NYT::NLogging diff --git a/library/cpp/yt/logging/benchmark/ya.make b/library/cpp/yt/logging/benchmark/ya.make new file mode 100644 index 00000000000..61fd0b49276 --- /dev/null +++ b/library/cpp/yt/logging/benchmark/ya.make @@ -0,0 +1,14 @@ +G_BENCHMARK() + +INCLUDE(${ARCADIA_ROOT}/library/cpp/yt/ya_cpp.make.inc) + +SRCS( + logger_tag.cpp +) + +PEERDIR( + library/cpp/yt/logging + library/cpp/yt/string +) + +END() diff --git a/library/cpp/yt/logging/logger-inl.h b/library/cpp/yt/logging/logger-inl.h index e54952822c8..87672ec6f6a 100644 --- a/library/cpp/yt/logging/logger-inl.h +++ b/library/cpp/yt/logging/logger-inl.h @@ -22,9 +22,9 @@ inline bool TLogger::IsAnchorUpToDate(const TLoggingAnchor& anchor) const } template <class... TArgs> -void TLogger::AddTag(const char* format, TArgs&&... args) +void TLogger::AddTag(TFormatString<TArgs...> format, TArgs&&... args) { - AddRawTag(Format(TRuntimeFormat{format}, std::forward<TArgs>(args)...)); + AddRawTag(Format(format, std::forward<TArgs>(args)...)); } template <class TType> @@ -35,7 +35,7 @@ void TLogger::AddStructuredTag(TStringBuf key, TType value) } template <class... TArgs> -TLogger TLogger::WithTag(const char* format, TArgs&&... args) const & +TLogger TLogger::WithTag(TFormatString<TArgs...> format, TArgs&&... args) const & { auto result = *this; result.AddTag(format, std::forward<TArgs>(args)...); @@ -43,7 +43,7 @@ TLogger TLogger::WithTag(const char* format, TArgs&&... args) const & } template <class... TArgs> -TLogger TLogger::WithTag(const char* format, TArgs&&... args) && +TLogger TLogger::WithTag(TFormatString<TArgs...> format, TArgs&&... args) && { AddTag(format, std::forward<TArgs>(args)...); return std::move(*this); diff --git a/library/cpp/yt/logging/logger.cpp b/library/cpp/yt/logging/logger.cpp index 458d01d8133..64d8b06fa7e 100644 --- a/library/cpp/yt/logging/logger.cpp +++ b/library/cpp/yt/logging/logger.cpp @@ -220,23 +220,23 @@ void TLogger::Write(TLogEvent&& event) const LogManager_->Enqueue(std::move(event)); } -void TLogger::AddRawTag(const std::string& tag) +void TLogger::AddRawTag(TStringBuf tag) { auto* state = GetMutableCoWState(); if (!state->Tag.empty()) { state->Tag += ", "; } - state->Tag += tag; + state->Tag.append(tag.data(), tag.size()); } -TLogger TLogger::WithRawTag(const std::string& tag) const & +TLogger TLogger::WithRawTag(TStringBuf tag) const & { auto result = *this; result.AddRawTag(tag); return result; } -TLogger TLogger::WithRawTag(const std::string& tag) && +TLogger TLogger::WithRawTag(TStringBuf tag) && { AddRawTag(tag); return std::move(*this); diff --git a/library/cpp/yt/logging/logger.h b/library/cpp/yt/logging/logger.h index cab385bf676..d781326e88d 100644 --- a/library/cpp/yt/logging/logger.h +++ b/library/cpp/yt/logging/logger.h @@ -227,21 +227,21 @@ public: void Write(TLogEvent&& event) const; - void AddRawTag(const std::string& tag); + void AddRawTag(TStringBuf tag); template <class... TArgs> - void AddTag(const char* format, TArgs&&... args); + void AddTag(TFormatString<TArgs...> format, TArgs&&... args); template <class TType> void AddStructuredTag(TStringBuf key, TType value); void AddStructuredValidator(TStructuredValidator validator); - TLogger WithRawTag(const std::string& tag) const &; - TLogger WithRawTag(const std::string& tag) &&; + TLogger WithRawTag(TStringBuf tag) const &; + TLogger WithRawTag(TStringBuf tag) &&; template <class... TArgs> - TLogger WithTag(const char* format, TArgs&&... args) const &; + TLogger WithTag(TFormatString<TArgs...> format, TArgs&&... args) const &; template <class... TArgs> - TLogger WithTag(const char* format, TArgs&&... args) &&; + TLogger WithTag(TFormatString<TArgs...> format, TArgs&&... args) &&; template <class TType> TLogger WithStructuredTag(TStringBuf key, TType value) const &; diff --git a/library/cpp/yt/logging/ya.make b/library/cpp/yt/logging/ya.make index 8b338ed3b77..b9cf5c6e715 100644 --- a/library/cpp/yt/logging/ya.make +++ b/library/cpp/yt/logging/ya.make @@ -18,6 +18,7 @@ END() RECURSE( backends + benchmark plain_text_formatter ) |
