Question

Since protobuffers are a wonderful alternative for java serialisation, we have used it extensively. Also, we have used java builders as general data object. On examining the speeds of constructing an object using message builder ,forming the instance parameter, and normal java primitives forming the object, we found that for an object containing 6 primitive fields, constructing the object using builder(which is the parameter of the object) took 1.1ms whereas using java primitives took only 0.3ms! And for a list of 50 of such fields! Are builders heavy, that using them as general data object affects the speed of construction to this extent?

Below is the sample design i used for the analysis,

message PersonList
{
   repeated Person = 1; 

    message Person 
    {
         optional string name = 1;
         optional int32 age = 2;
         optional string place = 3;
         optional bool alive = 4;
         optional string profession = 5;
    }
}       

The java equivalent 

Class PersonList {

     List<Person> personList;

     Class Person {
         String name;
         int age;
         String place;
         boolean alive;
         String profession;
     }
    /* getters and setters*/
}  
Was it helpful?

Solution

I have a hard time imagining anything that contains only "6 primitive values" could take 7ms to construct. That's perhaps 100,000 times as long as it should take. So I'm not sure I understand what you're doing.

That said, protobuf builders are indeed more complicated than a typical POJO for a number of reasons. For example, protobuf objects keep track of which fields are currently set. Also, repeated primitives are boxed, which makes them pretty inefficient compared to a Java primitive array. So if you measure construction time alone you may see a significant difference. However, these effects are typically irrelevant compared to the time spent in the rest of your app's code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top