From 17afde9e8cbce589c85797c96cdb7c3ba0a4763c Mon Sep 17 00:00:00 2001 From: Matthias Kleiner Date: Mon, 25 Aug 2025 13:33:39 +0200 Subject: [PATCH] TPC: sort buffer of pressure in case it is not sorted - allocate enough memory to prevent reallocation --- DataFormats/Detectors/TPC/src/DCS.cxx | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/DataFormats/Detectors/TPC/src/DCS.cxx b/DataFormats/Detectors/TPC/src/DCS.cxx index 14c3887f8e8ae..b56d07acd7c73 100644 --- a/DataFormats/Detectors/TPC/src/DCS.cxx +++ b/DataFormats/Detectors/TPC/src/DCS.cxx @@ -329,12 +329,27 @@ void fillBuffer(std::pair, std::vector>& buffe } } - std::pair, std::vector> buffTmp{ - std::vector(buffer.first.begin() + idxStartBuffer, buffer.first.end()), - std::vector(buffer.second.begin() + idxStartBuffer, buffer.second.end())}; - - buffTmp.first.insert(buffTmp.first.end(), values.first.begin(), values.first.end()); - buffTmp.second.insert(buffTmp.second.end(), values.second.begin(), values.second.end()); + std::pair, std::vector> buffTmp; + auto& [buffVals, buffTimes] = buffTmp; + + // Preallocate enough capacity to avoid reallocations + buffVals.reserve(buffer.first.size() - idxStartBuffer + values.first.size()); + buffTimes.reserve(buffer.second.size() - idxStartBuffer + values.second.size()); + // Insert the kept part of the old buffer + buffVals.insert(buffVals.end(), buffer.first.begin() + idxStartBuffer, buffer.first.end()); + buffTimes.insert(buffTimes.end(), buffer.second.begin() + idxStartBuffer, buffer.second.end()); + // Insert the new values + buffVals.insert(buffVals.end(), values.first.begin(), values.first.end()); + buffTimes.insert(buffTimes.end(), values.second.begin(), values.second.end()); + + // this should not happen + if (!std::is_sorted(buffTimes.begin(), buffTimes.end())) { + LOGP(info, "Pressure buffer not sorted after filling - sorting it"); + std::vector idx(buffTimes.size()); + o2::math_utils::SortData(buffTimes, idx); + o2::math_utils::Reorder(buffVals, idx); + o2::math_utils::Reorder(buffTimes, idx); + } buffer = std::move(buffTmp); }