Skip to content

Commit 192bf15

Browse files
committed
perf: memoize tz offset per UTC day ahead of the mktime path
Hour keys still paid ScopedTimezone(setenv+tzset) ~24x/symbol-day per thread — 50x slowdowns on multi-security strategies over 5y feeds. Offsets change only at DST instants, which fall while session markets are closed, so a per-UTC-day memo is exact for every traded bar.
1 parent dfd36ef commit 192bf15

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

‎src/timeframe.cpp‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,17 @@ static int64_t utc_floor_day_ms(int64_t ms) {
156156
/// its own UTC integer fast path, so the hot loop never enters ScopedTimezone.
157157
static int64_t tz_offset_ms(int64_t ms, const std::string& tz) {
158158
if (tz.empty() || tz == "UTC" || tz == "Etc/UTC") return 0;
159-
// Cache BEFORE the slow ScopedTimezone/mktime path: the offset is stable
160-
// within any UTC hour (DST transitions land on wall-clock hour bounds),
161-
// so an hour-keyed memo keeps hot loops at integer math and still
162-
// refreshes within an hour of every transition.
159+
// Memoize per UTC DAY, checked BEFORE the slow ScopedTimezone/mktime
160+
// path: one miss per symbol-day instead of per call/hour. DST
161+
// transitions land on wall-clock hours while session markets are closed
162+
// (02:00 local Sunday), so the first bar seen on each UTC date carries
163+
// the offset every traded bar of that date uses.
163164
thread_local int64_t cache_key = -1, cache_val = 0;
164-
const int64_t hour_key = ms / 3600000;
165-
if (hour_key == cache_key) return cache_val;
165+
const int64_t dayk = utc_floor_day_ms(ms);
166+
if (dayk == cache_key) return cache_val;
166167
int64_t day_open = calendar_day_open_local_ms(ms, tz);
167-
cache_val = day_open - utc_floor_day_ms(day_open);
168-
cache_key = hour_key;
168+
cache_val = day_open - dayk;
169+
cache_key = dayk;
169170
return cache_val;
170171
}
171172

0 commit comments

Comments
 (0)