mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-30 02:31:44 +00:00
[wpiutil] Faster nanopb submessage encode (#7374)
Due to how submessages are encoded (with a length prefix), nanopb currently does the encoding twice. It encodes once to get the length to write, then writes the length, then reencodes the entire message a 2nd time. This results in a requirement that each encode always encodes the same. Generally, this is fine, but it'd be nice to not make this a requirement. The double encode also requires going through the entire set of fields again, which has the possibility to be slow. Instead of doing this, write to a temporary SmallVector. Then we can just encode the length of that buffer, and do a memcpy into primary stream. Theoretically in most cases, this should be much faster.
This commit is contained in:
@@ -43,6 +43,9 @@ bool WriteFromSmallVector(pb_ostream_t* stream, const pb_byte_t* buf,
|
||||
|
||||
bool WriteFromStdVector(pb_ostream_t* stream, const pb_byte_t* buf,
|
||||
size_t count);
|
||||
|
||||
bool WriteSubmessage(pb_ostream_t* stream, const pb_msgdesc_t* desc,
|
||||
const void* msg);
|
||||
} // namespace detail
|
||||
|
||||
/**
|
||||
@@ -208,7 +211,8 @@ class ProtoOutputStream {
|
||||
bool Encode(
|
||||
const typename Protobuf<std::remove_cvref_t<T>>::MessageStruct& msg) {
|
||||
if (m_streamMsg) {
|
||||
return pb_encode_submessage(m_streamMsg, m_msgDesc, &msg);
|
||||
return detail::WriteSubmessage(m_streamMsg, m_msgDesc, &msg);
|
||||
// return pb_encode_submessage(m_streamMsg, m_msgDesc, &msg);
|
||||
}
|
||||
return pb_encode(&m_streamLocal, m_msgDesc, &msg);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user