Read selective fields without deserializing the entire object?

97 views
Skip to first unread message

Ashwin Jayaprakash

unread,
Oct 29, 2010, 7:13:42 PM10/29/10
to Protocol Buffers
Is there a way to read selective fields without deserializing the
entire object?

My serialized message is stored in memory as a (Heap)ByteBuffer.

I'm trying to find out if any of the (de-)serialization frameworks
allow a way to read specific fields without having to deserialize the
entire message. The message has almost 200 fields - mostly Strings and
a few long and int fields. Deserialization is expensive and creates
too much garbage considering the fact that I just need to read 1-2
fields.

Thanks in advance.

Henner Zeller

unread,
Oct 29, 2010, 8:19:02 PM10/29/10
to Ashwin Jayaprakash, Protocol Buffers

The fields are not 'indexed' in the beginning of the message so that
it is not simple to just jump to a particular field.

What you can do is to direclty use the CodedInputStream to parse
through the message, tag by tag and only read the tags you are
interested in. As soon as you have all fields together, you can stop
reading. So it is a bit manual work involved.
Also, if you want to be faster and skip bigger parts of the message,
make sure to create sub-messages within that message, maybe there is a
logical grouping of your fields ? You can skip a sub-message at once
if you don't need it instead of iterating through all fields in it,
which is faster.

H.

>
> Thanks in advance.
>
> --
> You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
> To post to this group, send email to prot...@googlegroups.com.
> To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
>
>

Ashwin Jayaprakash

unread,
Oct 29, 2010, 8:41:03 PM10/29/10
to Protocol Buffers
Cool, thanks!

ZHOU Xiaobo

unread,
Oct 31, 2010, 12:20:00 AM10/31/10
to Protocol Buffers
A further question:
Can ProtoBuf do in-place deserialization?

Say I have a packet including a COMMAND field and a big DATA field, and
I should only deserialize this packet to read COMMAND to decide how to process DATA.
The question is: does ProtoBuf copy DATA field when deserializing, and can I avoid that?

Kenton Varda

unread,
Nov 2, 2010, 10:55:21 PM11/2/10
to ZHOU Xiaobo, Protocol Buffers
2010/10/30 ZHOU Xiaobo <xb....@qq.com>
A further question:
Can ProtoBuf do in-place deserialization?

Not at present, no.
Reply all
Reply to author
Forward
0 new messages