Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize cloning of job payload #4303

Merged
merged 1 commit into from
Oct 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion lib/sidekiq/processor.rb
Original file line number Diff line number Diff line change
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