From acdfc59ebcf042dd4db26647e9532cfccd79d825 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Wed, 2 Oct 2019 02:02:52 +0300 Subject: [PATCH] Optimize cloning of job payload --- lib/sidekiq/processor.rb | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/sidekiq/processor.rb b/lib/sidekiq/processor.rb index 770b5e4ae..cf2f69fec 100644 --- a/lib/sidekiq/processor.rb +++ b/lib/sidekiq/processor.rb @@ -265,7 +265,7 @@ def stats(job_hash, queue) # the job fails, what is pushed back onto Redis hasn't # been mutated by the worker. def cloned(thing) - Marshal.load(Marshal.dump(thing)) + deep_dup(thing) end def constantize(str) @@ -280,5 +280,27 @@ def constantize(str) constant.const_get(name, false) end end + + def deep_dup(obj) + if Integer === obj || Float === obj || TrueClass === obj || FalseClass === obj || NilClass === obj + return obj + elsif String === obj + return obj.dup + elsif Array === obj + duped = Array.new(obj.size) + obj.each_with_index do |value, index| + duped[index] = deep_dup(value) + end + elsif Hash === obj + duped = obj.dup + duped.each_pair do |key, value| + duped[key] = deep_dup(value) + end + else + duped = obj.dup + end + + duped + end end end