From 0edcce37c73a1180fc0d84026ecc3066fdf01cbf Mon Sep 17 00:00:00 2001 From: Holger Just Date: Tue, 28 Mar 2017 20:05:21 +0200 Subject: [PATCH] Use yield instead of to_proc in Hash#merge This is functionally equivalent. However, the previous behavior resulted in the block being materialized as a proc which is quite expensive, both during materialization as well as when calling it. By using `block_given?`, we can avoid this materialization. --- lib/rackstash/fields/hash.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/rackstash/fields/hash.rb b/lib/rackstash/fields/hash.rb index 89fb70b..839092a 100644 --- a/lib/rackstash/fields/hash.rb +++ b/lib/rackstash/fields/hash.rb @@ -276,8 +276,14 @@ module Rackstash # and `force` is `true` # @return [Rackstash::Fields::Hash] a new hash containing the merged # key-value pairs - def merge(hash, force: true, scope: nil, &block) - dup.merge!(hash, force: force, scope: scope, &block) + def merge(hash, force: true, scope: nil) + if block_given? + dup.merge!(hash, force: force, scope: scope) { |key, old_val, new_val| + yield key, old_val, new_val + } + else + dup.merge!(hash, force: force, scope: scope) + end end # Adds the contents of `hash` to `self`. `hash` is normalized before being