Protocol buffer Newbuilder Slow speed problem

Source: Internet
Author: User
The recent project has been using protocol buffer as the protocol layer, and in the process of using it, it is found that each time a message is created builder--that is newbuilder, it is very slow (often requires more than 20ms), So that everyone is now basically talking about the newbuilder color change. Today bright finally can't help, ask me why a builder's creation so slow, I am playing games, take time to pull out the source to see a bit, found inside is really new a builder object, this builder object is not so complex, and the construction method is simple enough, no logic , yes, I can't answer the bright question suddenly. Can only say: You wait, I play the game to study a bit.
Think about it, if there is no logic in the constructor of a class, take a look at its instance fields, superclass fields, and how to construct them, and find a builder's inheritance structure like this:
It's scary to watch, but to be honest, the fields in each class and how they're constructed don't make it more than 20ms, so what's the problem?
If the creation is unpleasant, then it must be on the first class load. To verify this speculation, I put the newbuilder call into a loop, looking at the time of each loop, and the result is the same as the conjecture, except for the first scare, which is basically 0 at the millisecond level.
With this validation, we continue to demonstrate that the builder's load is placed outside the entire test, and then tested in a class.forname way, which is more clear, the first Newbuilder only used about 5ms, the others are 0ms levels.
Looking closely at the definition of builder and its various parent classes, all of which are static class, that is, the JVM does not actually load it until we actually use it, which is why it is very slow for the first time.

Protocol buffer Newbuilder Slow speed problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.