Optimise sidereal_course performance

Avoid calling into sgTimeGMT (which calls into the C library)
every single update.
This commit is contained in:
James Turner 2021-07-21 22:54:11 +01:00
parent 5f334d3839
commit 877c3a68e6

View File

@ -32,6 +32,7 @@
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <unordered_map>
#ifdef HAVE_SYS_TIME_H #ifdef HAVE_SYS_TIME_H
# include <sys/time.h> // for get/setitimer, gettimeofday, struct timeval # include <sys/time.h> // for get/setitimer, gettimeofday, struct timeval
@ -62,6 +63,9 @@ static const double SIDRATE = 0.9972695677;
// tzContainer stores all the current Timezone control points/ // tzContainer stores all the current Timezone control points/
std::unique_ptr<SGTimeZoneContainer> static_tzContainer; std::unique_ptr<SGTimeZoneContainer> static_tzContainer;
using StartTimeByYearHash = std::unordered_map<int, time_t>;
static StartTimeByYearHash global_yearToGMTHash;
void SGTime::init( const SGGeod& location, const SGPath& root, time_t init_time ) void SGTime::init( const SGGeod& location, const SGPath& root, time_t init_time )
{ {
gst_diff = -9999.0; gst_diff = -9999.0;
@ -143,11 +147,18 @@ static double sidereal_precise( double mjd, double lng )
// return a courser but cheaper estimate of sidereal time // return a courser but cheaper estimate of sidereal time
static double sidereal_course( time_t cur_time, const struct tm *gmt, double lng ) static double sidereal_course( time_t cur_time, const struct tm *gmt, double lng )
{ {
time_t start_gmt, now; time_t now;
double diff, part, days, hours, lstTmp; double diff, part, days, hours, lstTmp;
now = cur_time; now = cur_time;
// sgTimeGetGMT is expensive when called frequently, so cache the
// result.
time_t start_gmt = global_yearToGMTHash[gmt->tm_year];
if (start_gmt == 0) {
start_gmt = sgTimeGetGMT(gmt->tm_year, 2, 21, 12, 0, 0); start_gmt = sgTimeGetGMT(gmt->tm_year, 2, 21, 12, 0, 0);
global_yearToGMTHash[gmt->tm_year] = start_gmt;
}
diff = (now - start_gmt) / (3600.0 * 24.0); diff = (now - start_gmt) / (3600.0 * 24.0);