Home  >  Q&A  >  body text

JAVA代码如何优化?

黄舟黄舟2715 days ago342

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 13:11:25

    First of all, the way the code is written is relatively clear. Except that querying Json data should be encapsulated into the DAO layer, it does not have extreme requirements on performance, and it is not recommended to do too much optimization.
    But if it is really necessary, here are some optimization ideas:

    1. If your BlackList is large,
      1.1 You can estimate the number of retrieved values, and adjust the capacity and even the load factor when creating a new one to avoid opening up memory space multiple times;
      1.2 Or create it once and cache it directly, using a triggered update mechanism to avoid repeated creation;
      1.3 Replacing HashMap with TreeMap can slightly improve query performance. In fact, you can use Set in this scenario, which can slightly reduce memory overhead;
      1.4 You can write the ID of this List into redis or memcached, so that it does not occupy the memory space of this application.
    2. Json deserialization can use higher performance libraries, such as Jackson, fastjson
    3. If you can control the end where data is obtained, you can consider a higher-performance serialization method, such as protobuff.
    4. If the number of query result Lists can be estimated, the initial capacity can also be set.

    It feels like you want 10,000 objects, but you don’t want to create 10,000 objects. This is a self-contradictory thing, and there seems to be no solution.

    reply
    0
  • Cancelreply