perf: cut per-event CPU on the runtime hot paths
Every backend event pays a fixed cost in the event processor and a per-value
cost in the state machinery: reading fields and computed vars, wrapping
values in mutable proxies, building the delta, encoding it to JSON and, with
a redis or disk manager, pickling the touched states. Each of those paths did
work per call that only has to happen once per class, per handler or per
type, and a few did work that was never needed at all.
Computed vars: a cache hit re-validated the whole cached value against the
return type on every read (a 1000-element cached list cost ~1 ms per read;
now ~0.2 us), the three cache attribute names were formatted per access, the
last-updated timestamp was stored for every var although only interval vars
read it, and marking a var dirty walked dependency dicts for names that have
no dependents. The type mismatch is now logged once per computed value.
Fields: scalar values skip the mutability check, and the dirty marking does
not re-assign an already set touched flag through the dev-mode setattr hook.
Mutable proxies: a proxy built for a list element no longer goes through the
class lookup and the type-call dispatch, immutable values return before any
tracking check in getattr, getitem and iter, the dataclass frame
check only runs for proxied dataclasses (via sys._getframe instead of a
five-frame walk), and a method that is rebound to the proxy is no longer
wrapped in a FunctionWrapper first.
Event processor: the event context is built once per enqueue (fork takes the
router_data), transaction ids come straight from os.urandom, the handler's
coroutine-ness, background and supersedes flags and type hints are resolved
once per handler, the per-event imports are hoisted, a handler that yields
nothing skips the yield validation, the memory manager's modify_state and
modify_state_with_links are plain async context managers instead of async
generators, substate lookup resolves the class path once, and build_delta
reads field values without constructing a proxy to unwrap.
Serialization: json_dumps shares one encoder per set of separators instead
of building a JSONEncoder per call, dataclasses serialize through a per-type
serializer instead of dataclasses.fields() per instance, proxied values
resolve their serializer once instead of dispatching twice, and StateUpdate
serializes from a precomputed field tuple.
State managers: redis persists every touched state of a tree in one pipeline
instead of one write task (and one lock check) per state, deserializing a
state updates the instance dict and drops the stale keys instead of filtering
per key, the state-size check only runs on states over the limit, and the
disk manager only derives a token for the states it writes.
Measured on an idle 4-core VM (min of interleaved rounds):
- in-process event pipeline, counter handler with 4 computed vars:
266 -> 205 us/event; 1000-row filtered/sorted table: 8.5 -> 4.4 ms/event - proxy iteration of 1000 dataclass rows: -55%; sorted(): -62%;
attribute chains: -25%; write paths unchanged - cached computed var hit: 1.32 -> 0.19 us (scalar), 992 -> 0.19 us (list of
1000); scalar field read -35%, write -36% - JSON: 4-var delta 4.5 -> 2.7 us; 1000 proxied dataclass rows 2.5 -> 1.6 ms
- redis manager, 35-state tree: 8.1 -> 2.1 ms/event; pickle 5.5 -> 3.3 us
Claude-Session: https://claude.ai/code/session_01DKCRZTdcqM4Smu9MKrjfhx