Skip to content

Commit

Permalink
Allocate new hash with entries_capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
jgaskins committed Aug 25, 2024
1 parent 8601403 commit 20d5297
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/hash.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ class Hash(K, V)
# hash.transform_keys { |key, value| key.to_s * value } # => {"a" => 1, "bb" => 2, "ccc" => 3}
# ```
def transform_keys(& : K, V -> K2) : Hash(K2, V) forall K2
copy = Hash(K2, V).new(initial_capacity: size <= 4 ? 0 : size)
copy = Hash(K2, V).new(initial_capacity: entries_capacity)
each_with_object(copy) do |(key, value), memo|
memo[yield(key, value)] = value
end
Expand All @@ -1763,7 +1763,7 @@ class Hash(K, V)
# hash.transform_values { |value, key| "#{key}#{value}" } # => {:a => "a1", :b => "b2", :c => "c3"}
# ```
def transform_values(& : V, K -> V2) : Hash(K, V2) forall V2
copy = Hash(K, V2).new(initial_capacity: size <= 4 ? 0 : size)
copy = Hash(K, V2).new(initial_capacity: entries_capacity)
each_with_object(copy) do |(key, value), memo|
memo[key] = yield(value, key)
end
Expand Down

0 comments on commit 20d5297

Please sign in to comment.