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
Sort hash keys on JSON encode #665
Conversation
Make sure JSON result stays the same for a given hash. This is necessary for caching.
@boxi0: In what case does the order of the JSON keys matter for caching? Caching should be handled with HTTP cache headers, not the body. https://developers.google.com/speed/docs/best-practices/caching Also, it will under no circumstances be possible for us to take a pull request without any tests. |
I'm using the response body's JSON string to calculate an ETag hash for this response. So in this case the order of the JSON keys matter. |
Allright, i might have been a bit too quick here, so the change could go away again. Sorting costs quite a bit of performance and we might be better off just recommending sorted hashes as a solution to the rare caching problem. |
Sorry for the confusion, my patch now has two 👎 votes, so i'm reverting it. fadc41e |
If the decision to include/not include this feature is based on performance concerns we should decide what is an acceptable performance hit, and then test to see if this surpasses that threshold. I grabbed the JSON from a GitHub API search, generated a structure from it, and then timed runs with the 'sort' enabled, and with it not (the current code). Here is a gist showing the test: https://gist.github.com/daoswald/005ef45f06d8bafc5519 I ran the test ten times, first with no sort, and then with sort. Then took the average of the ten runs, and finally calculated a percent change. The change was a 2.4% performance penalty for sorting the JSON, using the data demonstrated in the gist. The data set was about 162KB of JSON, decoded to a structure, and then encoded repeatedly in a benchmark. I don't have any idea whether this is an acceptable performance hit to take for the feature, but hopefully this will provide useful information to those giving +1 / -1 to the proposal. |
@daoswald Thanks for the detailed benchmark. |
I'm 👎 I think you should cache based on input, not output. Or find something else that is unique with the request. |
I'm neutral on this issue, 2.4% is not that much of a performance hit, but the use case presented here doesn't convince me either. |
I can solve my problem doing other things, yes. But I think a canonical option like in JSON::JS would be smart. |
Typo: JSON::XS |
On the plus side, you might enjoy this new caching helper that also resulted from this discussion. 😉 http://mojolicio.us/perldoc/Mojolicious/Plugin/DefaultHelpers#is_fresh |
I do, thank you. :) |
Make sure JSON result stays the same for a given hash. This is necessary
for caching.