encoder: reduce memory allocations and copying (#28704)

* reduce memory allocations and copying

* #include <vector>
old-commit-hash: a4179a7c23
chrysler-long2
Dean Lee 1 year ago committed by GitHub
parent ff8521a32e
commit 5ab1fc7674
  1. 11
      system/loggerd/encoder/encoder.cc
  2. 3
      system/loggerd/encoder/encoder.h

@ -27,8 +27,11 @@ void VideoEncoder::publisher_publish(VideoEncoder *e, int segment_num, uint32_t
edat.setData(dat); edat.setData(dat);
if (flags & V4L2_BUF_FLAG_KEYFRAME) edat.setHeader(header); if (flags & V4L2_BUF_FLAG_KEYFRAME) edat.setHeader(header);
auto words = new kj::Array<capnp::word>(capnp::messageToFlatArray(msg)); uint32_t bytes_size = capnp::computeSerializedSizeInWords(msg) * sizeof(capnp::word);
auto bytes = words->asBytes(); if (e->msg_cache.size() < bytes_size) {
e->pm->send(e->encoder_info.publish_name, bytes.begin(), bytes.size()); e->msg_cache.resize(bytes_size);
delete words; }
kj::ArrayOutputStream output_stream(kj::ArrayPtr<capnp::byte>(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);
} }

@ -4,6 +4,7 @@
#include <cstdint> #include <cstdint>
#include <memory> #include <memory>
#include <thread> #include <thread>
#include <vector>
#include "cereal/messaging/messaging.h" #include "cereal/messaging/messaging.h"
#include "cereal/visionipc/visionipc.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<capnp::byte> header, kj::ArrayPtr<capnp::byte> dat); static void publisher_publish(VideoEncoder *e, int segment_num, uint32_t idx, VisionIpcBufExtra &extra, unsigned int flags, kj::ArrayPtr<capnp::byte> header, kj::ArrayPtr<capnp::byte> dat);
protected: protected:
int in_width, in_height; int in_width, in_height;
const EncoderInfo encoder_info; const EncoderInfo encoder_info;
@ -32,4 +32,5 @@ private:
// total frames encoded // total frames encoded
int cnt = 0; int cnt = 0;
std::unique_ptr<PubMaster> pm; std::unique_ptr<PubMaster> pm;
std::vector<capnp::byte> msg_cache;
}; };

Loading…
Cancel
Save