Skip to content

Commit

Permalink
[Immutablize#convert_value] reduce number of class comparisons
Browse files Browse the repository at this point in the history
Signed-off-by: David Crosby <dcrosby@fb.com>
  • Loading branch information
dafyddcrosby committed Feb 9, 2024
1 parent e8860dc commit 13075a3
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/chef/node/immutable_collections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,21 @@ def safe_dup(e)
end

def convert_value(value)
# The order in this case statement is *important*.
# ImmutableMash and ImmutableArray should be tested first,
# as this saves unnecessary creation of intermediate objects
case value
when ImmutableMash, ImmutableArray
value
when Hash
ImmutableMash.new(value, __root__, __node__, __precedence__)
if ImmutableMash === value
# Save an object creation
value
else
ImmutableMash.new(value, __root__, __node__, __precedence__)
end
when Array
ImmutableArray.new(value, __root__, __node__, __precedence__)
if ImmutableArray === value
# Save an object creation
value
else
ImmutableArray.new(value, __root__, __node__, __precedence__)
end
else
safe_dup(value).freeze
end
Expand Down

0 comments on commit 13075a3

Please sign in to comment.