|
4 | 4 | #include <pineforge/source/pine_strategy_host.hpp> |
5 | 5 | #include <pineforge/compat/pine/market_admission.hpp> |
6 | 6 | #include <pineforge/pending_order_mirror.hpp> |
7 | | - |
| 7 | +#include "pine_cohort_exposure.hpp" |
8 | 8 | #include <pineforge/native_calendar.hpp> |
9 | 9 | #include <pineforge/timeframe.hpp> |
10 | 10 |
|
@@ -5362,37 +5362,37 @@ double PineExecutionAdapter::cohort_exposure_for(const SourceId& id) const noexc |
5362 | 5362 | } |
5363 | 5363 |
|
5364 | 5364 | double PineExecutionAdapter::cohort_exposure_for(const CohortFacts& facts) const noexcept { |
5365 | | - double total = 0.0; |
5366 | | - // `live_units_by_origin` is a lookup table. Quantities must follow the |
5367 | | - // cohort's deterministic source insertion order, never its hash buckets. |
5368 | | - // Only roster positions whose incarnation has a unit row can contribute: |
5369 | | - // step through just those, ascending, rather than the id's whole entry |
5370 | | - // history. Allocation-free (the pending-order readback calls this). |
5371 | | - bool have_previous = false; |
5372 | | - std::size_t previous = 0; |
5373 | | - for (;;) { |
5374 | | - bool have_next = false; |
5375 | | - std::size_t next = 0; |
5376 | | - double units = 0.0; |
5377 | | - for (const auto& unit : facts.live_units_by_origin) { |
5378 | | - const auto* positions = facts.origins.positions_of(unit.first); |
5379 | | - if (!positions) continue; |
5380 | | - for (const auto position : *positions) { |
5381 | | - if (have_previous && position <= previous) continue; |
5382 | | - if (!have_next || position < next) { |
5383 | | - have_next = true; |
5384 | | - next = position; |
5385 | | - units = unit.second; |
5386 | | - } |
5387 | | - break; |
5388 | | - } |
5389 | | - } |
5390 | | - if (!have_next) break; |
5391 | | - if (std::isfinite(units) && units > 0.0) total += units; |
5392 | | - have_previous = true; |
5393 | | - previous = next; |
5394 | | - } |
5395 | | - return std::isfinite(total) && total > 0.0 ? total : 0.0; |
| 5365 | + PINEFORGE_LOOKUP_INDEX_WORK("cohort_exposure_call", this, run_counter_, 1); |
| 5366 | + // Quantities follow source insertion order, never the lookup table's |
| 5367 | + // hash buckets. Each retained position contributes separately. |
| 5368 | + // Repeated incarnations contribute once per occurrence, in that order. |
| 5369 | + // Allocation-free: pending-order readback calls this const noexcept member. |
| 5370 | + // Finite/positive filtering and the final clamp preserve overflow to zero. |
| 5371 | + // Reordering, regrouping or multiplying repeated units changes bits. |
| 5372 | + // Diagnostic work belongs to the returned value, never adapter state. |
| 5373 | + // The seam receives the original roster and map by const reference. |
| 5374 | + // Global positions keep their order after prefix retirement. |
| 5375 | + // A unit row without a retained position contributes no term. |
| 5376 | + const auto folded = detail::fold_cohort_exposure(facts.origins, |
| 5377 | + facts.live_units_by_origin); |
| 5378 | + PINEFORGE_LOOKUP_INDEX_WORK("cohort_exposure_step", this, run_counter_, folded.steps); |
| 5379 | + PINEFORGE_LOOKUP_INDEX_WORK("cohort_exposure_window", this, run_counter_, |
| 5380 | + facts.origins.members().size()); |
| 5381 | + PINEFORGE_LOOKUP_INDEX_WORK("cohort_exposure_rows", this, run_counter_, |
| 5382 | + facts.live_units_by_origin.size()); |
| 5383 | + if (folded.path == 2) |
| 5384 | + PINEFORGE_LOOKUP_INDEX_WORK("cohort_exposure_path_b", this, run_counter_, 1); |
| 5385 | + if (folded.path == 3) |
| 5386 | + PINEFORGE_LOOKUP_INDEX_WORK("cohort_exposure_b_abandoned", this, run_counter_, 1); |
| 5387 | +#ifdef PINEFORGE_COHORT_EXPOSURE_FIXTURES |
| 5388 | + detail::verify_cohort_exposure(facts.origins, facts.live_units_by_origin, |
| 5389 | + folded.total); |
| 5390 | +#endif |
| 5391 | + // The fixture oracle is independent of the selected production path. |
| 5392 | + // It checks every call, including both empty-state early returns. |
| 5393 | + // No aggregate or reservation state is updated by this read. |
| 5394 | + // Callers receive only the clamped exposure value. |
| 5395 | + return folded.total; |
5396 | 5396 | } |
5397 | 5397 |
|
5398 | 5398 | // The units of the position an exit's entry (from_entry) holds; none for an |
@@ -30295,3 +30295,159 @@ std::array<std::uint64_t, 3> PineExecutionAdapter::retirement_validation_state() |
30295 | 30295 | } |
30296 | 30296 | #endif |
30297 | 30297 | } // namespace pineforge::source |
| 30298 | + |
| 30299 | +namespace pineforge::source::detail { |
| 30300 | + |
| 30301 | +CohortExposureFold fold_cohort_exposure( |
| 30302 | + const OriginRoster& origins, |
| 30303 | + const std::unordered_map<std::uint64_t, double>& live_units_by_origin) noexcept { |
| 30304 | + if (live_units_by_origin.empty() || origins.members().empty()) return {0.0, 0, 0}; |
| 30305 | + std::uint64_t steps = 0; |
| 30306 | + std::uint8_t path = 1; |
| 30307 | + if (origins.members().size() > 4 * live_units_by_origin.size() + 16) { |
| 30308 | + // Positions are disjoint across incarnations and ascending within |
| 30309 | + // each row. Sorting all occurrences restores the roster's order. |
| 30310 | + struct Term { std::size_t position; double units; }; |
| 30311 | + Term buf[kCohortFoldStack]; |
| 30312 | + std::size_t n = 0; |
| 30313 | + std::size_t mapped = 0; |
| 30314 | + bool abandoned = false; |
| 30315 | + for (const auto& unit : live_units_by_origin) { |
| 30316 | + ++steps; |
| 30317 | + const auto* positions = origins.positions_of(unit.first); |
| 30318 | + if (!positions) continue; |
| 30319 | + for (const auto position : *positions) { |
| 30320 | + ++steps; |
| 30321 | + ++mapped; |
| 30322 | + if (n == kCohortFoldStack) { |
| 30323 | + abandoned = true; |
| 30324 | + continue; |
| 30325 | + } |
| 30326 | + buf[n++] = {position, unit.second}; |
| 30327 | + } |
| 30328 | + } |
| 30329 | + if (!abandoned) { |
| 30330 | + std::sort(buf, buf + n, [](const Term& a, const Term& b) noexcept { |
| 30331 | + return a.position < b.position; |
| 30332 | + }); |
| 30333 | + // Deterministic sort charge: P * ceil(log2 P), zero for P <= 1. |
| 30334 | + std::uint64_t levels = 0; |
| 30335 | + for (auto remaining = n > 0 ? n - 1 : 0; remaining; remaining >>= 1) |
| 30336 | + ++levels; |
| 30337 | + steps += static_cast<std::uint64_t>(n) * levels; |
| 30338 | + double total = 0.0; |
| 30339 | + for (std::size_t i = 0; i < n; ++i) { |
| 30340 | + ++steps; |
| 30341 | + const double units = buf[i].units; |
| 30342 | + if (std::isfinite(units) && units > 0.0) total += units; |
| 30343 | + } |
| 30344 | + return {std::isfinite(total) && total > 0.0 ? total : 0.0, steps, 2}; |
| 30345 | + } |
| 30346 | + // The partial stack gather is discarded. Comparing this charge with |
| 30347 | + // M keeps either overflow completion bounded by the frozen selector. |
| 30348 | + const auto mapped_count = static_cast<std::uint64_t>(mapped); |
| 30349 | + const auto row_count = |
| 30350 | + static_cast<std::uint64_t>(live_units_by_origin.size()); |
| 30351 | + // The selector has P + 1 passes. Each pass can inspect all R rows |
| 30352 | + // and all P positions, including positions in exhausted rows. |
| 30353 | + const auto charge_max = std::numeric_limits<std::uint64_t>::max(); |
| 30354 | + const auto charge_add = [charge_max](std::uint64_t left, |
| 30355 | + std::uint64_t right) noexcept { |
| 30356 | + return right > charge_max - left ? charge_max : left + right; |
| 30357 | + }; |
| 30358 | + const auto charge_mul = [charge_max](std::uint64_t left, |
| 30359 | + std::uint64_t right) noexcept { |
| 30360 | + return left != 0 && right > charge_max / left |
| 30361 | + ? charge_max : left * right; |
| 30362 | + }; |
| 30363 | + const auto pass_count = mapped_count == charge_max |
| 30364 | + ? charge_max : mapped_count + 1; |
| 30365 | + const auto selector_charge = |
| 30366 | + charge_mul(pass_count, charge_add(row_count, mapped_count)); |
| 30367 | + path = 3; |
| 30368 | + if (static_cast<std::uint64_t>(origins.members().size()) >= selector_charge) { |
| 30369 | + // Frozen selector, kept term-for-term so its acceptance-ordered |
| 30370 | + // floating-point additions remain the old fold's additions. |
| 30371 | + double total = 0.0; |
| 30372 | + bool have_previous = false; |
| 30373 | + std::size_t previous = 0; |
| 30374 | + for (;;) { |
| 30375 | + bool have_next = false; |
| 30376 | + std::size_t next = 0; |
| 30377 | + double units = 0.0; |
| 30378 | + for (const auto& unit : live_units_by_origin) { |
| 30379 | + ++steps; |
| 30380 | + const auto* positions = origins.positions_of(unit.first); |
| 30381 | + if (!positions) continue; |
| 30382 | + for (const auto position : *positions) { |
| 30383 | + ++steps; |
| 30384 | + if (have_previous && position <= previous) continue; |
| 30385 | + if (!have_next || position < next) { |
| 30386 | + have_next = true; |
| 30387 | + next = position; |
| 30388 | + units = unit.second; |
| 30389 | + } |
| 30390 | + break; |
| 30391 | + } |
| 30392 | + } |
| 30393 | + if (!have_next) break; |
| 30394 | + if (std::isfinite(units) && units > 0.0) total += units; |
| 30395 | + have_previous = true; |
| 30396 | + previous = next; |
| 30397 | + } |
| 30398 | + return {std::isfinite(total) && total > 0.0 ? total : 0.0, steps, path}; |
| 30399 | + } |
| 30400 | + } |
| 30401 | + double total = 0.0; |
| 30402 | + for (const auto& origin : origins) { |
| 30403 | + ++steps; |
| 30404 | + const auto unit = live_units_by_origin.find(origin.incarnation); |
| 30405 | + if (unit == live_units_by_origin.end()) continue; |
| 30406 | + const double units = unit->second; |
| 30407 | + if (std::isfinite(units) && units > 0.0) total += units; |
| 30408 | + } |
| 30409 | + return {std::isfinite(total) && total > 0.0 ? total : 0.0, steps, path}; |
| 30410 | +} |
| 30411 | + |
| 30412 | +#ifdef PINEFORGE_COHORT_EXPOSURE_FIXTURES |
| 30413 | +void verify_cohort_exposure( |
| 30414 | + const OriginRoster& origins, |
| 30415 | + const std::unordered_map<std::uint64_t, double>& live_units_by_origin, |
| 30416 | + double actual) noexcept { |
| 30417 | + // Frozen selector: deliberately independent of the production fold. |
| 30418 | + double total = 0.0; |
| 30419 | + // `live_units_by_origin` is a lookup table. Quantities must follow the |
| 30420 | + // cohort's deterministic source insertion order, never its hash buckets. |
| 30421 | + // Only roster positions whose incarnation has a unit row can contribute: |
| 30422 | + // step through just those, ascending, rather than the id's whole entry |
| 30423 | + // history. Allocation-free (the pending-order readback calls this). |
| 30424 | + bool have_previous = false; |
| 30425 | + std::size_t previous = 0; |
| 30426 | + for (;;) { |
| 30427 | + bool have_next = false; |
| 30428 | + std::size_t next = 0; |
| 30429 | + double units = 0.0; |
| 30430 | + for (const auto& unit : live_units_by_origin) { |
| 30431 | + const auto* positions = origins.positions_of(unit.first); |
| 30432 | + if (!positions) continue; |
| 30433 | + for (const auto position : *positions) { |
| 30434 | + if (have_previous && position <= previous) continue; |
| 30435 | + if (!have_next || position < next) { |
| 30436 | + have_next = true; |
| 30437 | + next = position; |
| 30438 | + units = unit.second; |
| 30439 | + } |
| 30440 | + break; |
| 30441 | + } |
| 30442 | + } |
| 30443 | + if (!have_next) break; |
| 30444 | + if (std::isfinite(units) && units > 0.0) total += units; |
| 30445 | + have_previous = true; |
| 30446 | + previous = next; |
| 30447 | + } |
| 30448 | + const double expected = std::isfinite(total) && total > 0.0 ? total : 0.0; |
| 30449 | + if (std::memcmp(&actual, &expected, sizeof expected) != 0) std::abort(); |
| 30450 | +} |
| 30451 | +#endif |
| 30452 | + |
| 30453 | +} // namespace pineforge::source::detail |
0 commit comments