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

Thread through caching in chinese based calendars #4411

Merged
merged 16 commits into from
Dec 13, 2023

Conversation

Manishearth
Copy link
Member

@Manishearth Manishearth commented Dec 6, 2023

Progress on #3933

Builds on #4407.

This only caches the yearinfo, it does not yet load data from data sources. That may happen in a separate PR.

@Manishearth
Copy link
Member Author

fyi @sffc . The bulk of the PR should still be reviewable, if the other PR lands feel free to start reviewing just tell me you're doing so.

@Manishearth Manishearth force-pushed the chinese-yearinfo branch 3 times, most recently from 24fb99b to f823da1 Compare December 6, 2023 19:18
@Manishearth Manishearth marked this pull request as ready for review December 6, 2023 19:18
@Manishearth Manishearth requested review from sffc and a team as code owners December 6, 2023 19:18
@Manishearth
Copy link
Member Author

Manishearth commented Dec 6, 2023

Tests pass!

@sffc this is ready for review, you can review the other PR first if you'd like, or everything at once

@@ -304,11 +297,13 @@ impl Calendar for Chinese {
fn day_of_year_info(&self, date: &Self::DateInner) -> types::DayOfYearInfo {
let prev_year = date.0 .0.year.saturating_sub(1);
let next_year = date.0 .0.year.saturating_add(1);
// TODO(#3933): we should maybe have this cached already
let prev_info = self.get_precomputed_data().load_or_compute_info(prev_year);
Copy link
Member

Choose a reason for hiding this comment

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

This is not a super hot code path (week of year) so maybe fine if it is a little slower

Copy link
Member Author

Choose a reason for hiding this comment

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

(result from discussion: this is a hot code path due to ExtractedDateTimeInfo. we could change how that works if necessary, but it's a nontrivial refactor)

ret[12] = next_new_year - 1; // last day of last month is the day before the next new year

let mut current_month_start = new_year;
let mut current_month_major_solar_term = major_solar_term_from_fixed::<C>(new_year);
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I prefer not introducing new code that uses the astronomy functions directly. Prefer to use abstractions we already have such as days_in_month.

Copy link
Member Author

@Manishearth Manishearth Dec 8, 2023

Choose a reason for hiding this comment

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

That recomputes a lot of things. I think this is the best way to do this, and honestly easier to understand when debugging.

Copy link
Member

Choose a reason for hiding this comment

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

ok, not super comfortable reviewing astronomy code that isn't a direct port from lisp but thanks for writing the unit tests below

components/calendar/src/chinese_based.rs Show resolved Hide resolved
@Manishearth Manishearth requested a review from sffc December 9, 2023 00:00
ret[12] = next_new_year - 1; // last day of last month is the day before the next new year

let mut current_month_start = new_year;
let mut current_month_major_solar_term = major_solar_term_from_fixed::<C>(new_year);
Copy link
Member

Choose a reason for hiding this comment

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

ok, not super comfortable reviewing astronomy code that isn't a direct port from lisp but thanks for writing the unit tests below

let mut current_month_major_solar_term = major_solar_term_from_fixed::<C>(new_year);
let mut leap_month_index = None;
for i in 0u8..12 {
let next_month_start = new_moon_on_or_after::<C>((current_month_start + 28).as_moment());
Copy link
Member

Choose a reason for hiding this comment

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

Question: does 28 work? I remember Reingold using various constants in different contexts

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, since on-or-after starts at midnight, so the potential off-by-one error is in the other direction in our favor. We know that next_month_start must be current_month_start + 29 or 30.

@Manishearth Manishearth merged commit df7a7ef into unicode-org:main Dec 13, 2023
28 checks passed
@Manishearth Manishearth deleted the chinese-yearinfo branch December 13, 2023 19:32
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