forked from rails/jbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
Optimize array! DSL
#14
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
moberegger
merged 9 commits into
main
from
moberegger/affinity/optimize-array-partial-renders
Jun 16, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9dcf3f7
Optimize array! DSL
moberegger dd20ab1
Better array! optimization
moberegger 77d740a
Default collections args to nil
moberegger 957d044
Stop allocating memory for empty collections
moberegger 9fb0b81
Save memory allocations for internal array calls
moberegger 2aa6f20
Make constants private
moberegger c7c88ff
Add support for present?
moberegger e380da0
More frozen empty arrays
moberegger 0cdb1b6
Add benchmark code
moberegger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'benchmark/ips' | ||
| require 'benchmark/memory' | ||
| require_relative '../../lib/jbuilder' | ||
|
|
||
| Post = Struct.new(:id, :body) | ||
| json = Jbuilder.new | ||
| posts = 3.times.map { Post.new(it, "Post ##{it}") } | ||
|
|
||
| Benchmark.ips do |x| | ||
| x.report('before') do | ||
| json.set! :posts, posts do |post| | ||
| json.extract! post, :id, :body | ||
| end | ||
| end | ||
| x.report('after') do | ||
| json.set! :posts, posts do |post| | ||
| json.extract! post, :id, :body | ||
| end | ||
| end | ||
|
|
||
| x.hold! 'temp_array_ips' | ||
| x.compare! | ||
| end | ||
|
|
||
| json = Jbuilder.new | ||
|
|
||
| Benchmark.memory do |x| | ||
| x.report('before') do | ||
| json.set! :posts, posts do |post| | ||
| json.extract! post, :id, :body | ||
| end | ||
| end | ||
| x.report('after') do | ||
| json.set! :posts, posts do |post| | ||
| json.extract! post, :id, :body | ||
| end | ||
| end | ||
|
|
||
| x.hold! 'temp_array_memory' | ||
| x.compare! | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'benchmark/ips' | ||
| require 'benchmark/memory' | ||
| require_relative '../../lib/jbuilder' | ||
|
|
||
| Post = Struct.new(:id, :body) | ||
| json = Jbuilder.new | ||
| posts = 3.times.map { Post.new(it, "Post ##{it}") } | ||
|
|
||
| Benchmark.ips do |x| | ||
| x.report('before') do | ||
| json.set! :posts, posts, :id, :body | ||
| end | ||
| x.report('after') do | ||
| json.set! :posts, posts, :id, :body | ||
| end | ||
|
|
||
| x.hold! 'temp_array_ips' | ||
| x.compare! | ||
| end | ||
|
|
||
| json = Jbuilder.new | ||
|
|
||
| Benchmark.memory do |x| | ||
| x.report('before') do | ||
| json.set! :posts, posts, :id, :body | ||
| end | ||
| x.report('after') do | ||
| json.set! :posts, posts, :id, :body | ||
| end | ||
|
|
||
| x.hold! 'temp_array_memory' | ||
| x.compare! | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'benchmark/ips' | ||
| require 'benchmark/memory' | ||
| require_relative '../../lib/jbuilder' | ||
| require_relative '../../lib/jbuilder/jbuilder_template' | ||
|
|
||
| Post = Struct.new(:id, :body) | ||
| json = JbuilderTemplate.new nil | ||
| posts = 3.times.map { Post.new(it, "Post ##{it}") } | ||
|
|
||
| Benchmark.ips do |x| | ||
| x.report('before') do | ||
| json.array!(nil) | ||
| end | ||
| x.report('after') do | ||
| json.array!(nil) | ||
| end | ||
|
|
||
| x.hold! 'temp_array_ips' | ||
| x.compare! | ||
| end | ||
|
|
||
| json = JbuilderTemplate.new nil | ||
|
|
||
| Benchmark.memory do |x| | ||
| x.report('before') { json.array! posts, :id, :body } | ||
| x.report('after') { json.array! posts, :id, :body } | ||
|
|
||
| x.hold! 'temp_array_memory' | ||
| x.compare! | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| require 'jbuilder/version' | ||
| require 'json' | ||
| require 'active_support/core_ext/hash/deep_merge' | ||
| require 'active_support/core_ext/object/blank' | ||
|
|
||
| class Jbuilder | ||
| @@key_formatter = nil | ||
|
|
@@ -33,14 +34,16 @@ def self.encode(*args, &block) | |
| new(*args, &block).target! | ||
| end | ||
|
|
||
| BLANK = Blank.new | ||
| BLANK = Blank.new.freeze | ||
| EMPTY_ARRAY = [].freeze | ||
| private_constant :BLANK, :EMPTY_ARRAY | ||
|
|
||
| def set!(key, value = BLANK, *args, &block) | ||
| result = if ::Kernel.block_given? | ||
| if !_blank?(value) | ||
| # json.comments @post.comments { |comment| ... } | ||
| # { "comments": [ { ... }, { ... } ] } | ||
| _scope{ array! value, &block } | ||
| _scope{ _array value, &block } | ||
| else | ||
| # json.comments { ... } | ||
| # { "comments": ... } | ||
|
|
@@ -60,7 +63,7 @@ def set!(key, value = BLANK, *args, &block) | |
| elsif _is_collection?(value) | ||
| # json.comments @post.comments, :content, :created_at | ||
| # { "comments": [ { "content": "hello", "created_at": "..." }, { "content": "world", "created_at": "..." } ] } | ||
| _scope{ array! value, *args } | ||
| _scope{ _array value, args } | ||
|
Comment on lines
-63
to
+66
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. json.set! :posts, posts, :id, :body |
||
| else | ||
| # json.author @post.creator, :name, :email_address | ||
| # { "author": { "name": "David", "email_address": "[email protected]" } } | ||
|
|
@@ -207,18 +210,8 @@ def child! | |
| # json.array! [1, 2, 3] | ||
| # | ||
| # [1,2,3] | ||
| def array!(collection = [], *attributes, &block) | ||
| array = if collection.nil? | ||
| [] | ||
| elsif ::Kernel.block_given? | ||
| _map_collection(collection, &block) | ||
| elsif attributes.any? | ||
| _map_collection(collection) { |element| _extract element, attributes } | ||
| else | ||
| _format_keys(collection.to_a) | ||
| end | ||
|
|
||
| @attributes = _merge_values(@attributes, array) | ||
| def array!(collection = EMPTY_ARRAY, *attributes, &block) | ||
| _array(collection, attributes, &block) | ||
| end | ||
|
|
||
| # Extracts the mentioned attributes or hash elements from the passed object and turns them into attributes of the JSON. | ||
|
|
@@ -244,7 +237,7 @@ def extract!(object, *attributes) | |
|
|
||
| def call(object, *attributes, &block) | ||
| if ::Kernel.block_given? | ||
| array! object, &block | ||
| _array object, &block | ||
| else | ||
| _extract object, attributes | ||
| end | ||
|
|
@@ -277,6 +270,20 @@ def target! | |
|
|
||
| alias_method :method_missing, :set! | ||
|
|
||
| def _array(collection = EMPTY_ARRAY, attributes = nil, &block) | ||
| array = if collection.nil? | ||
| EMPTY_ARRAY | ||
| elsif block | ||
| _map_collection(collection, &block) | ||
| elsif attributes.present? | ||
| _map_collection(collection) { |element| _extract element, attributes } | ||
| else | ||
| _format_keys(collection.to_a) | ||
| end | ||
|
|
||
| @attributes = _merge_values(@attributes, array) | ||
| end | ||
|
|
||
| def _extract(object, attributes) | ||
| if ::Hash === object | ||
| _extract_hash_values(object, attributes) | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.