Skip to content

Commit

Permalink
Prefer the faster and more idiomatic Array#map in json_clone (#4313)
Browse files Browse the repository at this point in the history
* Prefer the faster Array#map to a manual implementation

* On the benchmark from #4303
  on MRI 2.6.4, before 396657.9 i/s, after 461013.3 i/s.

* Use `duped` as the return value in the only branch it's needed
  • Loading branch information
eregon authored and mperham committed Oct 6, 2019
1 parent b138213 commit 3380c12
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions lib/sidekiq/processor.rb
Expand Up @@ -279,24 +279,20 @@ def constantize(str)
# been mutated by the worker.
def json_clone(obj)
if Integer === obj || Float === obj || TrueClass === obj || FalseClass === obj || NilClass === obj
return obj
obj
elsif String === obj
return obj.dup
obj.dup
elsif Array === obj
duped = Array.new(obj.size)
obj.each_with_index do |value, index|
duped[index] = json_clone(value)
end
obj.map { |e| json_clone(e) }
elsif Hash === obj
duped = obj.dup
duped.each_pair do |key, value|
duped[key] = json_clone(value)
end
duped
else
duped = obj.dup
obj.dup
end

duped
end
end
end

0 comments on commit 3380c12

Please sign in to comment.