From f0e0ef0bf3e769b609de3fd1d79a5b615b8bc881 Mon Sep 17 00:00:00 2001 From: Siddhartha Malladi Date: Thu, 27 Jun 2024 18:14:31 -0400 Subject: [PATCH] [SDK] Added reserve for spans array in BatchSpanProcessor. (#2724) * Added reserve for spans array in BatchSpanProcessor. Added reserve for spans array in BatchSpanProcessor. Helps to allocate the amount of memory needed for number of records so that dynamic memory allocation doesn't happen in the consume method. .push_back() reallocates memory each time the method is called. Using .reserve() would avoid memory reallocation as already the memory is allocated. References: https://cplusplus.com/reference/vector/vector/push_back/ https://cplusplus.com/reference/vector/vector/reserve/ * Added reserve for spans array in BatchLogProcessor. Added reserve for spans array in BatchLogProcessor. Same as previously done for BatchSpanProcessor * Update batch_log_record_processor.cc * Update batch_span_processor.cc * Update sdk/src/logs/batch_log_record_processor.cc --- sdk/src/logs/batch_log_record_processor.cc | 2 ++ sdk/src/trace/batch_span_processor.cc | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/sdk/src/logs/batch_log_record_processor.cc b/sdk/src/logs/batch_log_record_processor.cc index c85d56739e..f5c35ad339 100644 --- a/sdk/src/logs/batch_log_record_processor.cc +++ b/sdk/src/logs/batch_log_record_processor.cc @@ -208,6 +208,8 @@ void BatchLogRecordProcessor::Export() break; } + // Reserve space for the number of records + records_arr.reserve(num_records_to_export); buffer_.Consume(num_records_to_export, [&](CircularBufferRange> range) noexcept { range.ForEach([&](AtomicUniquePtr &ptr) { diff --git a/sdk/src/trace/batch_span_processor.cc b/sdk/src/trace/batch_span_processor.cc index c5e72f8176..03b79f6ebb 100644 --- a/sdk/src/trace/batch_span_processor.cc +++ b/sdk/src/trace/batch_span_processor.cc @@ -206,6 +206,10 @@ void BatchSpanProcessor::Export() NotifyCompletion(notify_force_flush, exporter_, synchronization_data_); break; } + + // Reserve space for the number of records + spans_arr.reserve(num_records_to_export); + buffer_.Consume(num_records_to_export, [&](CircularBufferRange> range) noexcept { range.ForEach([&](AtomicUniquePtr &ptr) {