From a3f452d80b121363077b33f6bff12d859bb2885e Mon Sep 17 00:00:00 2001 From: henryzhx8 Date: Thu, 31 Oct 2024 02:34:24 +0000 Subject: [PATCH] polish --- core/pipeline/serializer/SLSSerializer.cpp | 47 ++++++++++--------- core/protobuf/sls/LogGroupSerializer.cpp | 2 +- core/protobuf/sls/LogGroupSerializer.h | 2 +- .../log_pb/LogGroupSerializerUnittest.cpp | 2 +- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/core/pipeline/serializer/SLSSerializer.cpp b/core/pipeline/serializer/SLSSerializer.cpp index b3685103f1..ebcedb710f 100644 --- a/core/pipeline/serializer/SLSSerializer.cpp +++ b/core/pipeline/serializer/SLSSerializer.cpp @@ -66,9 +66,10 @@ bool SLSEventGroupSerializer::Serialize(BatchedEvents&& group, string& res, stri bool enableNs = mFlusher->GetContext().GetGlobalConfig().mEnableTimestampNanosecond; - size_t logGroupSZ = 0; + // caculate serialized logGroup size first, where some critical results can be cached vector logSZ(group.mEvents.size()); - vector> metricEventContentSZ(group.mEvents.size()); + vector> metricEventContentCache(group.mEvents.size()); + size_t logGroupSZ = 0; switch (eventType) { case PipelineEvent::Type::LOG: for (size_t i = 0; i < group.mEvents.size(); ++i) { @@ -86,16 +87,16 @@ bool SLSEventGroupSerializer::Serialize(BatchedEvents&& group, string& res, stri if (e.Is()) { continue; } else if (e.Is()) { - metricEventContentSZ[i].first = to_string(e.GetValue()->mValue); + metricEventContentCache[i].first = to_string(e.GetValue()->mValue); } - metricEventContentSZ[i].second = GetMetricLabelSize(e); + metricEventContentCache[i].second = GetMetricLabelSize(e); size_t contentSZ = 0; contentSZ += GetLogContentSize(METRIC_RESERVED_KEY_NAME.size(), e.GetName().size()); - contentSZ += GetLogContentSize(METRIC_RESERVED_KEY_VALUE.size(), metricEventContentSZ[i].first.size()); + contentSZ += GetLogContentSize(METRIC_RESERVED_KEY_VALUE.size(), metricEventContentCache[i].first.size()); contentSZ += GetLogContentSize(METRIC_RESERVED_KEY_TIME_NANO.size(), e.GetTimestampNanosecond() ? 19U : 10U); - contentSZ += GetLogContentSize(METRIC_RESERVED_KEY_LABELS.size(), metricEventContentSZ[i].second); + contentSZ += GetLogContentSize(METRIC_RESERVED_KEY_LABELS.size(), metricEventContentCache[i].second); logGroupSZ += GetLogSize(contentSZ, false, logSZ[i]); } break; @@ -120,30 +121,30 @@ bool SLSEventGroupSerializer::Serialize(BatchedEvents&& group, string& res, stri return false; } - static LogGroupSerializer logGroup; - logGroup.PrepareToSerialize(logGroupSZ); + static LogGroupSerializer serializer; + serializer.Prepare(logGroupSZ); for (size_t i = 0; i < group.mEvents.size(); ++i) { if (group.mEvents[i].Is()) { const auto& logEvent = group.mEvents[i].Cast(); - logGroup.StartToAddLog(logSZ[i]); - logGroup.AddLogTime(logEvent.GetTimestamp()); + serializer.StartToAddLog(logSZ[i]); + serializer.AddLogTime(logEvent.GetTimestamp()); for (const auto& kv : logEvent) { - logGroup.AddLogContent(kv.first, kv.second); + serializer.AddLogContent(kv.first, kv.second); } if (enableNs && logEvent.GetTimestampNanosecond()) { - logGroup.AddLogTimeNs(logEvent.GetTimestampNanosecond().value()); + serializer.AddLogTimeNs(logEvent.GetTimestampNanosecond().value()); } } else if (group.mEvents[i].Is()) { const auto& metricEvent = group.mEvents[i].Cast(); if (metricEvent.Is()) { continue; } - logGroup.StartToAddLog(logSZ[i]); - logGroup.AddLogTime(metricEvent.GetTimestamp()); - logGroup.AddLogContentMetricLabel(metricEvent, metricEventContentSZ[i].second); - logGroup.AddLogContentMetricTimeNano(metricEvent); - logGroup.AddLogContent(METRIC_RESERVED_KEY_VALUE, metricEventContentSZ[i].first); - logGroup.AddLogContent(METRIC_RESERVED_KEY_NAME, metricEvent.GetName()); + serializer.StartToAddLog(logSZ[i]); + serializer.AddLogTime(metricEvent.GetTimestamp()); + serializer.AddLogContentMetricLabel(metricEvent, metricEventContentCache[i].second); + serializer.AddLogContentMetricTimeNano(metricEvent); + serializer.AddLogContent(METRIC_RESERVED_KEY_VALUE, metricEventContentCache[i].first); + serializer.AddLogContent(METRIC_RESERVED_KEY_NAME, metricEvent.GetName()); } else { errorMsg = "unsupported event type in event group"; return false; @@ -151,16 +152,16 @@ bool SLSEventGroupSerializer::Serialize(BatchedEvents&& group, string& res, stri } for (const auto& tag : group.mTags.mInner) { if (tag.first == LOG_RESERVED_KEY_TOPIC) { - logGroup.AddTopic(tag.second); + serializer.AddTopic(tag.second); } else if (tag.first == LOG_RESERVED_KEY_SOURCE) { - logGroup.AddSource(tag.second); + serializer.AddSource(tag.second); } else if (tag.first == LOG_RESERVED_KEY_MACHINE_UUID) { - logGroup.AddMachineUUID(tag.second); + serializer.AddMachineUUID(tag.second); } else { - logGroup.AddLogTag(tag.first, tag.second); + serializer.AddLogTag(tag.first, tag.second); } } - res = std::move(logGroup.GetResult()); + res = std::move(serializer.GetResult()); return true; } diff --git a/core/protobuf/sls/LogGroupSerializer.cpp b/core/protobuf/sls/LogGroupSerializer.cpp index 2bcfe566d3..41c4431133 100644 --- a/core/protobuf/sls/LogGroupSerializer.cpp +++ b/core/protobuf/sls/LogGroupSerializer.cpp @@ -97,7 +97,7 @@ static inline void fixed32_pack(uint32_t value, string& output) { } } -void LogGroupSerializer::PrepareToSerialize(size_t size) { +void LogGroupSerializer::Prepare(size_t size) { mRes.clear(); mRes.reserve(size); } diff --git a/core/protobuf/sls/LogGroupSerializer.h b/core/protobuf/sls/LogGroupSerializer.h index 76bbcc1ebb..98dbe42f67 100644 --- a/core/protobuf/sls/LogGroupSerializer.h +++ b/core/protobuf/sls/LogGroupSerializer.h @@ -34,7 +34,7 @@ extern const std::string METRIC_LABELS_KEY_VALUE_SEPARATOR; class LogGroupSerializer { public: - void PrepareToSerialize(size_t size); + void Prepare(size_t size); void StartToAddLog(size_t size); void AddLogTime(uint32_t logTime); void AddLogContent(StringView key, StringView value); diff --git a/core/unittest/log_pb/LogGroupSerializerUnittest.cpp b/core/unittest/log_pb/LogGroupSerializerUnittest.cpp index f1cc094a68..17c37817cf 100644 --- a/core/unittest/log_pb/LogGroupSerializerUnittest.cpp +++ b/core/unittest/log_pb/LogGroupSerializerUnittest.cpp @@ -47,7 +47,7 @@ void LogGroupSerializerUnittest::TestSerialize() { groupSZ += GetLogTagSize(strlen("key_6"), strlen("value_6")); LogGroupSerializer logGroup; - logGroup.PrepareToSerialize(groupSZ); + logGroup.Prepare(groupSZ); logGroup.StartToAddLog(logSZ[0]); logGroup.AddLogTime(1234567890); logGroup.AddLogContent("key_1", "value_1");