ekahssay / json-simple

Automatically exported from code.google.com/p/json-simple
Apache License 2.0
0 stars 0 forks source link

Slow performance on JSONObject.toJSONString(Map map) #44

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
In the class JSONObject, the method:
    String toJSONString(Map map)
uses on every entry in the map, the private method :
    String toJSONString(String key, Object value, StringBuffer sb)

But this solution leads to poor perfomances on big maps, because the second 
method not only append 'key' and 'value' to the specified StringBuffer 'sb', 
but it also returns a String representation of the StringBuffer 'sb' ( 
returning sb.toString() ). This toString conversion is expensive and can be 
unnecessary for the caller of the method.
It will be better if the method will have as signature :
    void toJSONString(String key, Object value, StringBuffer sb)
leaving to the the caller of the method the responsability to convert the 
buffer in a String, only when needed.
In effect, the method :
    String toJSONString(Map map) 
show poor performances while executed on big maps, suffering for this 
unnecessary conversion on the internal method :
    String toJSONString(String key, Object value, StringBuffer sb)
In our test, on a map with about 16000 entries, it is about 500-1000 times 
slower compared to an experimental implementation that uses a new method :
    void toJSONString(String key, Object value, StringBuffer sb)

Original issue reported on code.google.com by giancarl...@gmail.com on 11 May 2011 at 10:37

GoogleCodeExporter commented 8 years ago

Original comment by fangyid...@gmail.com on 12 May 2011 at 8:56

GoogleCodeExporter commented 8 years ago
May I suggest you to try 
http://code.google.com/p/json-smart
this fork is a json-simple with better performances.

Original comment by uriel.chemouni on 16 May 2011 at 11:35

GoogleCodeExporter commented 8 years ago
Thank you for the suggestion, I will check it.

Original comment by giancarl...@gmail.com on 16 May 2011 at 2:52

GoogleCodeExporter commented 8 years ago
I think the attached patch fixes this issue, though using a slightly different 
approach. The main idea is the same -- serialize nested maps to a single buffer
instead of making repeated copies. 

Original comment by michael....@gmail.com on 28 Jun 2011 at 5:46

Attachments:

GoogleCodeExporter commented 8 years ago
I'd argue that json-simple shouldn't list "High performance" as one of its 
features while this issue is not resolved--or at least it should clarify that 
"high performance" only applies to parsing, not serialization. This issue makes 
serialization an O(n^2) when it should be O(n).

Original comment by michael....@gmail.com on 6 Jul 2011 at 6:01

GoogleCodeExporter commented 8 years ago
There's no need to use StringBuffer (which is synchronized) because the 
corresponding variable is not used by multiple threads. Use StringBuilder 
instead - it's much faster.

Original comment by g.byczyn...@gmail.com on 29 Dec 2011 at 12:46

GoogleCodeExporter commented 8 years ago
This is done in r216.

@g.byczynski Sadly, we can't use StringBuilders as long as we're trying to 
maintain JDK 1.2 compatibility.

Original comment by jon.cham...@gmail.com on 10 Aug 2013 at 4:38