From 9d1f9c6e26f420adc5afeef10ab5f7976f129e15 Mon Sep 17 00:00:00 2001 From: Vlad Kuznecov Date: Fri, 12 Jan 2024 15:51:12 +0000 Subject: [PATCH 1/2] use iostream instead of printf --- ydb/core/base/logoblob.cpp | 35 ++++++++++++----------------------- ydb/core/base/traceid.cpp | 15 ++++++++------- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/ydb/core/base/logoblob.cpp b/ydb/core/base/logoblob.cpp index d5deb4b75af5..00cc971a3963 100644 --- a/ydb/core/base/logoblob.cpp +++ b/ydb/core/base/logoblob.cpp @@ -1,35 +1,24 @@ #include "logoblob.h" #include -#include namespace NKikimr { TString TLogoBlobID::ToString() const { - return Sprintf( - "[%" PRIu64 ":%" PRIu32 ":%" PRIu32 ":%" PRIu32 ":%" PRIu32 ":%" PRIu32 ":%" PRIu32 "]", - TabletID(), - Generation(), - Step(), - Channel(), - Cookie(), - BlobSize(), - PartId()).data(); + TStringStream ss; + Out(ss); + return ss.Str(); } void TLogoBlobID::Out(IOutputStream &o) const { - char buf[240]; - snprintf(buf, sizeof(buf), - "[%" PRIu64 ":%" PRIu32 ":%" PRIu32 ":%" PRIu32 ":%" PRIu32 ":%" PRIu32 ":%" PRIu32 "]", - TabletID(), - Generation(), - Step(), - Channel(), - Cookie(), - BlobSize(), - PartId() - ); - - o << buf; + o << "[" + << TabletID() << ":" + << Generation() << ":" + << Step() << ":" + << Channel() << ":" + << Cookie() << ":" + << BlobSize() << ":" + << PartId() + << "]" ; } void TLogoBlobID::Out(IOutputStream &o, const TVector &vec) { diff --git a/ydb/core/base/traceid.cpp b/ydb/core/base/traceid.cpp index 23311ea0d1cc..ef4856784224 100644 --- a/ydb/core/base/traceid.cpp +++ b/ydb/core/base/traceid.cpp @@ -20,16 +20,17 @@ TTraceID TTraceID::GenerateNew() { } TString TTraceID::ToString() const { - TString result; - TStringOutput out(result); - Out(out); - return result; + TStringStream ss; + Out(ss); + return ss.Str(); } void TTraceID::Out(IOutputStream &o) const { - char buf[240]; - snprintf(buf, sizeof(buf), "[ID:%" PRIu64 ", Created: %s]", RandomID, TInstant::MicroSeconds(CreationTime).ToRfc822StringLocal().data()); - o << buf; + o << "[" + << "ID: " << RandomID << ", " + << "Created: " << TInstant::MicroSeconds(CreationTime).ToRfc822StringLocal() + << "]"; + } bool TTraceID::operator<(const TTraceID &x) const { From b3ea45d70cc1bb53389ddd4b896a9ad4cbef4b45 Mon Sep 17 00:00:00 2001 From: Vlad Kuznecov Date: Mon, 15 Jan 2024 15:07:16 +0000 Subject: [PATCH 2/2] fixes from PR, add reserve to strings --- ydb/core/base/logoblob.cpp | 24 +++++++++++++----------- ydb/core/base/traceid.cpp | 14 ++++++-------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/ydb/core/base/logoblob.cpp b/ydb/core/base/logoblob.cpp index 00cc971a3963..385c0a3aa14d 100644 --- a/ydb/core/base/logoblob.cpp +++ b/ydb/core/base/logoblob.cpp @@ -4,21 +4,23 @@ namespace NKikimr { TString TLogoBlobID::ToString() const { - TStringStream ss; - Out(ss); - return ss.Str(); + TString str; + str.reserve(64); + TStringOutput outStr(str); + Out(outStr); + return str; } void TLogoBlobID::Out(IOutputStream &o) const { - o << "[" - << TabletID() << ":" - << Generation() << ":" - << Step() << ":" - << Channel() << ":" - << Cookie() << ":" - << BlobSize() << ":" + o << '[' + << TabletID() << ':' + << Generation() << ':' + << Step() << ':' + << Channel() << ':' + << Cookie() << ':' + << BlobSize() << ':' << PartId() - << "]" ; + << ']' ; } void TLogoBlobID::Out(IOutputStream &o, const TVector &vec) { diff --git a/ydb/core/base/traceid.cpp b/ydb/core/base/traceid.cpp index ef4856784224..af3cace92f25 100644 --- a/ydb/core/base/traceid.cpp +++ b/ydb/core/base/traceid.cpp @@ -20,17 +20,15 @@ TTraceID TTraceID::GenerateNew() { } TString TTraceID::ToString() const { - TStringStream ss; - Out(ss); - return ss.Str(); + TString str; + str.reserve(128); + TStringOutput outStr(str); + Out(outStr); + return str; } void TTraceID::Out(IOutputStream &o) const { - o << "[" - << "ID: " << RandomID << ", " - << "Created: " << TInstant::MicroSeconds(CreationTime).ToRfc822StringLocal() - << "]"; - + o << "[ID: " << RandomID << ", " << "Created: " << TInstant::MicroSeconds(CreationTime).ToRfc822StringLocal() << "]"; } bool TTraceID::operator<(const TTraceID &x) const {