Skip to content

Commit da7d577

Browse files
committed
preallocate vectors
1 parent 832645e commit da7d577

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

selfdrive/boardd/boardd.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -295,14 +295,19 @@ void send_empty_panda_state(PubMaster *pm) {
295295

296296
std::optional<bool> send_panda_states(PubMaster *pm, const std::vector<Panda *> &pandas, bool spoofing_started) {
297297
bool ignition_local = false;
298+
const uint32_t pandas_cnt = pandas.size();
298299

299300
// build msg
300301
MessageBuilder msg;
301302
auto evt = msg.initEvent();
302-
auto pss = evt.initPandaStates(pandas.size());
303+
auto pss = evt.initPandaStates(pandas_cnt);
303304

304305
std::vector<health_t> pandaStates;
306+
pandaStates.reserve(pandas_cnt);
307+
305308
std::vector<std::array<can_health_t, PANDA_CAN_CNT>> pandaCanStates;
309+
pandaCanStates.reserve(pandas_cnt);
310+
306311
for (const auto& panda : pandas){
307312
auto health_opt = panda->get_state();
308313
if (!health_opt) {
@@ -330,7 +335,7 @@ std::optional<bool> send_panda_states(PubMaster *pm, const std::vector<Panda *>
330335
pandaStates.push_back(health);
331336
}
332337

333-
for (uint32_t i = 0; i < pandas.size(); i++) {
338+
for (uint32_t i = 0; i < pandas_cnt; i++) {
334339
auto panda = pandas[i];
335340
const auto &health = pandaStates[i];
336341

0 commit comments

Comments
 (0)