JSON Serialization is very slow
-------------------------------
Key: SHINDIG-633
URL: https://issues.apache.org/jira/browse/SHINDIG-633
Project: Shindig
Issue Type: Improvement
Components: Common Components (Java)
Reporter: Paul Lindner
BeanJsonConverter recalcuates the getters for a given method for a class every
single time. This is terribly inefficient and results in regex code running
for every serialize.
Patch that caches these follows:
+++
java/social-api/src/main/java/org/apache/shindig/social/core/util/BeanJsonConverter.java
(working copy)
@@ -44,6 +44,7 @@
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import java.util.concurrent.ConcurrentHashMap;
/**
* Converts pojos to json objects.
@@ -168,9 +169,19 @@
}
}
+
+ private static final ConcurrentHashMap<Class,List<MethodPair>>
CLASS_TO_METHODS = new ConcurrentHashMap<Class,List<MethodPair>>();
+
private List<MethodPair> getMatchingMethods(Object pojo, Pattern pattern) {
- List<MethodPair> availableGetters = Lists.newArrayList();
+ List<MethodPair> availableGetters;
+ Class<?> clz = pojo.getClass();
+ availableGetters = CLASS_TO_METHODS.get(clz);
+
+ if (availableGetters != null)
+ return availableGetters;
+
+ availableGetters = Lists.newArrayList();
Method[] methods = pojo.getClass().getMethods();
for (Method method : methods) {
Matcher matcher = pattern.matcher(method.getName());
@@ -185,6 +196,7 @@
}
availableGetters.add(new MethodPair(method, fieldName));
}
+ CLASS_TO_METHODS.put(clz, availableGetters);
return availableGetters;
}
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.