Skip to content

Optimize _blank? - #622

Open
moberegger wants to merge 2 commits into
rails:mainfrom
moberegger:moberegger/optimize-blank
Open

Optimize _blank?#622
moberegger wants to merge 2 commits into
rails:mainfrom
moberegger:moberegger/optimize-blank

Conversation

@moberegger

Copy link
Copy Markdown
Contributor

Switches _blank? from BLANK == value to Blank === value.

Originally _blank? did a Ruby-level dispatch to Blank#==, which itself calls super (for an identity check) and then Blank === other. We can short circuit this to simply be Blank === other. This gives us the same result for less work because Blank#== would always return true for any Blank instance. We don't actually need to an identity check.

blank1 = Jbuilder::Blank.new
blank2 = Jbuilder::Blank.new

puts blank1 == blank2 # true
puts blank2 == blank1 # true

Below is an IPS benchmark against a simple builder that exercises _blank a few times.

json.foo do
  json.bar 123
end
ruby 4.0.6 (2026-07-14 revision 03b6d3f889) +PRISM [arm64-darwin23]
Warming up --------------------------------------
                 old    50.799k i/100ms
                 new    61.777k i/100ms
Calculating -------------------------------------
                 old    500.692k (± 2.6%) i/s    (2.00 μs/i) -      2.540M in   5.072875s
                 new    643.490k (± 1.4%) i/s    (1.55 μs/i) -      3.274M in   5.088164s

Comparison:
old:   500692.4 i/s
new:   643489.7 i/s - 1.29x  faster

Comment thread lib/jbuilder/blank.rb
Comment on lines -5 to -8
def ==(other)
super || Blank === other
end

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary for the optimization, but figured I'd remove it since it is no longer running in the gem.

I suppose it's possible the a user is doing something with Jbuilder::Blank directly that may require this, but I believe this class was intended to be an implementation detail.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant