[ruby/pp] 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.

https://github.com/ruby/pp/commit/3bf6df0e5c
This commit is contained in:
Jeremy Evans 2025-04-19 23:47:36 -07:00 committed by git
parent 1628bbb18a
commit 3ce5d89c20

View File

@ -276,15 +276,20 @@ class PP < PrettyPrint
def seplist(list, sep=nil, iter_method=:each) # :yield: element
sep ||= lambda { comma_breakable }
first = true
kwsplat = EMPTY_HASH
list.__send__(iter_method) {|*v|
if first
first = false
else
sep.call
end
RUBY_VERSION >= "3.0" ? yield(*v, **{}) : yield(*v)
kwsplat ? yield(*v, **kwsplat) : yield(*v)
}
end
EMPTY_HASH = if RUBY_VERSION >= "3.0"
{}.freeze
end
private_constant :EMPTY_HASH
# A present standard failsafe for pretty printing any given Object
def pp_object(obj)