-
Notifications
You must be signed in to change notification settings - Fork 174
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
Optimize chinese_based
and astronomy
#3687
Comments
cc @eggrobin, our resident expert in practical implementations of moon math |
I don't think this is as much of an issue for |
My first guess would be to unwind the solar longitude, at which point you have reduced the problem to root finding on a monotonic function; at which point they're good algorithms for finding a zero of a function Brent. Conveniently, the solar longitude is appears to be computed unwound using some sort of Poisson series, and then reduced to [0°, 360°[, so unwinding is trivial: icu4x/components/calendar/src/astronomy.rs Line 1103 in 553f49d
I am out at RAI 68 next week and at UTC 176 the week after that, so it will be a while before I get back to this. |
Also investigate whether we can use a more efficient numerical algorithm like gradient descent. |
That is what I meant by
|
I added Chinese to the benchmark tests for creating new dates in different calendars; here are the results:
While all the other listed calendars can be created on the order of hundreds of nanoseconds or of microseconds, Chinese performs much worse with 12.466 ms. This is obviously relatively very slow, but is it slow enough to be an issue? I don't have very good context to whether this speed requires optimization or not. I think the main reason for this particular function being so much slower is the need to identify whether the current year is a leap year, something which is much harder to do in Chinese than in other calendars simply by the nature of the calendar, and I'm not sure (very genuinely not sure, I will investigate further) how much of that can be optimized versus just being a result of its inherent complexity. |
12 ms is definitely on the scale where we should be worried. The line I like to draw is that we need to render a spreadsheet of data at 120 fps (frames per second). For dates and times, let's say that we want a ballpark of 1000 dates formatted at 120 fps, which gives us about 8 µs per item. Let's see how close we can get to that. |
Also: in my opinion, the critical path operation should be converting from ISO to the calendar and then loading the formattable pieces from it (FormattableYear, etc). |
So just to be clear, creating an ISO date -> converting to a calendar -> getting the FormattableYear/Month and day via |
Yep. |
I misread the function, the table of values is actually for:
So it's doing pretty much the complete path and some more. |
Ah, I would have a separate benchmark for creation vs arithmetic vs getting formatting data |
I suspect the DateDuration math is the really slow part and we have a path to optimize it |
I used the debugger to trace through functions like |
I ran the function
|
Similar table for
|
I think I misinterpreted what these functions were doing in their searches before, and it seems like Reingold's approximations used in these functions are pretty good. However, |
chinese_based
and astronomy
chinese_based
and astronomy
There are many places in
chinese_based
andastronomy
which involve searching for the next of a given astronomical phenomenon. For example,fn winter_solstice_on_or_before
inchinese_based
, with the goal of finding the first winter solstice on or before a givenRataDie
, searches for the date near the givenRataDie
at which the solar longitude first exceeds 270 degrees. This is done with a while loop which goes through each day and compares the solar longitude to 270 degrees.There are several similar functions which search for astronomical events in this way; most of these are based off Calendrical Calculations functions which do this, however @echeran mentioned that the book's authors make clear these algorithms are not optimized. To improve the efficiency of lunar calendars now being added to icu_calendar, we should examine and optimize these functions where possible.
This is separate to the possible optimizations we could make by caching certain information as part of
ArithmeticDate
orChineseBasedDateInner
/ChineseDateInner
.@sotam1069 @echeran @sffc @Manishearth
The text was updated successfully, but these errors were encountered: