Skip to content

Commit 3bf6df0

Browse files
committed
Avoid an array allocation per element in list passed to seplist
The array allocation was because the keyword splat expression is not recognized as safe by the compiler. Also avoid unnecessary >= method call per element. This uses a private constant to avoid unnecessary work at runtime. I assume the only reason this code is needed is because v may end with a ruby2_keywords hash that we do not want to treat as keywords. This issue was found by the performance warning in Ruby feature 21274.
1 parent 5b5d483 commit 3bf6df0

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

lib/pp.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,20 @@ def comma_breakable
276276
def seplist(list, sep=nil, iter_method=:each) # :yield: element
277277
sep ||= lambda { comma_breakable }
278278
first = true
279+
kwsplat = EMPTY_HASH
279280
list.__send__(iter_method) {|*v|
280281
if first
281282
first = false
282283
else
283284
sep.call
284285
end
285-
RUBY_VERSION >= "3.0" ? yield(*v, **{}) : yield(*v)
286+
kwsplat ? yield(*v, **kwsplat) : yield(*v)
286287
}
287288
end
289+
EMPTY_HASH = if RUBY_VERSION >= "3.0"
290+
{}.freeze
291+
end
292+
private_constant :EMPTY_HASH
288293

289294
# A present standard failsafe for pretty printing any given Object
290295
def pp_object(obj)

0 commit comments

Comments
 (0)