From 09a8131c63667d9de9ee3921e7f877c09c9092c8 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Wed, 28 Mar 2018 14:34:24 +0200 Subject: Move Hash::Merge -> Hash::Merge::Augment, new Hash::Merge from Hash::Merge::F Original Hash::Merge is incredibly slow due to the MONKEY. This is someone that is very unlikely to be what people want. App::Assixt reports parse time to go from 13s to 7s on first run, 13s to 0.3s on subsequent runs, which is *massive*. --- lib/Hash/Merge/Augment.pm6 | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 lib/Hash/Merge/Augment.pm6 (limited to 'lib/Hash/Merge/Augment.pm6') diff --git a/lib/Hash/Merge/Augment.pm6 b/lib/Hash/Merge/Augment.pm6 new file mode 100644 index 0000000..b256b8e --- /dev/null +++ b/lib/Hash/Merge/Augment.pm6 @@ -0,0 +1,63 @@ +#! /usr/bin/env false + +use v6.c; +use MONKEY-TYPING; + +# Don't use precompilation in order to not conflict with other MONKEY-TYPING +# modules. +no precompilation; + +augment class Hash +{ + #| Merges a second hash into the hash the method is called on. Hash given as + #| the argument is not modified. + #| Traverses the full tree, replacing items in the original hash with the + #| hash given in the argument. Does not replace positional elements by default, + #| and instead appends the items from the supplied hash's array to the original + #| hash's array. The object type of positionals is not retained and instead + #| becomes an Array type. + #| Use :no-append-array to replace arrays and positionals instead, which will + #| also retain the original type and not convert to an Array + multi method merge (Hash:U: %b, Bool:D :$no-append-array = False) { + warn "Cannot merge an undefined Hash!"; + return %b; + } + + multi method merge (Hash:D: %b, Bool:D :$no-append-array = False) + { + hashmerge self, %b, :$no-append-array; + } + + sub hashmerge (%merge-into, %merge-source, Bool:D :$no-append-array) + { + for %merge-source.keys -> $key { + if %merge-into{$key}:exists { + given %merge-source{$key} { + when Hash { + hashmerge %merge-into{$key}, + %merge-source{$key}, + :$no-append-array; + } + when Positional { + %merge-into{$key} = $no-append-array + ?? %merge-source{$key} + !! + do { + my @a; + @a.push: $_ for %merge-into{$key}.list; + @a.push: $_ for %merge-source{$key}.list; + @a; + } + } + # Non-positionals, so strings or Bools or whatever + default { %merge-into{$key} = %merge-source{$key} } + } + } else { + %merge-into{$key} = %merge-source{$key}; + } + } + %merge-into; + } +} + +# vim: ft=perl6 ts=4 sw=4 et -- cgit v1.1