Skip to content

Commit 38e00f3

Browse files
luisleo526claude
andcommitted
TradeLedger: a sink run keeps the read cache at a fixed 128 rows
PR #496's kernel leg failed test_selected_metrics_l17b (sink heap-flatness witness, 12 checks): the ledger read cache was allocated by the consumer's own closed-row reads and grew with the closed-trade count to 65,536 x 312 = 20,447,232 B in sink runs, against a budget of 2 x kScratchWindowBytes with large <= small + window. Owner rule: the witness is not loosened. At run begin the consumer now sets the ledger cache budget from the run mode: TradeLedger::kSinkCacheRows = 128 rows (128 x 312 = 39,936 B, under one 64 KiB scratch window, independent of the trade count) when a report sink is installed, the default 65,536 rows otherwise (sink-off runs, which is how the verifier and the sweep run, are unchanged). set_cache_rows is called on a freshly cleared ledger, so it cannot hit the frozen refusal, and it runs every begin so a reused handle never keeps a stale sink budget. Arithmetic against the witness: other working storage 65,592..65,928 B (window plus 56..392) + 39,936 = at most 105,864 <= 131,072 (2 windows); small (1k trades) and large (100k trades) both allocate exactly 128 x 312, so large - small = 0 <= window. A static_assert in test_p2cx_tledger_budget pins kSinkCacheRows x kLedgerEntryBytes <= 64 KiB. Mutations: the sink branch using the default budget (l17b red); every run using the sink budget (the hungpixi-shaped I/O test red). Based on ef4c628 (nothrow operator new fix). Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent ef4c628 commit 38e00f3

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

‎include/pineforge/trade_ledger.hpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class TradeLedger {
4747
kRecordBytes + 2 * sizeof(size_type) + kCacheStringBytes;
4848
static constexpr size_type kDefaultCacheRows = 65536;
4949
static constexpr size_type kMaxCacheRows = 1048576;
50+
// A run with a report sink installed keeps the cache inside the sink's
51+
// fixed heap window: 128 x 312 = 39,936 B, under one 64 KiB scratch window.
52+
static constexpr size_type kSinkCacheRows = 128;
5053
using Visitor = std::function<bool(size_type, const Trade&)>;
5154

5255
class const_iterator {

‎src/native_execution_consumer.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3166,6 +3166,11 @@ bool NativeExecutionConsumer::begin_ready(BacktestEngine& engine, NativeRunPhase
31663166
closed_row_prefix_.clear();
31673167
}
31683168
engine.trades_.set_scratch_directory(engine.report_sink_state_.scratch_dir);
3169+
// The ledger read cache is bounded by the run's memory contract: a sink run
3170+
// keeps it at a fixed size independent of the trade count (the sink
3171+
// heap-flatness witness); other runs use the default budget.
3172+
engine.trades_.set_cache_rows(engine.report_sink_state_.enabled
3173+
? TradeLedger::kSinkCacheRows : TradeLedger::kDefaultCacheRows);
31693174
precommit_digest_.reset();
31703175
margin_liquidation_.reset();
31713176
has_margin_path_ = false;

‎tests/test_p2cx_tledger_budget.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ namespace {
88
using namespace p2cx_tledger;
99
using Size = TradeLedger::size_type;
1010

11+
// A sink run keeps the read cache inside one report_sink::kScratchWindowBytes
12+
// (64 KiB, src/report_scratch.hpp): the sink heap-flatness witness
13+
// (test_selected_metrics_l17b) allows 2 windows for ALL working storage, of
14+
// which the sink's own window takes one. 128 x 312 = 39,936 <= 65,536.
15+
static_assert(TradeLedger::kSinkCacheRows * TradeLedger::kLedgerEntryBytes <= 64U * 1024U,
16+
"sink-run ledger cache exceeds one scratch window");
17+
static_assert(TradeLedger::kSinkCacheRows >= TradeLedger::kResidentRows
18+
&& TradeLedger::kSinkCacheRows <= TradeLedger::kDefaultCacheRows,
19+
"sink-run ledger cache rows outside the budget range");
20+
1121
void bounded(const TradeLedger& ledger) {
1222
CHECK(ledger.cache_size() <= ledger.cache_rows());
1323
CHECK(ledger.cache_resident_bytes()

0 commit comments

Comments
 (0)