Skip to content

Commit

Permalink
update_string: use cached buffer (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee authored Mar 9, 2021
1 parent b1fe8b1 commit 4194712
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions can/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class MessageState {
class CANParser {
private:
const int bus;
kj::Array<capnp::word> aligned_buf;

const DBC *dbc = NULL;
std::unordered_map<uint32_t, MessageState> message_states;
Expand Down
13 changes: 8 additions & 5 deletions can/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<MessageParseOptions> &options,
const std::vector<SignalParseOptions> &sigoptions)
: bus(abus) {
: bus(abus), aligned_buf(kj::heapArray<capnp::word>(1024)) {

dbc = dbc_lookup(dbc_name);
assert(dbc);
Expand Down Expand Up @@ -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<capnp::word>((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<capnp::word>(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<cereal::Event>();

last_sec = event.getLogMonoTime();
Expand Down

0 comments on commit 4194712

Please sign in to comment.