On Tue, Sep 29, 2009 at 12:22 PM, alopecoid <[email protected]> wrote:
> > Hi, > > Can serialized messages be used reliably as keys? > > In other words, is it guaranteed that... > > - Two equal messages will always generate equal byte sequences? > (Are fields always written in the same order?) > Only if: 1) The implementation you are using writes fields in canonical order. All official implementations do this, and probably most unofficial ones. However, implementations are technically allowed to write fields in any order. 2) There are no unknown fields in the message. Your message may have unknown fields if you originally parsed it off the wire and the sender is a newer binary that knows about new fields recently added to the .proto file. In C++ you can get rid of all unknown fields in a message by calling the DiscardUnknownFields() method. If possible, I would recommend designing your application such that it only requires that equal messages have the same serialization *most* of the time. For example, if you were designing a cache where the cache key is the hash of a serialized message, then the worst that can happen if two equal messages had different serializations is that you'd perform the same operation twice rather than hitting cache. As long as this is relatively rare, it's no big deal. > - Two unequal messages will always generate unequal byte sequences? > As Jon said, this clearly has to be true. If two messages could have the same serialization, then how would the parser know which one to produce when parsing? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to [email protected] 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 -~----------~----~----~----~------~----~------~--~---
