You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
perf(kernel): cache window bounds instead of re-deriving them per simulation
A search runs thousands of simulations over the same charge/export windows,
varying only the limits, but every call re-derived the window start/end bounds
from the window dicts - four ctypes arrays for the kernel scenario and two tuples
for the prediction cache key. That was the largest single block of Python time in
a plan.
Caches them in prediction_kernel keyed on the identity of the window list, with
the derived fields built lazily so a caller that only wants the hash tuple never
pays to build the ctypes arrays. Hit rate on the benchmark is ~94%.
Correctness rests on every mutation of a window's start/end invalidating the
cache, so the 16 in-place assignments on the planning path now go through
set_window_start()/set_window_end(). The guard is run_window_cache_tests, which
replays a full calculate_plan with VALIDATE_WINDOW_CACHE on - that re-derives the
bounds on every cache hit and raises on any stale entry, so a future bare
window["start"] = ... on this path fails the suite rather than silently
simulating the wrong window geometry. The test also asserts the validator itself
catches a planted stale entry, so it cannot pass vacuously.
Pool workers unpickle fresh window lists every call and can never hit the cache,
where leaving it on cost ~3% of a pooled plan, so Pool() now runs
disable_window_cache as its worker initialiser. The cache is bounded and pins the
lists it keys on, so a caller that never repeats a list cannot grow it without
limit or alias a freed list's id() onto the wrong entry.
Measured on random scenario 0 (median of 3):
threads=0 2366.3ms -> 1839.0ms -22.3%
threads=auto 2134.0ms -> 2146.8ms +0.6% (noise)
C++ share of plan time rises from 43.7% to 56.2%; pk_run itself is unchanged at
19,209 calls of 55.0us, which is what confirms the simulation work is identical.
The 20 scenario benchmark drops 42.6s -> 33.4s.
Verified byte-identical: all 20 random scenarios unchanged on metric, cost and
all three PV futures, and kernel_parity passes.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
0 commit comments