How to get the serialized length of a message?

30 views
Skip to first unread message

vlo...@gmail.com

unread,
May 6, 2020, 3:54:05 PM5/6/20
to Cap'n Proto
Is there a convenient way to peek at the size a message will take up on the wire? My thinking is to create a custom output stream that I `capnp::writeMessage` to that just sums everything but I feel like this could be done more efficiently within capn'n'proto, just couldn't find anything like that at first glance. Is there an obvious API missing? Would there be any interest in me putting up a diff with such an API?

Kenton Varda

unread,
May 6, 2020, 5:29:35 PM5/6/20
to Vitali Lovich, Cap'n Proto

On Wed, May 6, 2020 at 2:54 PM <vlo...@gmail.com> wrote:
Is there a convenient way to peek at the size a message will take up on the wire? My thinking is to create a custom output stream that I `capnp::writeMessage` to that just sums everything but I feel like this could be done more efficiently within capn'n'proto, just couldn't find anything like that at first glance. Is there an obvious API missing? Would there be any interest in me putting up a diff with such an API?

--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/d47d474c-f268-4053-99ca-b2213a2e58d0%40googlegroups.com.

Vitali Lovich

unread,
May 6, 2020, 5:49:06 PM5/6/20
to Kenton Varda, Cap'n Proto
The optometrist tells me I have 20/20 vision but I think it must clearly be a lie at this point :). Any objection to a contribution to return the size in bytes?

Kenton Varda

unread,
May 6, 2020, 6:07:47 PM5/6/20
to Vitali Lovich, Cap'n Proto
Eh, I like having the APIs be in words in hopes that it reminds people to think about alignment. When allocating space for a capnp message, you should always allocate an array of words, not an array of bytes.

Seems not too hard for the caller to multiply by sizeof(capnp::word) if needed?

-Kenton

Vitali Lovich

unread,
May 6, 2020, 6:09:13 PM5/6/20
to Kenton Varda, Cap'n Proto
That’s fair. I’m having some corruption issues so I’m trying to layer a framing message around cap’n’proto that has the length of the payload (+ checksums) which is where the length in bytes comes in useful.
Reply all
Reply to author
Forward
0 new messages