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

Perf: Cache ts offset guesses for quickDT #1579

Conversation

schleyfox
Copy link
Contributor

This is part of a series of PRs based on performance work we have done to
improve a use-case involving parsing/formatting hundreds of thousands of dates
where luxon was the bottleneck.

This includes the commit from #1574 to establish the benchmark

Previously we guessed by calling zone.offset(tsNow) on every
construction of a date from date parts. This caches that result instead,
saving a call to offset. The tests verify that this works across DST boundaries when the cache holds an offset for the previous DST state from now.

For processes that live across a DST boundary and mostly handle current date times, this will mean the guess is wrong and we'll have to re-guess. Since we're caching one call to offset, this should be performance neutral to current behavior. Otherwise, we save a call.

Benchmark Comparison (name | before | after | after/before):

DateTime.local with numbers and zone | 50,913 ±0.18% | 66,168 ±0.25% | 1.3x

Copy link

linux-foundation-easycla bot commented Jan 22, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@schleyfox schleyfox marked this pull request as ready for review January 22, 2024 16:50
@schleyfox
Copy link
Contributor Author

/easycla

src/datetime.js Outdated
function guessOffsetForZone(zone) {
if (!DateTime._zoneOffsetGuessCache[zone]) {
if (DateTime._zoneOffsetTs === undefined) {
DateTime._zoneOffsetTs = Settings.now();
Copy link
Member

Choose a reason for hiding this comment

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

Should support clearing the cache here

expect(d.hour).toBe(1);
expect(d.offset).toBe(-4 * 60);

DateTime._zoneOffsetGuessCache = {};
Copy link
Member

Choose a reason for hiding this comment

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

You could then use the resetCaches() function in here

@icambron
Copy link
Member

Looks pretty good. Just requested a few changes

@schleyfox
Copy link
Contributor Author

Changed, thanks!

@icambron
Copy link
Member

icambron commented Mar 9, 2024

@schleyfox if you fix the conflicts, I'll merge this

Previously we guessed by calling zone.offset(tsNow) on every
construction of a date from date parts. This caches that result instead,
saving a call to offset
@schleyfox schleyfox force-pushed the benjamin--integ-cache-offset-guesses-for-quickDT branch from a479dfe to f7d747d Compare March 9, 2024 21:39
@schleyfox
Copy link
Contributor Author

@icambron conflicts fixed. thanks.

@icambron icambron merged commit cafc4ee into moment:master Mar 12, 2024
2 checks passed
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