From 419471253a4283a1ce831f9d1dfd1d62c426e15f Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Tue, 9 Mar 2021 23:10:39 +0800 Subject: [PATCH] update_string: use cached buffer (#356) --- can/common.h | 1 + can/parser.cc | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/can/common.h b/can/common.h index 4daf816..e9cf591 100644 --- a/can/common.h +++ b/can/common.h @@ -43,6 +43,7 @@ public: class CANParser { private: const int bus; + kj::Array aligned_buf; const DBC *dbc = NULL; std::unordered_map message_states; diff --git a/can/parser.cc b/can/parser.cc index 44baee5..59d7e95 100644 --- a/can/parser.cc +++ b/can/parser.cc @@ -108,7 +108,7 @@ bool MessageState::update_counter_generic(int64_t v, int cnt_size) { CANParser::CANParser(int abus, const std::string& dbc_name, const std::vector &options, const std::vector &sigoptions) - : bus(abus) { + : bus(abus), aligned_buf(kj::heapArray(1024)) { dbc = dbc_lookup(dbc_name); assert(dbc); @@ -209,12 +209,15 @@ void CANParser::UpdateValid(uint64_t sec) { } void CANParser::update_string(const std::string &data, bool sendcan) { - // format for board, make copy due to alignment issues, will be freed on out of scope - auto amsg = kj::heapArray((data.length() / sizeof(capnp::word)) + 1); - memcpy(amsg.begin(), data.data(), data.length()); + // format for board, make copy due to alignment issues. + const size_t buf_size = (data.length() / sizeof(capnp::word)) + 1; + if (aligned_buf.size() < buf_size) { + aligned_buf = kj::heapArray(buf_size); + } + memcpy(aligned_buf.begin(), data.data(), data.length()); // extract the messages - capnp::FlatArrayMessageReader cmsg(amsg); + capnp::FlatArrayMessageReader cmsg(aligned_buf.slice(0, buf_size)); cereal::Event::Reader event = cmsg.getRoot(); last_sec = event.getLogMonoTime();