Performance analysis of using protobuff builders as general data object

假如想象 提交于 2019-12-11 18:06:33

问题


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*/
}  

回答1:


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.



来源:https://stackoverflow.com/questions/21586250/performance-analysis-of-using-protobuff-builders-as-general-data-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!