Skip to content

Extremely inefficient instantiating a distant local date #12

@autarch

Description

@autarch

Migrated from rt.cpan.org #47671 (status was 'open')

Requestors:

From mschwern@cpan.org (@schwern) on 2009-07-08 02:01:30:

$ time perl -wle 'use DateTime; print DateTime->new( year => 3058,
time_zone => "local" )'
3058-01-01T00:00:00

real 0m9.558s
user 0m8.619s
sys 0m0.165s

This occurs whether time_zone is "local" or set to a zone like
"America/Chicago".

Profiling reveals that this makes tens of thousands of method calls.
The big hog is 10000 calls to DateTime::set_time_zone which is being
called by add_duration() which all traces back to
DateTime::TimeZone::_generate_spans_until_match looping over OlsenDB
calls 2000+ times.

This all traces back to DateTime->offset called by
DateTime::_handle_offset_modifier and DateTime::_calc_local_rd

Its possible one could hold off doing whatever it is that calc_local_rd
does, considering that information is not necessary just to instantiate
an object, but the root problem is _generate_spans_until_match's
algorithm. I'm not sure what its purpose is, but it appears to be
iterative by year resulting in Y iterations where Y is the year being
search for (minus some arbitrary lower limit).

If the whole purpose is to determine the time zone offset, this
algorithm is much more efficiently done as a binary search. Even if we
assume a 64 bit year it will take at most 64 iterations to zero in on
the right year. And so on for the month, day, hour, minute and second.

If its just searching the Olsen DB for applicable zone changes I don't
pretend to understand how that works but there's got to be a better way
to do it. C libraries can do it in milliseconds and Perl is just not
that slow. Perhaps with some sort of... database?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions