Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: to_offset avoids binary search if the timestamp is greatr than the biggest one #105

Conversation

1996fanrui
Copy link
Contributor

The subsequent optimization of #103

The logic inside of Tzif#to_offset

binary_search_by_key find the index from transitions, when the timestamp is greater than all timestamp in transitions. It's not needed to binary search.

It always happens when some timezones had DST before, but DST is canceled for now.

Change

Compare the timestamp with the biggest timestamp in transitions. If timestamp > self.transitions.last().unwrap().timestamp, we can avoid the binary search.

Performance change

In biref, the performance of instant_to_civil_datetime_static-Asia/Shanghai is improved 2 times via cthe urrent PR and #104

The performance of instant_to_civil_datetime_static-Asia/Shanghai :

# before this PR and https://github.com/BurntSushi/jiff/pull/104(master branch)
group                                                base/chrono-tzfile/                    base/chrono/                           base/jiff/
-----                                                -------------------                    ------------                           ----------
instant_to_civil_datetime_static-Asia/Shanghai       1.00     34.7±0.51ns        ? ?/sec    1.08     37.4±0.62ns        ? ?/sec    2.87     99.3±1.35ns        ? ?/sec

# with https://github.com/BurntSushi/jiff/pull/104
group                                                base/chrono-tzfile/                    base/chrono/                           base/jiff/
-----                                                -------------------                    ------------                           ----------
instant_to_civil_datetime_static-Asia/Shanghai       1.00     34.5±0.98ns        ? ?/sec    1.10     38.1±2.72ns        ? ?/sec    1.98     68.4±5.85ns        ? ?/sec


# https://github.com/BurntSushi/jiff/pull/104 with current PR
group                                                base/chrono-tzfile/                    base/chrono/                           base/jiff/
-----                                                -------------------                    ------------                           ----------
instant_to_civil_datetime_static-Asia/Shanghai       1.00     34.6±1.09ns        ? ?/sec    1.15     39.7±6.03ns        ? ?/sec    1.45     50.1±1.02ns        ? ?/sec

Copy link
Owner

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I buy it!

@BurntSushi BurntSushi merged commit 529105e into BurntSushi:master Aug 18, 2024
17 checks passed
@1996fanrui 1996fanrui deleted the dev/zoned_optimization_avoid_binary_search branch August 19, 2024 01:21
Copy link
Contributor Author

@1996fanrui 1996fanrui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @BurntSushi for the quick review and merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants