VarSet: implement deep copying ourselves instead of using Marshal for a speed increase

This commit is contained in:
Josh Holtrop 2014-04-15 09:16:30 -04:00
parent fdc5c8773c
commit f5be3a3571

View File

@ -40,14 +40,14 @@ module Rscons
# @param values [VarSet, Hash] New set of variables. # @param values [VarSet, Hash] New set of variables.
def append(values) def append(values)
values = values.vars if values.is_a?(VarSet) values = values.vars if values.is_a?(VarSet)
@vars.merge!(Marshal.load(Marshal.dump(values))) @vars.merge!(deep_dup(values))
self self
end end
# Create a new VarSet object based on the first merged with other. # Create a new VarSet object based on the first merged with other.
# @param other [VarSet, Hash] Other variables to add or overwrite. # @param other [VarSet, Hash] Other variables to add or overwrite.
def merge(other = {}) def merge(other = {})
VarSet.new(Marshal.load(Marshal.dump(@vars))).append(other) VarSet.new(deep_dup(@vars)).append(other)
end end
alias_method :clone, :merge alias_method :clone, :merge
@ -75,5 +75,25 @@ module Rscons
end end
end end
end end
private
# Create a deep copy of a Hash or Array.
# @param obj [Hash, Array] Hash or Array to deep copy.
# @return [Hash, Array] Deep copied value.
def deep_dup(obj)
if obj.is_a?(Hash)
obj.reduce({}) do |result, (k, v)|
result[k] = deep_dup(v)
result
end
elsif obj.is_a?(Array)
obj.map { |v| deep_dup(v) }
elsif obj.is_a?(String)
obj.dup
else
obj
end
end
end end
end end