java-Improve reflection efficiency __java

Source: Internet
Author: User
java-Improve reflection efficiency
Feature Description: Java developers are aware of the slow speed of reflection, so many Java developers are discouraged by the use of reflection mechanisms (including BME component SDO). I want to know exactly where the reflex mechanism is slow. There is no way to improve it so that we can continue to use it. If a good thing does not use it because of its own flaws, it is a pity that reflection is the same. What I want to say is: we should improve it a little bit.
Code features: 1. Description
Java developers are aware of the slow speed of reflection, so many Java developers are discouraged by the use of reflection mechanisms (including BME component SDO). I want to know exactly where the reflex mechanism is slow. There is no way to improve it so that we can continue to use it. If a good thing does not use it because of its own flaws, it is a pity that reflection is the same. What I want to say is: we should improve it a little bit.
2. How to use the error
The wrong way to do this is to use the Class.forName method every time you need to get the object of the class, or call Getdeclaredmethod (String name, Class<?&gt, when you need to call a method on the class object). Parametertypes) or GetMethod (String name, Class<?> ... parametertypes) method gets the methods object and then invokes the Invoke (object obj) on it. Object ... args) method.
There are two places that are prone to loss of performance:
The invocation of the Class.forName method executes the search of the class class file under the entire classpath, and frequent invocation comparisons affect performance.
Getdeclaredmethod on Class object (String, Class<?> ...) or GetMethod (String, class<?> ...) Method executes a search on the class object by the Methods object. Some colleagues also use the GetMethods () method to get an array of methods, and then perform the search task, and actually GetMethods () also executes the collective copy ratio of the method object directly (String, Class<?&gt, ...). or GetMethod (String, class<?> ...) Method also consumes time and space.
3. Cache thought
The idea of cache is to store the required reflection middleware for later use. Whatever method is used to get the methods object on the class object, the return is the copy object of the object. It is a pity that some of these copy objects are recycled only once. We can do this to cache these objects for later use. In the storage data structure, it is no doubt that the HashMap search speed is the fastest, it is mainly through the object of the hash code to do a lookup, the speed is super fast. However, the operations on the HashMap are not thread-safe and need to be improved to achieve synchronization.
4. Concrete implementation
Requires two components ClassInfo and Reflectioncache.
ClassInfo mainly holds the information of the class object, mainly the method map. The ClassInfo includes three-part methods of Map:getter, setters, and other. Getter is the method of acquiring attributes of class, Setter is the method of setting the attribute of class, and other is other method. It should be noted that the getter and setter methods need to fully conform to the JavaBean specification (the Isxxx method is within the Getter method Range), and its key value is the property name of the method. The other method is a method other than getter and setter.
The Reflectioncache component caches the ClassInfo primarily through HashMap. The cached key value is the full name of the class object in ClassInfo. As a String object, its cached key value is java.lang.String. And Reflectioncache provides several different get and put methods to facilitate the user's operation.
In addition, the generation of ClassInfo requires the use of classinfoutils tools. Its main task is to create ClassInfo objects, where ClassInfo can provide method type information to specify the type of the cache (such as all method-all, accessor method-access, get method-getter, and set method-setter).
5. Synchronous control
Classinfomap is a static variable in Reflectioncache, then the synchronization problem of HashMap is followed. I have made some improvements to reflectioncache, mainly to the disposal of the Put method. The first HashMap fetch operation (get operation) is not joined to the synchronization operation, so the obtained operation can be concurrent. The problem now is that if you don't get the ClassInfo object, you're going to do a setup operation (set operation), and then the concurrency problem comes along. There may be many threads at the same time to set ClassInfo, and after the first ClassInfo thread finishes, the second thread should stop setting ClassInfo. Above this requirement, we need to add a sync block to the Reflectioncache put operation and let the putting operation perform an extra action: return the ClassInfo added to Classinfomap, whether it was added by another thread or not. So when we set up ClassInfo, we can do this:
ClassInfo ClassInfo = Reflectioncache.putclassinfo (String.class);
6. Improving efficiency
The improvement in efficiency is obvious. The main is to save the intermediate variable creation and reflection data lookup time.
Test data
100,000 times, 20 threads, no class.forname operation
General usage: 11375 milliseconds
reflectioncache:2562 milliseconds


100,000 times, 20 threads, with class.forname operation
General usage: 16125 milliseconds
reflectioncache:4187 milliseconds
Visible use of reflectioncache significantly increased efficiency.
7. How to use
First import the Reflectioncache class file or make it into a jar package and place it under the classpath.
Get ClassInfo:
ClassInfo ClassInfo = Reflectioncache.getclassinfo ("java.lang.String");
Set ClassInfo:
ClassInfo = Reflectioncache.putclassinfo (String.class);


The GetClassInfo and Putclassinfo methods have two overloaded functions, respectively corresponding to the parameters of object and class.

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.