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 DateTime.later with about 15%, mostly because
of a lot less allocations.
  • Loading branch information
lizmat committed Nov 11, 2018
1 parent c03e71d commit 11cb4fa
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/core/DateTime.pm6
Expand Up @@ -278,12 +278,21 @@ my class DateTime does Dateish {
method hh-mm-ss() { sprintf "%02d:%02d:%02d", $!hour,$!minute,$!second }

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;
# 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;

# work on instant (tai)
Expand Down

0 comments on commit 11cb4fa

Please sign in to comment.