Edward Zheng
Edward Zheng

Reputation: 23

The implementation of serializing a sub-message in protobuf

I'm learning the implementation of protobuf. However, there is one point that i just cannot understand. How can the framework serialize a sub-message(object) into a buffer with a variable head, even sometimes it's length is really hard to figure out! Serialize it to somewhere else and copy it into buffer later?

Upvotes: 2

Views: 586

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063964

There are at least four ways of doing that I can think of, off the top of my head - "which" is an implementation detail, as long as it gets the answer right:

  • serialize to a separate buffer, then copy
  • make an assumption about the length the header will take, skip that, serialize, then go back and fix-up the header (may involve a move, but not always)
  • if you use a builder, it could be serializing individuallly as you add data, then composing later: in which case figuring out the length is trivial
  • or it could have code to calculate the length of values without actually serializing

I've implemented it at least 3 of these ways, at different times!

Upvotes: 1

Related Questions