Optimize select_items for the most common use case: key exists.

This commit is contained in:
Juerd Waalboer 2013-02-28 01:44:46 +01:00
parent 551c22c8ba
commit 4df8d427ed
2 changed files with 5 additions and 5 deletions

View file

@ -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;
}
}

View file

@ -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;
}