Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
GoogleCodeExporter opened this issue Apr 3, 2016 · 7 comments
Closed

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

GoogleCodeExporter opened this issue Apr 3, 2016 · 7 comments

Comments

@GoogleCodeExporter
Copy link

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
Copy link
Author

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

  • Changed state: Accepted
  • Added labels: Type-Enhancement
  • Removed labels: Type-Defect

@GoogleCodeExporter
Copy link
Author

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
Copy link
Author

Thank you for the suggestion, I will check it.

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

@GoogleCodeExporter
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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

  • Changed state: Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant