Skip to content

Commit

Permalink
Optimize cloning of job payload
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima authored and mperham committed Oct 2, 2019
1 parent 6d0279b commit acdfc59
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/sidekiq/processor.rb
Expand Up @@ -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)
Expand All @@ -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

0 comments on commit acdfc59

Please sign in to comment.