From 4df8d427edb47b4f94fa1cba0b7000bde95bda88 Mon Sep 17 00:00:00 2001 From: Juerd Waalboer Date: Thu, 28 Feb 2013 01:44:46 +0100 Subject: [PATCH] Optimize select_items for the most common use case: key exists. --- RevBank/Cart.pm | 6 +++--- plugins/deposit | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/RevBank/Cart.pm b/RevBank/Cart.pm index 0ec6bd0..c4a39e0 100644 --- a/RevBank/Cart.pm +++ b/RevBank/Cart.pm @@ -110,13 +110,13 @@ sub select_items { use v5.12; # New smartmatch semantics - my $match_all = (@_ == 1); # Match everything if no key/match is given. - my @matches; for my $user (keys %$self) { for my $item (@{ $self->{$user} }) { push @matches, { user => $user, %$item } - if $match_all or $item->{ $key } ~~ $smartmatch; + if @_ == 1 # No key or match given: match everything + or @_ == 2 and exists $item->{ $key } # Just a key + or @_ == 3 and $item->{ $key } ~~ $smartmatch; } } diff --git a/plugins/deposit b/plugins/deposit index ef28bca..4fae454 100755 --- a/plugins/deposit +++ b/plugins/deposit @@ -11,7 +11,7 @@ sub command :Tab(deposit) { return "Amount to deposit into your account", \&amount; } - if ($cart->select_items(is_deposit => 1)) { + if ($cart->select_items('is_deposit')) { # No other plugin recognised the input, so it must be a new user. $self->{new_user} = $command; return "Add new account for user '$command'?", \&create; @@ -46,7 +46,7 @@ sub create { sub hook_checkout { my ($class, $cart, $user, $transaction_id) = @_; my $sum; - $sum += $_->{amount} for $cart->select_items(is_deposit => 1); + $sum += $_->{amount} for $cart->select_items('is_deposit'); say sprintf "Don't forget to add EUR %.2f to the cash box!", $sum if $sum; }