From 5ab1fc7674480fba1f51652d9cc67d0b7ffa5ff0 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Wed, 13 Dec 2023 11:15:47 +0800 Subject: [PATCH] encoder: reduce memory allocations and copying (#28704) * reduce memory allocations and copying * #include old-commit-hash: a4179a7c230e687bebd15581ccdb3f7d34a690f4 --- system/loggerd/encoder/encoder.cc | 11 +++++++---- system/loggerd/encoder/encoder.h | 3 ++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/system/loggerd/encoder/encoder.cc b/system/loggerd/encoder/encoder.cc index 869b4617b3..0aba4b8b49 100644 --- a/system/loggerd/encoder/encoder.cc +++ b/system/loggerd/encoder/encoder.cc @@ -27,8 +27,11 @@ void VideoEncoder::publisher_publish(VideoEncoder *e, int segment_num, uint32_t edat.setData(dat); if (flags & V4L2_BUF_FLAG_KEYFRAME) edat.setHeader(header); - auto words = new kj::Array(capnp::messageToFlatArray(msg)); - auto bytes = words->asBytes(); - e->pm->send(e->encoder_info.publish_name, bytes.begin(), bytes.size()); - delete words; + uint32_t bytes_size = capnp::computeSerializedSizeInWords(msg) * sizeof(capnp::word); + if (e->msg_cache.size() < bytes_size) { + e->msg_cache.resize(bytes_size); + } + kj::ArrayOutputStream output_stream(kj::ArrayPtr(e->msg_cache.data(), bytes_size)); + capnp::writeMessage(output_stream, msg); + e->pm->send(e->encoder_info.publish_name, e->msg_cache.data(), bytes_size); } diff --git a/system/loggerd/encoder/encoder.h b/system/loggerd/encoder/encoder.h index a8bfd5c054..9c23928cc6 100644 --- a/system/loggerd/encoder/encoder.h +++ b/system/loggerd/encoder/encoder.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "cereal/messaging/messaging.h" #include "cereal/visionipc/visionipc.h" @@ -23,7 +24,6 @@ public: static void publisher_publish(VideoEncoder *e, int segment_num, uint32_t idx, VisionIpcBufExtra &extra, unsigned int flags, kj::ArrayPtr header, kj::ArrayPtr dat); - protected: int in_width, in_height; const EncoderInfo encoder_info; @@ -32,4 +32,5 @@ private: // total frames encoded int cnt = 0; std::unique_ptr pm; + std::vector msg_cache; };