Skip to content

correct use CapnProto #1974

Answered by kentonv
angelskieglazki asked this question in Q&A
Mar 21, 2024 · 1 comments · 1 reply
Discussion options

You must be logged in to vote
  kj::Array<capnp::word> words = messageToFlatArray(message);
  kj::ArrayPtr<kj::byte> bytes = words.asBytes();
  dest.appendBytes((uint32_t)bytes.size());
  std::vector<uint8_t> v(bytes.begin(), bytes.end());
  dest.appendBytes(v);

This looks like it's making at least three copies of the message content: messageToFlatArray makes one, constructing a vector is a second, and appendBytes() is a third. This could be more efficient if you implement a subclass of kj::OutputStream which writes directly to your Blob, then use capnp::writeMessage(message, stream).

Similarly on the read side you are reading into a std::vector first, and then copying that into a kj::Array. The best would be if you …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@angelskieglazki
Comment options

Answer selected by angelskieglazki
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants