Java Common Refactoring Optimization summary--Experience yourself

Source: Internet
Author: User

Code Refactoring
6 Major principles:
Single principle of responsibility (a class should preferably have only one behavioral motive, too much responsibility can lead to too high coupling),
Open closure principle (functions can be extended, but cannot be modified internally),
Dependency reversal principle (should rely on abstraction rather than on specific objects),
The principle of substitution of the Richter scale (the behavior of the subclass of the parent class is unchanged.) It is with the Richter substitution principle that the "open-closed" principle becomes possible),
Interface Isolation principle (provides a wide, narrow, different interface for the same role to handle different clients),
Dimitri rule (least known principle; if two classes do not have to communicate directly with each other, then these two classes should not have a direct interaction)
Design Pattern Application:
<1.Adapter mode (from original Sphinx to SOLR application)
<2.Abstract Factory Method Mode + Reflection + configuration file for configurable dynamics (CSV, bean, JSON, etc.)
<3.Builder mode: Separates the construction of a complex object from its representation so that the same build process can create different representations
It is mainly used to parse the query conditions passed by the client to construct and encapsulate, and generate corresponding solrquery. And after the final query is completed, the JSON format of the corresponding return is built.
<4.Filter Mode: Allows developers to use different criteria to filter a set of objects and connect them in a decoupled way through logical operations.
Filter filter incoming query time, filter the existing cluster location query scope (hot/recent/warm/all)
<5.Observer pattern: Defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time. This subject object notifies all observer objects when the state changes, enabling them to automatically update themselves (simulated ZK)
Real-time preprocessing triggers updates to the underlying dependent database updates.
Multiple update classes registered to the Unified Listener + cache Mamcache + departure update module (monitoring database data changes + indicators) + database

Code Tuning
<1. You can specify the classes and methods of the final adornment, so be sure to specify that the Java compiler will inline all the final methods to improve the efficiency of the Java operation. The Java compiler will look for opportunities to inline all final methods, and inline is important for improving the efficiency of Java operations, see Java Runtime Optimizations. This will increase the performance by an average of 50%.
<2. string concatenation cannot use + to splice, the bottom layer will create a new object, the two objects are joined together, should use Stringbuilder/stringbuffer
<3. Try to use local variables. Because this is created in the stack, the content disappears as the method runs, and no additional garbage collection is required
<4. In some large loops, minimize repetitive calculations; For example, length in a For loop

The invocation of a method, even if there is only one sentence in the method, is consumed, including creating the stack frame, protecting the scene when calling the method, recovering the site when the method is called, and so on. So for example, the following actions:

    for (int i = 0; i < list.size (); i++)    {...}

Suggested substitutions are:

    int length = List.size (); i < length; i++)    {...}

In this way, when the list.size () is very large, it reduces a lot of consumption


<5. Try to use lazy loading mode to create when needed
<6. Do not use try...catch in loops ...
<7. When the length of the content to be added can be evaluated, then when the container is initialized, it is directly specified to open up such a large space, except for HashMap, which is about 2 of the power size, because it is stored in list form.

such as ArrayList, Linkedllist, StringBuilder, StringBuffer, HashMap, HashSet, and so on, take StringBuilder as an example:

(1) StringBuilder ()//16-character space is assigned by default

(2) StringBuilder (int size)//space with size characters assigned by default

(3) StringBuilder (String str)//default 16 characters +str.length () character space

The initialization capacity of a class can be set by its initialization function, which can significantly improve performance. For example, StringBuilder, length indicates the number of characters the current StringBuilder can hold. Because when the StringBuilder reaches its maximum capacity, it will increase its capacity to the current twice times plus 2, whenever the StringBuilder reaches its maximum capacity, it will have to create a new character array and copy the old character array contents into the new character array-- This is an operation that is very expensive to perform. Imagine, if you can estimate the character array to hold about 5,000 characters without specifying the length, the nearest 5000 of the 2 power is 4096, each expansion plus 2 regardless, then:

(1) on the basis of 4096, and then apply 8,194 size of the character array, add up to the equivalent of a 12,290-size character array, if you can initially specify a 5,000-size character array, saving more than a space

(2) Copy the original 4,096 characters into the new character array.

In this way, both the memory space is wasted and the code runs efficiently. Therefore, it is wrong to set a reasonable initialization capacity for the collection and tool class of the underlying array implementation, which will bring an immediate effect. Note, however, that a collection like HashMap is implemented as an array + linked list, so do not set the initial size to the size you estimate because it is almost 0 more likely to connect an object on a table. The initial size proposal is set to the power of N of 2, which can be set to new HashMap (128), and new HashMap (256) if it can be estimated to have 2000 elements.


<8. When copying large amounts of data, it is best to use the System.arraycopy () method [when the content length is greater than 10000, use this method, or it is too small to use for a loop faster]
<9. Multiplication and division use bit arithmetic as much as possible
<10. Do not create objects within a loop
<11. Do not create unused objects, do not introduce unused references.
<12. Using database connection pooling with databases
<13. IO operation with buffered input and output streams: BufferedReader, Bufferedinputstream/bufferedwriter, Bufferedoutputstream

BufferedReader will read from the physical stream at once 8k(the default value, can be set) byte content into memory, if the outside request, it will be accessed here, if not in the memory in the physical stream to read again.  Even if read, also 8k. If you do not use BufferedReader to read the physical stream directly, it is read by Byte, and each read of the physical stream has IO operations. IO operations are the most time-consuming. BufferedReader is to reduce the amount of IO operations and save you time.

Simply put, an IO operation, read a byte is also read, read 8k bytes is also read, the time difference between the two takes a little. It takes a lot of time to get back and forth from one IO.Like a large car (set up 100 people), to go to the station pick up to the company, followed by a person is also connected, and then 100 people are connected, and the same time. Obviously, it's the best deal to pick up 100 of people. The physical flow is one byte at a time (one person) buffered is a 8k byte (100 people) for reading a fixed length byte file, of course BufferedReader faster!


<14. The base type is converted to a string, using the fastest. ToString () method

Therefore, the ToString () method is preferred when you encounter a conversion of a basic data type into string. As for why, it's simple:

1. The string.valueof () method calls the Integer.tostring () method at the bottom, but it will be bearish before the call

2, Integer.tostring () method will not say, directly called the

3, i + "" bottom using StringBuilder implementation, first with the Append method splicing, and then use the ToString () method to get the string

In contrast, the three are clearly 2 fastest, 1 times, 3 slowest


<15. Traversing map corresponding to the Key-value, it is best to use the iterator EntrySet method, if only using the key value, the use of Hm.keyset (); more reasonable
<16. Use more of the packages provided under Apache: StringUtils, Collections, arrays convert list, or use Guava:joiner, Splitter, Strings, maps to empty

Java Common Refactoring Optimization summary--Experience yourself

Related Article

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.