develooper Front page | perl.datetime | Postings from March 2017

small DateTime optimization

Thread Next
From:
Flavio S. Glock
Date:
March 15, 2017 14:13
Subject:
small DateTime optimization
Message ID:
CAHMRfDxT-XQJYEkuLAOeH6GgnhNZf6nJbEuuW3sV2L+uWXWrTw@mail.gmail.com
We've been using this patch at work for years, and I just forgot to
contribute it back. I hope you find it worth applying.

I believe the typical use case was:

  my $dt = $param || DateTime->now();

In this case $param is used in boolean context, which makes a fallback call
to _stringify(). With the patch, "boolean" just returns true.

The stringification change is a micro-optimization, it is not that
important.



        # optimize boolean and string
        use overload
                '""'  => sub {
                               $_[0]->ymd('-') . 'T' . $_[0]->hms(':')
                             },
                bool  => sub { 1 },

                '<=>' => '_compare_overload',
                'cmp' => '_compare_overload',
                '-'   => '_subtract_overload',
                '+'   => '_add_overload',
                fallback   => 1;

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About