Re: Does SerializeToXxx() guarantee a call to ByteSize()?

2008-09-19 Thread Kenton Varda
You probably shouldn't rely on this.  Some of the serialization methods will
guarantee this, some won't.  What you can do is call ByteSize() yourself,
and then use SerializeWithCachedSizes(), which requires that ByteSize() was
already called and won't call it again.  You'll have to do a small amount of
extra setup (allocating a ZeroCopyOutputStream and a CodedOutputStream), but
it's pretty straightforward -- you can just copy the code out of message.cc.
Another thing that you could do which is specific to your use case:  Instead
of allocating a byte array to serialize into, just use a string:

string bytes;
pkt.SerializeToString(bytes);
send(fd, bytes.data(), bytes.size(), 0);

This is better anyway since it won't break if your messages become larger
than SIZE.

On Fri, Sep 19, 2008 at 2:12 PM, Leandro Lucarella [EMAIL PROTECTED] wrote:


 Hi.

 I guess SerializeToXxx() methods internally calls to ByteSize(), so after
 a serialization, I can call GetCachedSize().

 My question is, first, is this true? =) And second, can I rely on this?

 The use case is this:

 pkt.SerializeToArray(buffer, SIZE);
 send(fd, buffer, pkt.GetCachedSize(), 0);

 Is this safe or should I use pkt.ByteSize() instead?


 Thank you.

 --
 Leandro Lucarella (luca) | Blog colectivo:
 http://www.mazziblog.com.ar/blog/

 
 GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)

 
 Soñé que tenia un caballo
 Que me trataba mejor que vos
 Tenia tan buena onda con ella
 Era mi yegua, mucho mas que vos


 


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To post to this group, send email to protobuf@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en
-~--~~~~--~~--~--~---



Re: Does SerializeToXxx() guarantee a call to ByteSize()?

2008-09-19 Thread Leandro Lucarella

Kenton Varda, el 19 de septiembre a las 16:12 me escribiste:
 You probably shouldn't rely on this.  Some of the serialization methods will
 guarantee this, some won't.  What you can do is call ByteSize() yourself,
 and then use SerializeWithCachedSizes(), which requires that ByteSize() was
 already called and won't call it again.  You'll have to do a small amount of
 extra setup (allocating a ZeroCopyOutputStream and a CodedOutputStream), but
 it's pretty straightforward -- you can just copy the code out of message.cc.
 Another thing that you could do which is specific to your use case:  Instead
 of allocating a byte array to serialize into, just use a string:
 
 string bytes;
 pkt.SerializeToString(bytes);
 send(fd, bytes.data(), bytes.size(), 0);

Thanks for the tip. I didn't do it this way because I use the same buffer
for receiving, and I can't use std::string for receiving.

I could use two buffers, but now I wonder how std::string is used when
serializing. Is the ByteSize() precalculated and space is reserve()ed in
the string or it's incrementally expanded as it's serialized?

 This is better anyway since it won't break if your messages become larger
 than SIZE.

This is not a problem to me, all messages are really short and I have
a physical size limit imposed by the lower level (I'm using TIPC RDM
sockets) anyway. So I have to drop the message (or abort the program) if
it's size is longer than this limit, and I have guaranteed that I can't
possibly receive a message that doesn't fit my buffer.

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/

GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)

HOMBRE DESNUDO AMENAZA A LOS VECINOS CON UNA KATANA DESDE SU BALCON
-- Crónica TV


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To post to this group, send email to protobuf@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en
-~--~~~~--~~--~--~---