Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Give Date its own BUILD
making Date creation 1.5x faster
  • Loading branch information
lizmat committed Dec 25, 2015
1 parent 8b4fe44 commit a9c49a2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/core/Date.pm
@@ -1,9 +1,11 @@
my class Date does Dateish {

method BUILD($!year,$!month,$!day,$!daycount = Int) { self }

multi method new(Date: Int() $year, Int() $month, Int() $day) {
(1..12).in-range($month,'Month');
(1 .. self!DAYS-IN-MONTH($year,$month)).in-range($day,'Day');
self.bless(:$year, :$month, :$day)
nqp::create(self).BUILD($year,$month,$day)
}
multi method new(Date: :$year!, :$month = 1, :$day = 1) {
self.new($year,$month,$day)
Expand All @@ -23,15 +25,15 @@ my class Date does Dateish {
self.new(+$0,+$1,+$2)
}
multi method new(Date: Dateish $d) {
self.bless(:year($d.year), :month($d.month), :day($d.day));
nqp::create(self).BUILD($d.year,$d.month,$d.day)
}
multi method new(Date: Instant $i) {
self.new(DateTime.new($i))
}

method new-from-daycount($daycount) {
my ($year, $month, $day) = self!ymd-from-daycount($daycount);
self.bless(:$daycount, :$year, :$month, :$day);
nqp::create(self).BUILD($year,$month,$day,$daycount);
}

method today() {
Expand Down Expand Up @@ -110,8 +112,12 @@ my class Date does Dateish {
self.new(|%args);
}
method !clone-without-validating(*%_) { # A premature optimization.
my %args = :$!year, :$!month, :$!day, %_;
self.bless(|%args);
my $h := nqp::getattr(%_,Map,'$!storage');
nqp::create(self).BUILD(
nqp::existskey($h,'year') ?? nqp::atkey($h,'year') !! $!year,
nqp::existskey($h,'month') ?? nqp::atkey($h,'month') !! $!month,
nqp::existskey($h,'day') ?? nqp::atkey($h,'day') !! $!day,
)
}

method succ(Date:D:) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/Dateish.pm
@@ -1,7 +1,7 @@
my role Dateish {
has Int $.year;
has Int $.month = 1;
has Int $.day = 1;
has Int $.month;
has Int $.day;
has Int $.daycount;

method IO(|c) { IO::Path.new(self) } # because Dateish is not Cool
Expand Down

0 comments on commit a9c49a2

Please sign in to comment.