The order of elements in a protobuf map field

Viewed 2700

I have many map fields defined in my protocol buffer messages. The messages are populated in C++ and received in a different C++ component, which reads their content using the Descriptor and Reflection APIs.

Given a map field, say:

map <int32, int32> my_map = 1;

This is transported in the same way as something like this:

message my_map_entry {
  int32 key = 1;
  int32 value = 2;
}
repeated my_map_entry my_map = 1;

As I have understood the current limitation of Descriptor and Reflection APIs, here I have to perform look ups by iterating over the received data. Of course I could put all the data in some more suitable data structure, such as a std::unordered_map if I wanted to do many look ups in the received map field, but I generally only do one look up per received map field.

Can I assume something about the order in which the data is received? Is the repeated my_map_entry messages perhaps ordered, because of the underlaying data structure used in the protocol buffer implementation? If so, a look up for an integer key in a map can stop when a larger key is found. That could give me a potential optimization when it comes to processing the received map fields in my application.

1 Answers

You can not assume the order of the map is similar after serialization.

The following quote is taken from the protobuf website:

Wire format ordering and map iteration ordering of map values is undefined, so you cannot rely on your map items being in a particular order

In general protobuf may serialize fields in a random order.

Related