We measured in multithreaded environment that static FUNCTION_MATCHER and FUNCTION_HEADER_MATCHER in JSONUtil is big performence bottleneck. The reason is:
* the matchers are static
* java.util.regex.Pattern.matcher has synchronized block
* JSONUtils.isFunction is called many times for one JSon tree serialization even there is no function
We can see in a thread dump that there are many threads waiting for that lock:
"http-0.0.0.0-8080-62" daemon prio=6 tid=0x000000000b43b310 nid=0x174 waiting for monitor entry [0x0000000056a0d000..0x0000000056a0f8e0]
at java.util.regex.Pattern.matcher(Pattern.java:875)
- waiting to lock <0x000000002803aff0> (a java.util.regex.Pattern)
at net.sf.json.regexp.JdkRegexpMatcher.matches(JdkRegexpMatcher.java:43)
at net.sf.json.util.JSONUtils.isFunction(JSONUtils.java:278)
at net.sf.json.JSONObject._processValue(JSONObject.java:2388)
at net.sf.json.JSONObject._setInternal(JSONObject.java:2439)
at net.sf.json.JSONObject._fromJSONObject(JSONObject.java:847)
at net.sf.json.JSONObject.fromObject(JSONObject.java:159)
at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:104)
at net.sf.json.JSONObject._processValue(JSONObject.java:2387)
at net.sf.json.JSONObject._setInternal(JSONObject.java:2439)
at net.sf.json.JSONObject._fromJSONObject(JSONObject.java:847)
at net.sf.json.JSONObject.fromObject(JSONObject.java:159)
at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:104)
at net.sf.json.JSONObject._processValue(JSONObject.java:2387)
at net.sf.json.JSONObject._setInternal(JSONObject.java:2439)
at net.sf.json.JSONObject._fromJSONObject(JSONObject.java:847)
at net.sf.json.JSONObject.fromObject(JSONObject.java:159)
at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:104)
at net.sf.json.JSONArray._processValue(JSONArray.java:2220)
at net.sf.json.JSONArray.processValue(JSONArray.java:2273)
at net.sf.json.JSONArray.addValue(JSONArray.java:2260)
at net.sf.json.JSONArray.element(JSONArray.java:1441)
at net.sf.json.JSONObject._accumulate(JSONObject.java:2372)
at net.sf.json.JSONObject.accumulate(JSONObject.java:1302)
The attached patch simply avoids calling of Pattern.matcher if passed string is not a function (does not start with "function[ ]").
JSONUtils.java patch
Logged In: YES
user_id=1192026
Originator: NO
Thank you for the patch Ales, I'll look into it tonight.
Logged In: YES
user_id=1192026
Originator: NO
PATCHED.