From 2f1ab6392057b33d7375a243aa37f46f829e9c55 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Fri, 8 Oct 2021 16:16:23 -0700 Subject: [PATCH] boardd: reduce unnecessary allocations (#22494) --- selfdrive/boardd/panda.cc | 8 +++++--- selfdrive/boardd/panda.h | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/selfdrive/boardd/panda.cc b/selfdrive/boardd/panda.cc index 9387808d7..c123d0c80 100644 --- a/selfdrive/boardd/panda.cc +++ b/selfdrive/boardd/panda.cc @@ -344,10 +344,12 @@ void Panda::send_heartbeat() { } void Panda::can_send(capnp::List::Reader can_data_list) { - static std::vector send; const int msg_count = can_data_list.size(); + const int buf_size = msg_count*0x10; - send.resize(msg_count*0x10); + if (send.size() < buf_size) { + send.resize(buf_size); + } for (int i = 0; i < msg_count; i++) { auto cmsg = can_data_list[i]; @@ -362,7 +364,7 @@ void Panda::can_send(capnp::List::Reader can_data_list) { memcpy(&send[i*4+2], can_data.begin(), can_data.size()); } - usb_bulk_write(3, (unsigned char*)send.data(), send.size(), 5); + usb_bulk_write(3, (unsigned char*)send.data(), buf_size, 5); } int Panda::can_receive(kj::Array& out_buf) { diff --git a/selfdrive/boardd/panda.h b/selfdrive/boardd/panda.h index 971ec8b42..36fd699be 100644 --- a/selfdrive/boardd/panda.h +++ b/selfdrive/boardd/panda.h @@ -45,6 +45,7 @@ class Panda { libusb_context *ctx = NULL; libusb_device_handle *dev_handle = NULL; std::mutex usb_lock; + std::vector send; void handle_usb_issue(int err, const char func[]); void cleanup();