-
Notifications
You must be signed in to change notification settings - Fork 440
Substantial performance improvements #54
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Closed
I'm not qualified to review this, but I'd just like to say thanks for taking the time to do this. |
Thanks so much for this. I really appreciate. This has to be the best pull request explanation ever. @dhh, please merge this, if anything to thank this kind soul for his hard work! |
dhh
pushed a commit
that referenced
this pull request
Sep 6, 2012
Substantial performance improvements
klaseskilson
referenced
this pull request
in tkomstadius/bruse
Apr 23, 2015
There is a huge potential problem in Jbuilder's performance when handling partials. Caching things properly should solve this. The solution used comes from http://goo.gl/uWXFKu and http://goo.gl/JyWc5b However, testing this in development doesn't help much, since caching doesn't work in the same way as in production mode.
This was referenced May 28, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi,
Jbuilder is awesome. However, it can be a little slow if the JSON that is built is very large. This patch aims to improve the situation.
Before I start explaining the changes, let me stress to anyone reading this that it is important to use a fast JSON serialisation library, such as
oj
. Please useoj
before considering this patch an improvement. Compared to slower libraries it may yield a 10x performance improvement during serialisation.The baseline benchmark that I used can be found here: https://gist.github.com/3638182 It is a stand-alone method that serialises an object with a couple of attributes and 2 arrays with 100 children each. Each child has 2-4 attributes.
Baseline benchmark (using
oj
as serialisation library),n = 1000
:Benchmark with the changes in this pull request,
n = 1000
:About twice as fast without
Jbuilder.key_format
. When usingJbuilder.key_format :camelize => :lower
the improvements were almost 10x. When used with Rails the performance improvements will be diluted, but could still be significant. It also does not address the performance problems that occur when using (many) partials. I have ideas how to fix those, but have not validated them yet. They also depend on the changes in this pull request.Here is a summary of the performance-related changes/commits and their cumulative effect on the benchmark without key formatting:
Most changes are quite small, but there is one significant change that I'd like to discuss. It is the change that leads to the enormous difference when using a key formatter.
Previously, each JSON key was always formatted when used. When iterating over many children, this would result in a lot of duplicate effort. Therefore, I've introduced a
KeyFormatter
object that will cache known keys and only perform (possibly expensive) formatting if the key isn't known. A newKeyFormatter
is instantiated for each rootJbuilder
object, and reused for child objects.As currently implemented however, it leads to a subtle change in functionality. Previously, it was possible to expand on previously set key formats:
Except it only worked like that when using
Jbuilder
directly, i.e. outside a Railsjbuilder
template. Inside Rails, thekey_format
was not even propagated to child elements:This pull request also fixes the fact that the key format is not propagated in Rails (and adding a few tests for the
JbuilderTemplate
class). However, I did not add the second behaviour: the ability to 'stack' multiple key formatters on top of each other. There were no tests for it and I wasn't sure the behaviour was intentional. It should be possible to add such a feature relatively easy, however.I fully realise this pull request has many changes in it, and that the slightly different behaviour with regards to key formatting needs to be considered carefully.
Please let me know your opinion(s) on these changes. I would be quite happy to adjust these patches if that means some or all of it can be merged.