Skip to content

Commit

Permalink
Make basic argument checking on DateTime.later a lot faster
Browse files Browse the repository at this point in the history
Improves performance of Date.later with about 30%, mostly because
of a lot less allocations.
  • Loading branch information
lizmat committed Nov 11, 2018
1 parent 0fa6988 commit 08fac04
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/core/Date.pm6
Expand Up @@ -104,12 +104,21 @@ my class Date does Dateish {
}

method later(:$earlier, *%unit) {
my @pairs = %unit.pairs;
die "More than one time unit supplied" if @pairs > 1;
die "No time unit supplied" unless @pairs;

my $unit = self!VALID-UNIT(@pairs.AT-POS(0).key);
my $amount = @pairs.AT-POS(0).value.Int;
# basic sanity check
nqp::if(
nqp::eqaddr(
(my \later := (my \iterator := %unit.iterator).pull-one),
IterationEnd
),
(die "No time unit supplied"),
nqp::unless(
nqp::eqaddr(iterator.pull-one,IterationEnd),
(die "More than one time unit supplied")
)
);
my $unit := later.key;
my $amount = later.value;
$amount = -$amount if $earlier;

if nqp::atkey($valid-units,$unit) -> $multiplier {
Expand Down

0 comments on commit 08fac04

Please sign in to comment.