Skip to content

Commit d02f1b7

Browse files
rustyconoverclaude
andcommitted
worker: cache aggregate const_args per execution_id
``_load_aggregate_const_args`` was hitting ``storage.aggregate_get([-2])`` on every aggregate_update / aggregate_window / aggregate_combine / aggregate_finalize call — once per output row for windowed aggregates. With the SQLite backend the read is microseconds and the cost is invisible; with the CF Durable Object backend it's a ~60 ms HTTPS RTT per call, so a 5000-row windowed query paid 300 s of round-trip just re-reading values that never change. Const args are written once at aggregate_bind (under group_id=-2) and are immutable for the rest of the execution. Cache them per ``execution_id`` with a sentinel for the "no const args" negative case (most aggregates) so we don't re-hit storage on every call. Process-local LRU mirrors ``_WindowPartitionCache``'s shape: bounded size (1024 entries), thread-safe via ``Lock``, cleared from ``aggregate_destructor`` alongside ``_window_partition_cache``. Measured on ``aggregate/window.test`` (1 launcher worker, CF DO storage in us-east): before: 258 s (mostly per-row storage.aggregate_get on the 5000-row multi-chunk partition test) after: 14 s (~18× speedup; almost all remaining wall-clock is the per-row ``aggregate_window`` worker RPC itself, which is the right ceiling without a batched worker-side path) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 915118a commit d02f1b7

1 file changed

Lines changed: 70 additions & 4 deletions

File tree

‎vgi/worker.py‎

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,51 @@ def clear_execution(self, execution_id: bytes) -> None:
377377
_window_partition_cache = _WindowPartitionCache()
378378

379379

380+
# Sentinel for "we already looked this execution_id up and the worker has no
381+
# const args" — distinguishable from None (= "not yet looked up").
382+
_ABSENT_SENTINEL: Any = object()
383+
384+
385+
class _AggregateConstArgsCache:
386+
"""Process-local LRU of aggregate const_args, keyed by execution_id.
387+
388+
Const args are written once at aggregate_bind (under group_id=-2) and
389+
never change. Every aggregate_update / aggregate_window / aggregate_finalize
390+
needs them, and ``_load_aggregate_const_args`` would otherwise issue one
391+
storage read per call — pathological under remote storage backends.
392+
393+
Cache holds either ``Arguments`` (positive hit), ``_ABSENT_SENTINEL``
394+
(the worker has no const params; remember the negative result to skip
395+
future reads), or absent (not yet looked up).
396+
"""
397+
398+
def __init__(self, max_size: int = 1024) -> None:
399+
self._entries: OrderedDict[bytes, Any] = OrderedDict()
400+
self._lock = Lock()
401+
self._max_size = max_size
402+
403+
def get(self, execution_id: bytes) -> Any:
404+
with self._lock:
405+
entry = self._entries.get(execution_id)
406+
if entry is not None:
407+
self._entries.move_to_end(execution_id)
408+
return entry
409+
410+
def put(self, execution_id: bytes, value: Any) -> None:
411+
with self._lock:
412+
self._entries[execution_id] = value
413+
self._entries.move_to_end(execution_id)
414+
while len(self._entries) > self._max_size:
415+
self._entries.popitem(last=False)
416+
417+
def clear_execution(self, execution_id: bytes) -> None:
418+
with self._lock:
419+
self._entries.pop(execution_id, None)
420+
421+
422+
_aggregate_const_args_cache = _AggregateConstArgsCache()
423+
424+
380425
# Streaming-partitioned aggregate sessions: session state is held in an
381426
# in-process LRU cache for the fast path, and persisted to FunctionStorage
382427
# (under partition_id=0) so chunk RPCs that land on a different pool worker
@@ -1725,13 +1770,33 @@ def _load_aggregate_const_args(
17251770
func_cls: type[AggregateFunction], # type: ignore[type-arg]
17261771
storage: BoundStorage,
17271772
) -> Arguments | None:
1728-
"""Load const arguments stored during aggregate_bind (group_id=-2)."""
1773+
"""Load const arguments stored during aggregate_bind (group_id=-2).
1774+
1775+
Cached in-process per execution_id. The const args are written once
1776+
at aggregate_bind time and never change, so a single ``aggregate_get``
1777+
is enough for the whole execution. Without the cache, windowed
1778+
aggregates would issue one storage read per output row — devastating
1779+
under remote storage backends like the CF Durable Object (~60 ms RTT
1780+
× N output rows).
1781+
"""
17291782
from vgi.arguments import Arguments
17301783

1784+
cached = _aggregate_const_args_cache.get(storage._execution_id)
1785+
if cached is _ABSENT_SENTINEL:
1786+
return None
1787+
if cached is not None:
1788+
return cached # type: ignore[return-value]
1789+
17311790
result = storage.aggregate_get([-2])
1732-
if result[0] is not None:
1733-
return Arguments.deserialize_from_bytes(result[0][1])
1734-
return None
1791+
if result[0] is None:
1792+
# Most aggregates have no const params; cache the negative result
1793+
# so we don't re-hit storage on every aggregate_window /
1794+
# aggregate_update / etc.
1795+
_aggregate_const_args_cache.put(storage._execution_id, _ABSENT_SENTINEL)
1796+
return None
1797+
parsed = Arguments.deserialize_from_bytes(result[0][1])
1798+
_aggregate_const_args_cache.put(storage._execution_id, parsed)
1799+
return parsed
17351800

17361801
def aggregate_bind(
17371802
self,
@@ -2051,6 +2116,7 @@ def aggregate_destructor(
20512116
# RPC was dropped mid-query.
20522117
storage.aggregate_window_partition_clear()
20532118
_window_partition_cache.clear_execution(request.execution_id)
2119+
_aggregate_const_args_cache.clear_execution(request.execution_id)
20542120

20552121
return AggregateDestructorResponse()
20562122

0 commit comments

Comments
 (0)