From 951793fd9f218151695f8f5a58d292c045df4541 Mon Sep 17 00:00:00 2001 From: deanlee Date: Fri, 29 Mar 2024 15:47:48 +0800 Subject: [PATCH] reserve --- can/common.h | 1 + can/parser.cc | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/can/common.h b/can/common.h index 16b2e808ed..b462528441 100644 --- a/can/common.h +++ b/can/common.h @@ -67,6 +67,7 @@ class CANParser { const int bus; const DBC *dbc = NULL; std::unordered_map message_states; + int32_t signal_count = -1; public: bool can_valid = false; diff --git a/can/parser.cc b/can/parser.cc index 832948d1c7..d70cc1bc03 100644 --- a/can/parser.cc +++ b/can/parser.cc @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -257,6 +258,13 @@ void CANParser::query_latest(std::vector &vals, uint64_t last_ts) { if (last_ts == 0) { last_ts = last_nanos; } + + if (signal_count == -1) { + signal_count = std::accumulate(message_states.cbegin(), message_states.cend(), 0, [](int n, auto &it) { + return n + it.second.parse_sigs.size(); + }); + } + vals.reserve(signal_count); for (auto& kv : message_states) { auto& state = kv.second; if (last_ts != 0 && state.last_seen_nanos < last_ts) {