Skip to content

Commit 52292db

Browse files
authored
Session clock: read each day's occurrences once per bar; time() arguments resolved without copies (#339)
## What `time(tf, session, tz)` with a script's own session argument reads TradingView's session clock outside UTC (#334). That path asked the per-day session cache about nine times a bar, and copied the session and zone strings on every `time()` / `time_close()` call. This PR reads each day's occurrences once per call, caches `utc_zone` per session-cache entry, and resolves the call's strings by reference. It changes one file, `src/session_time.cpp`: the language calendar functions under rule 1 of ADR 0001. It adds no switch, input, API or hashed state. ## Value identity (on this tree, against main) - the old and new session functions run side by side over the whole suite: 0 mismatches; - a differential over generated sessions, zones and timeframes, walking every DST switch of 2024–2026 minute by minute: 16.6 M queries, identical digest; - an independent review repeated it with 33.2 M queries: 0 different answers, identical cache and zone-memo history, clean under ASan + UBSan; - the 30 witness harvests: byte-identical, 0 re-pins; - the whole-suite per-trade dump (all 21 Trade fields at `%.17g`, every state hash): identical apart from rows that differ between two runs of one tree; - all 307 tape and witness executables: byte-identical; - corpus 312/312, 0 moved. ## Timing (on this tree, against main 44eab7b) Measured on a Xeon 8375C with 4 independent builds per tree, paired rotating blocks (81 per shape), and every process under exactly 22 environment variables. Positive means slower; `*` means the 95% interval covers 0. The **padded control** is this commit with every function outside `session_time.o` placed at main's address, checked symbol by symbol. | | native | ta-sma | bracket | udt | composite-vcp | |---|---|---|---|---|---| | batch median, ungathered (1 m / 1 m) | +1.23 | +1.26 | +0.87 | +0.83 | **−9.54** | | — padded control | +0.03* | +0.06* | +0.02* | −0.00* | **−7.93** | | per call, ungathered | +0.21 | +0.04* | +0.19 | +0.10 | **−5.16** | | — padded control | +0.05* | +0.01* | −0.02* | −0.02* | **−4.84** | | stream p95, ungathered | +0.46 | +1.53 | +0.46 | +0.17 | **−3.07** | | — padded control | +0.04* | +0.03* | −0.05* | +0.01* | **−3.46** | | batch median, gathered (1 m / 3 m) | +0.60 | +0.09* | +0.54 | +0.41 | **−5.80** | | — padded control | +0.06* | +0.06* | +0.05* | +0.09* | **−5.25** | - **Only composite-vcp's work changes.** Its instruction count drops from 232.5 M to 220.1 M over 2 × 5,000 stream pushes, and from 2,512 to 1,275 per `timeframe_time` call. - **The other four cases execute identical instructions.** Their differences against main are code placement: this file shrinks by 263 bytes and moves the hot functions by 192–256 bytes. With the layout held fixed (padded control), all four read 0 within noise on every metric. The commit message's table was measured with #336's tree as the base; the table above is this tree. ## Verification - Engine verify bundles on this exact tree: release 859, sanitizers 837, debug 840, kernel 307 with 0 boundary findings, parity 312/312, subset, docs. - One parity sweep of the full population on this head, paired with codegen main, is neutral: 7,989 comparable, no moves. - An independent review returned MERGE; its message fixes are applied. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
1 parent 44eab7b commit 52292db

1 file changed

Lines changed: 92 additions & 47 deletions

File tree

‎src/session_time.cpp‎

Lines changed: 92 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,9 @@ struct ArgCache {
947947
std::string session;
948948
std::string tz;
949949
bool ok = false;
950+
// utc_zone(tz), once asked (arg_cache_utc).
951+
bool utc_known = false;
952+
bool utc = false;
950953
ArgSession parsed;
951954
Day days[kDays];
952955
uint64_t used_at = 0;
@@ -1247,6 +1250,27 @@ const std::vector<WallOccurrence>& arg_day_occurrences(ArgCache& cache, int64_t
12471250
return kept.occurrences;
12481251
}
12491252

1253+
// The occurrences of session day `day` when its ring slot already holds them
1254+
// (arg_day_occurrences then returns them and changes nothing), else null.
1255+
const std::vector<WallOccurrence>* resolved_occurrences(const ArgCache& cache, int64_t day) {
1256+
const ArgCache::Day& slot =
1257+
cache.days[static_cast<std::size_t>(((day % ArgCache::kDays) + ArgCache::kDays)
1258+
% ArgCache::kDays)];
1259+
return slot.used && slot.day == day && slot.occurrences_done ? &slot.occurrences : nullptr;
1260+
}
1261+
1262+
// utc_zone(cache.tz), asked once per entry: the answer depends on the zone's
1263+
// spelling alone, as every memo slot holding a spelling records
1264+
// normalize_timezone_for_posix's reading of it. Asked where the reading
1265+
// first needs it, so a spelling that throws still throws there.
1266+
bool arg_cache_utc(ArgCache& cache) {
1267+
if (!cache.utc_known) {
1268+
cache.utc = utc_zone(cache.tz);
1269+
cache.utc_known = true;
1270+
}
1271+
return cache.utc;
1272+
}
1273+
12501274
// session_argument_intraday_bar on the session clock (the rule above): the
12511275
// latest occurrence that holds `bar_ms` -- by session day, then by open, as
12521276
// the plain reading picks -- cut into tf bars from its first instant.
@@ -1256,6 +1280,20 @@ bool clock_intraday_bar(int64_t bar_ms, ArgCache& cache, int64_t tf_ms, int64_t&
12561280
decompose_ms_local(bar_ms, cache.tz, local);
12571281
const int64_t today = days_from_civil(local.tm_year + 1900, local.tm_mon + 1, local.tm_mday);
12581282
const int64_t wall = bar_ms + static_cast<int64_t>(local.tm_gmtoff) * 1000;
1283+
// The scans below read days today - 5 .. today + 2. Each day's occurrences
1284+
// are asked of the cache at its first read, in the order the scans reach
1285+
// it; a later read is the same vector, since resolving a day touches only
1286+
// the days next to it and no two days within ten share a ring slot.
1287+
const std::vector<WallOccurrence>* asked[8] = {};
1288+
const auto occurrences = [&](int64_t d) -> const std::vector<WallOccurrence>& {
1289+
const std::vector<WallOccurrence>*& entry = asked[d - (today - 5)];
1290+
if (entry == nullptr) {
1291+
entry = resolved_occurrences(cache, d);
1292+
if (entry == nullptr)
1293+
entry = &arg_day_occurrences(cache, d);
1294+
}
1295+
return *entry;
1296+
};
12591297
bool found = false;
12601298
int64_t best_day = 0;
12611299
int64_t best_start = 0;
@@ -1265,11 +1303,11 @@ bool clock_intraday_bar(int64_t bar_ms, ArgCache& cache, int64_t tf_ms, int64_t&
12651303
int64_t day = 0;
12661304
int64_t start = 0;
12671305
int64_t end = 0;
1268-
if (!arg_day_occurrences(cache, today)[k].clock) {
1306+
if (!occurrences(today)[k].clock) {
12691307
// A window the clock does not read: its plain instances, as the
12701308
// plain reading scans them (one spelled past a day reaches back).
12711309
for (int64_t d = today + 1; d >= today - 5 && !held; --d) {
1272-
const WallOccurrence& o = arg_day_occurrences(cache, d)[k];
1310+
const WallOccurrence& o = occurrences(d)[k];
12731311
if (o.traded && o.end_ms > o.open_ms && o.open_ms <= bar_ms && bar_ms < o.end_ms) {
12741312
held = true;
12751313
day = d;
@@ -1281,10 +1319,10 @@ bool clock_intraday_bar(int64_t bar_ms, ArgCache& cache, int64_t tf_ms, int64_t&
12811319
// Rule 2: a deviating occurrence owns (previous end, own end].
12821320
bool owned = false;
12831321
for (int64_t d = today - 1; d <= today + 2 && !owned; ++d) {
1284-
const WallOccurrence& o = arg_day_occurrences(cache, d)[k];
1322+
const WallOccurrence& o = occurrences(d)[k];
12851323
if (!o.traded || !o.deviates || bar_ms > o.end_ms)
12861324
continue;
1287-
if (arg_day_occurrences(cache, d - 1)[k].end_ms >= bar_ms)
1325+
if (occurrences(d - 1)[k].end_ms >= bar_ms)
12881326
continue;
12891327
owned = true;
12901328
const int64_t post_wall = bar_ms + static_cast<int64_t>(o.post_s) * 1000;
@@ -1304,7 +1342,7 @@ bool clock_intraday_bar(int64_t bar_ms, ArgCache& cache, int64_t tf_ms, int64_t&
13041342
}
13051343
// Rule 3: the other occurrences.
13061344
for (int64_t d = today + 2; d >= today - 2 && !owned && !held; --d) {
1307-
const WallOccurrence& o = arg_day_occurrences(cache, d)[k];
1345+
const WallOccurrence& o = occurrences(d)[k];
13081346
if (!o.traded || o.deviates)
13091347
continue;
13101348
const bool in = o.overnight
@@ -1346,7 +1384,7 @@ bool session_argument_intraday_bar(int64_t bar_ms, const std::string& session,
13461384
if (tf_ms <= 0 || !cache.ok)
13471385
return false;
13481386
if (transition_clock && detail::session_clock_switches().transition_wall_clock
1349-
&& !utc_zone(tz))
1387+
&& !arg_cache_utc(cache))
13501388
return clock_intraday_bar(bar_ms, cache, tf_ms, open_ms, close_ms);
13511389
struct tm local {};
13521390
decompose_ms_local(bar_ms, tz, local);
@@ -1688,18 +1726,18 @@ static bool session_arg_is_timezone(const std::string& s) {
16881726
//
16891727
// Two distinct timezones come out of this:
16901728
//
1691-
// session_tz_out — the zone the SESSION window ("0930-1600") is read in.
1729+
// session_tz — the zone the SESSION window ("0930-1600") is read in.
16921730
// Pine: "To interpret the time zone of the specified session, time() and
16931731
// time_close() use the time zone of the exchange by default, unless a
16941732
// timezone argument is specified" (i.e. the default is syminfo.timezone).
16951733
// So an explicit tz argument wins; otherwise the caller-supplied
16961734
// syminfo_tz (codegen passes ``syminfo_.timezone``); otherwise UTC (the
16971735
// historical engine default, still what an unknown/empty syminfo yields).
16981736
//
1699-
// tf_tz_out — the zone the five-argument forms' D/W/M calendar floor is
1737+
// tf_tz — the zone the five-argument forms' D/W/M calendar floor is
17001738
// computed in when there is no session argument: explicit tz argument,
17011739
// else UTC. A valid session argument's D/W/M period is the session's own
1702-
// bar, in session_tz_out (session_argument_bar); intraday timeframes sit
1740+
// bar, in session_tz (session_argument_bar); intraday timeframes sit
17031741
// on the symbol's own HTF grid (the seven-argument forms) or the epoch
17041742
// grid (these) and never depend on tz.
17051743
//
@@ -1708,24 +1746,31 @@ static bool session_arg_is_timezone(const std::string& s) {
17081746
// session (no filter) but leave tz at the chart/UTC default — do NOT adopt the
17091747
// string as the timezone. The daily boundary then rolls at the chart/exchange
17101748
// (UTC) timezone, matching TradingView.
1711-
static void resolve_session_tz(const std::string& session,
1712-
const std::string& tz_in,
1713-
const std::string& syminfo_tz,
1714-
std::string& sess_out,
1715-
std::string& session_tz_out,
1716-
std::string& tf_tz_out) {
1717-
sess_out = session;
1718-
tf_tz_out = tz_in;
1719-
if (tf_tz_out.empty() && session_arg_is_timezone(sess_out)) {
1720-
sess_out.clear();
1721-
}
1722-
if (tf_tz_out.empty())
1723-
tf_tz_out = "UTC";
1724-
session_tz_out = tz_in;
1725-
if (session_tz_out.empty())
1726-
session_tz_out = syminfo_tz;
1727-
if (session_tz_out.empty())
1728-
session_tz_out = "UTC";
1749+
//
1750+
// Each of the three is one of the call's own strings or a constant, named by
1751+
// reference, so a call (once per bar per site) copies none.
1752+
struct SessionTzTriple {
1753+
const std::string& session;
1754+
const std::string& session_tz;
1755+
const std::string& tf_tz;
1756+
};
1757+
1758+
static SessionTzTriple resolve_session_tz(const std::string& session,
1759+
const std::string& tz_in,
1760+
const std::string& syminfo_tz) {
1761+
static const std::string kNoSession;
1762+
static const std::string kUtc = "UTC";
1763+
const bool explicit_tz = !tz_in.empty();
1764+
return {!explicit_tz && session_arg_is_timezone(session) ? kNoSession : session,
1765+
explicit_tz ? tz_in : !syminfo_tz.empty() ? syminfo_tz : kUtc,
1766+
explicit_tz ? tz_in : kUtc};
1767+
}
1768+
1769+
// The timeframe a time()/time_close() call reads: its own, else the chart's,
1770+
// else "1".
1771+
static const std::string& call_timeframe(const std::string& tf_in, const std::string& chart_tf) {
1772+
static const std::string kOneMinute = "1";
1773+
return !tf_in.empty() ? tf_in : !chart_tf.empty() ? chart_tf : kOneMinute;
17291774
}
17301775

17311776
// time() / time_close() of a valid session argument: the session's own bar of
@@ -1760,12 +1805,12 @@ int64_t timeframe_time(int64_t bar_ms,
17601805
const std::string& tz_in,
17611806
const std::string& chart_tf,
17621807
const std::string& syminfo_tz) {
1763-
std::string tf = tf_in.empty() ? chart_tf : tf_in;
1764-
if (tf.empty())
1765-
tf = "1";
1808+
const std::string& tf = call_timeframe(tf_in, chart_tf);
17661809

1767-
std::string sess, session_tz, tf_tz;
1768-
resolve_session_tz(session, tz_in, syminfo_tz, sess, session_tz, tf_tz);
1810+
const SessionTzTriple resolved = resolve_session_tz(session, tz_in, syminfo_tz);
1811+
const std::string& sess = resolved.session;
1812+
const std::string& session_tz = resolved.session_tz;
1813+
const std::string& tf_tz = resolved.tf_tz;
17691814

17701815
int64_t value = 0;
17711816
if (!sess.empty() && session_argument_time(bar_ms, tf, sess, session_tz, chart_tf, false, value))
@@ -1783,12 +1828,12 @@ static int64_t session_time_close_reading(int64_t bar_ms,
17831828
const std::string& chart_tf,
17841829
const std::string& syminfo_tz,
17851830
bool transition_clock) {
1786-
std::string tf = tf_in.empty() ? chart_tf : tf_in;
1787-
if (tf.empty())
1788-
tf = "1";
1831+
const std::string& tf = call_timeframe(tf_in, chart_tf);
17891832

1790-
std::string sess, session_tz, tf_tz;
1791-
resolve_session_tz(session, tz_in, syminfo_tz, sess, session_tz, tf_tz);
1833+
const SessionTzTriple resolved = resolve_session_tz(session, tz_in, syminfo_tz);
1834+
const std::string& sess = resolved.session;
1835+
const std::string& session_tz = resolved.session_tz;
1836+
const std::string& tf_tz = resolved.tf_tz;
17921837

17931838
int64_t value = 0;
17941839
if (!sess.empty() && session_argument_time(bar_ms, tf, sess, session_tz, chart_tf, true, value,
@@ -2003,16 +2048,16 @@ int64_t timeframe_time(int64_t bar_ms,
20032048
const std::string& chart_tf,
20042049
const std::string& sym_tz,
20052050
const std::string& sym_session) {
2006-
std::string tf = tf_in.empty() ? chart_tf : tf_in;
2007-
if (tf.empty())
2008-
tf = "1";
2051+
const std::string& tf = call_timeframe(tf_in, chart_tf);
20092052

20102053
// Composition with the syminfo session-tz default: the session window
20112054
// is read in the explicit tz, else syminfo.timezone (sym_tz), else UTC;
20122055
// a VALID session under a D/W/M tf is the session's own bar in that
20132056
// zone, while no valid session takes the symbol's own D/W/M bar.
2014-
std::string sess, session_tz, tf_tz;
2015-
resolve_session_tz(session, tz_in, sym_tz, sess, session_tz, tf_tz);
2057+
const SessionTzTriple resolved = resolve_session_tz(session, tz_in, sym_tz);
2058+
const std::string& sess = resolved.session;
2059+
const std::string& session_tz = resolved.session_tz;
2060+
const std::string& tf_tz = resolved.tf_tz;
20162061

20172062
int64_t value = 0;
20182063
if (!sess.empty() && session_argument_time(bar_ms, tf, sess, session_tz, chart_tf, false, value))
@@ -2046,12 +2091,12 @@ int64_t timeframe_time_close(int64_t bar_ms,
20462091
const std::string& chart_tf,
20472092
const std::string& sym_tz,
20482093
const std::string& sym_session) {
2049-
std::string tf = tf_in.empty() ? chart_tf : tf_in;
2050-
if (tf.empty())
2051-
tf = "1";
2094+
const std::string& tf = call_timeframe(tf_in, chart_tf);
20522095

2053-
std::string sess, session_tz, tf_tz;
2054-
resolve_session_tz(session, tz_in, sym_tz, sess, session_tz, tf_tz);
2096+
const SessionTzTriple resolved = resolve_session_tz(session, tz_in, sym_tz);
2097+
const std::string& sess = resolved.session;
2098+
const std::string& session_tz = resolved.session_tz;
2099+
const std::string& tf_tz = resolved.tf_tz;
20552100

20562101
int64_t value = 0;
20572102
if (!sess.empty() && session_argument_time(bar_ms, tf, sess, session_tz, chart_tf, true, value))

0 commit comments

Comments
 (0)