Skip to content

Commit 32e1df6

Browse files
rustyconoverclaude
andcommitted
perf: cache the immutable table-buffering init payload per execution
Every table_buffering_process RPC re-read the BUFFERING_INIT payload from FunctionStorage, half of all storage round trips for a buffered function (147 of 305 for a 300k-row distributed sum). The payload is written once at init and never changes for the life of the execution id, so a process-local read-through LRU (seeded at init, evicted on destructor) is safe; storage stays authoritative and a miss falls through to it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WzrG3FzSh28NiZ8UnVtRbk
1 parent 0206497 commit 32e1df6

1 file changed

Lines changed: 25 additions & 14 deletions

File tree

‎vgi/worker.py‎

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -551,17 +551,21 @@ def clear_execution(self, shard_key: str, execution_id: bytes) -> None:
551551

552552
# TableBufferingFunction init metadata is persisted to FunctionStorage at
553553
# init time (namespace FrameworkNS.BUFFERING_INIT, key = pack_int_key(_TABLE_BUFFERING_INIT_KEY))
554-
# and cold-loaded by every subsequent table_buffering_{process,combine}
555-
# RPC. There is intentionally NO in-process cache here — workers are
556-
# stateless w.r.t. table_buffering executions, so any pool worker can
557-
# serve any RPC for any execution_id.
558-
#
559-
# Cost: one extra storage round-trip per RPC. Negligible on subprocess
560-
# (SQLite is microseconds). One HTTP RTT on CfDo deployments — if that
561-
# surfaces as a hot-path bottleneck, the mitigation is to thread the
562-
# init metadata bytes through the request envelope from C++ instead.
554+
# and loaded by every subsequent table_buffering_{process,combine} RPC.
555+
# Storage stays authoritative — any pool worker can serve any RPC for any
556+
# execution_id — but the payload is written at init and never changed for
557+
# the life of the execution_id (ids are minted per execution and never
558+
# reused), so a process-local read-through cache is correctness-safe: a
559+
# miss (other machine, LRU eviction, restart) falls through to storage.
560+
# Without it every process() RPC paid one extra storage round trip, half
561+
# of all storage traffic for a buffered function (one HTTP RTT per input
562+
# batch on cloudflare-do). The cache holds the raw payload, so each RPC
563+
# still decodes fresh objects. Evicted on table_buffering_destructor.
563564
_TABLE_BUFFERING_INIT_KEY = -1
564565

566+
# (shard_key, execution_id) -> raw BUFFERING_INIT payload bytes.
567+
_table_buffering_init_cache = _AggregateConstArgsCache()
568+
565569

566570
# Streaming-shape FINALIZE phase materializes the user's
567571
# ``finalize() -> list[batch]`` return into a single state_log keyed
@@ -4108,10 +4112,14 @@ def _load_table_buffering_params(
41084112
request=request,
41094113
attach_plaintext=attach_full,
41104114
)
4111-
payload = cold_storage.state_get(
4112-
FrameworkNS.BUFFERING_INIT,
4113-
BoundStorage.pack_int_key(_TABLE_BUFFERING_INIT_KEY),
4114-
)
4115+
payload = _table_buffering_init_cache.get(cold_storage._shard_key, request.execution_id)
4116+
if payload is None:
4117+
payload = cold_storage.state_get(
4118+
FrameworkNS.BUFFERING_INIT,
4119+
BoundStorage.pack_int_key(_TABLE_BUFFERING_INIT_KEY),
4120+
)
4121+
if payload is not None:
4122+
_table_buffering_init_cache.put(cold_storage._shard_key, request.execution_id, payload)
41154123
if payload is None:
41164124
raise OSError(
41174125
f"table_buffering: unknown execution_id "
@@ -4210,6 +4218,7 @@ def table_buffering_destructor(
42104218
schema_path=getattr(request, "schema_path", None),
42114219
)
42124220
storage = self._bound(func_cls.storage, request.execution_id, request)
4221+
_table_buffering_init_cache.clear_execution(storage._shard_key, request.execution_id)
42134222
storage.execution_clear()
42144223
except Exception:
42154224
_logger.exception(
@@ -4909,11 +4918,13 @@ def init(self, request: InitRequest, ctx: CallContext) -> Stream[ProcessState, G
49094918
if request.phase == TableInOutFunctionInitPhase.TABLE_BUFFERING:
49104919
# Sink init: persist init metadata so any pool worker
49114920
# can cold-load + serve subsequent process/combine RPCs.
4921+
init_payload = _encode_table_buffering_init(request, init_response)
49124922
cold_storage.state_put(
49134923
FrameworkNS.BUFFERING_INIT,
49144924
BoundStorage.pack_int_key(_TABLE_BUFFERING_INIT_KEY),
4915-
_encode_table_buffering_init(request, init_response),
4925+
init_payload,
49164926
)
4927+
_table_buffering_init_cache.put(cold_storage._shard_key, init_response.execution_id, init_payload)
49174928
state = BufferedFinalizeState(
49184929
execution_id=init_response.execution_id,
49194930
ns=FrameworkNS.BUFFERING_INIT,

0 commit comments

Comments
 (0)