spray / spray-json

A lightweight, clean and simple JSON implementation in Scala
Apache License 2.0
974 stars 190 forks source link

intern() Strings from parsed JSON #143

Closed theon closed 9 years ago

theon commented 9 years ago

Hi there,

We cache in memory the result of parsing some JSON with spray-json. Recently we noticed we had hundreds of MB of copies of the empty String ("") because we get a lot of them in the JSON we parse .

How would you feel about calling intern() on Strings extracted from parsing JSON? It would probably have to be configurable so people who use Java 6 can turn it off. Java 6 puts interned Strings in perm gen :cry: Java 7 onwards puts interned Strings on the main heap :+1:

If you feel this would be satisfactory, I'm happy to make a PR.

sirthias commented 9 years ago

Thanks, Ian, for the suggestion. I'm not sure that interning all strings would be a good thing. After all, internalization isn't free and would significantly impact overall parsing performance. However, not creating a new String instance for empty strings is probably a good thing. Would this simple optimization already provide most of the benefit in your case?

theon commented 9 years ago

That's fair. I guess not everyone would want to make a space/speed trade-off and users can always intern them after parsing if they really want to save heap space. In our particular case the empty string is using an order of magnitude more memory than any other string, so would make the largest impact.

jrudolph commented 9 years ago

:+1:

sirthias commented 9 years ago

Ok, great. If you wanted to put in a PR we'd be happy to look at it. We can't, however, promise a new release in the immediate future as our priorities lay elsewhere ATM.

theon commented 9 years ago

akka-http by any chance? :grin:

Thanks for looking at this. It's not urgent at all, so I don't need a release anytime soon, but I'll put together a PR soonish.

sirthias commented 9 years ago

akka-http doesn't have any JSON support itself (just like spray "proper", spray-json is an independent library).

theon commented 9 years ago

Sorry, I meant is akka-http by any chance where your priorities lie at the moment :D

sirthias commented 9 years ago

Ah, I see. :) Yes, this akka-http thing is also a tiny bit on our todo list right now... ;)