1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Prefer the faster and more idiomatic Array#map in json_clone (#4313)

* Prefer the faster Array#map to a manual implementation

* On the benchmark from https://github.com/mperham/sidekiq/pull/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
This commit is contained in:
Benoit Daloze 2019-10-06 18:55:35 +02:00 committed by Mike Perham
parent b138213d4d
commit 3380c12af9

View file

@ -279,24 +279,20 @@ module Sidekiq
# 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
else
duped = obj.dup
end
duped
else
obj.dup
end
end
end
end