Skip to content

Commit 01ec1f0

Browse files
jnsdlsclaude
andauthored
perf: faster inserts and push responses, quicker import, build and map benchmarks (#31)
## Summary Rebuilt on top of #34, which landed the slotted AST, the inline query endpoint and the z2s plans this PR first carried. What's left is what #34 didn't cover: ```diff tx.mutate.<table>.insert / upsert - per column: server name, cast lookup, Params.column + per-table write plan (quoted name, cast, fetched once); Params.bind inlines the dedup js_stringify / bind keys + exact str and int first, the match only for subclasses handle_mutate - MutationID(...), MutationResponse(...), MutateSuccess(...) # validated + WireModel.trusted(...) # values it built or already parsed - MutateParams.from_json(dict(query)) + MutateParams.from_query(query) # two str keys, else Pydantic get_query - regex split + str.split when the name has no "|" import + defer_build on wire and schema models; TypeAdapters built on first use bench + build.* and map.*: the query endpoint's two stages, both sides + async CodSpeed cases driven without run_until_complete ``` I dropped two pieces of the old #31 after measuring them on top of #34: - **The transform-request fast path.** #32's `PlainJSON` already matches it. - **Copy-on-write in `map_ast`.** It gained 2 to 3% on simple queries, nothing on complex ones, and would have added branches the compatibility proof has to cover. ## Evidence Paired local runs against `main` (c2d58da), best of 15: | case | main | this PR | | --- | --- | --- | | mutate_cpu.crud.n100 | 2.68 ms | 2.26 ms | | mutate_cpu.put.n100 | 744 µs | 660 µs | | query.simple.b100 | 838 µs | 810 µs | | import of the endpoints and asyncpg adapter | 110 ms | 94 ms | CodSpeed against `main`: 6 benchmarks faster, none slower. The mutate cases gained 23 to 29%. The single-query async cases (`query.simple.b1` +31%, `query_json.simple.b1` +21%, `query.complex.b1` +13%) gained mostly because their harness no longer goes through `run_until_complete`, not from SDK changes. - **Before:** n/a, speed only. **After:** 714 passed, 100% coverage, and `python -m tests.compat` reports every port module exercised differentially. Python-only paths, such as str and int subclasses and a `user_id` that is not a str, carry `# python-only` markers and unit tests. ## Merge Danger **Door:** two-way **Blast Radius:** None Nothing on the wire or in the public API changes. `WireModel.trusted` is new and builds a model without validating it, so callers must only pass values they have already checked. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c2d58da commit 01ec1f0

19 files changed

Lines changed: 251 additions & 49 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ jobs:
147147
matrix:
148148
include:
149149
- name: cpu
150-
groups: query,query_json,parse,parse_json,mutate_cpu,z2s,startup
150+
groups: query,query_json,build,map,parse,parse_json,mutate_cpu,z2s,startup
151151
- name: postgres
152152
groups: run,mutate,pushproc
153153
services:

‎bench/__main__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
},
5555
},
5656
}
57-
CPU_GROUPS = ["query", "query_json", "parse", "parse_json", "mutate_cpu", "z2s"]
57+
CPU_GROUPS = ["query", "query_json", "build", "map", "parse", "parse_json", "mutate_cpu", "z2s"]
5858
POSTGRES_GROUPS = ["run", "mutate", "pushproc"]
5959
GROUPS = [*CPU_GROUPS, *POSTGRES_GROUPS, "startup"]
6060
"""A case's group is its name's first part."""

‎bench/cases.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ async def setup(b: Bench) -> None:
178178

179179

180180
GROUPS: dict[str, Callable[[Bench], Coroutine[Any, Any, None]]] = {
181-
**{g: cpu_group(g) for g in ("query", "query_json", "parse", "parse_json", "mutate_cpu", "z2s")},
181+
**{g: cpu_group(g) for g in ("query", "query_json", "build", "map", "parse", "parse_json", "mutate_cpu", "z2s")},
182182
"run": run,
183183
"mutate": mutate,
184184
"pushproc": pushproc,

‎bench/cpu.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
)
2929
from tests.wire import mutators
3030
from tests.wire.programs import apply, make_handler
31+
from zero_server.protocol.ast import map_ast
3132
from zero_server.protocol.custom_queries import TransformRequestMessage
3233
from zero_server.protocol.push import PushBody
3334
from zero_server.protocol.wire import parse_wire
@@ -101,4 +102,12 @@ async def push_fake(kind: str = kind, n: int = n) -> bytes:
101102
}.items():
102103
q = apply(zql.table(program["table"]), program["ops"])
103104
out[f"z2s.{name}"] = lambda q=q: compile_query(server_schema, query_schema, q.ast, q.format)
105+
106+
# The query endpoint's two stages on their own: building a program's query, and mapping its AST to
107+
# server names.
108+
for kind in ("simple", "complex"):
109+
program = W["programs"][kind]
110+
ast = apply(zql.table(program["table"]), program["ops"]).ast
111+
out[f"build.{kind}"] = lambda p=program: apply(zql.table(p["table"]), p["ops"])
112+
out[f"map.{kind}"] = lambda ast=ast: map_ast(ast, query_schema.client_to_server_mapper)
104113
return out

‎bench/test_cpu.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import asyncio
1111
import inspect
12+
from collections.abc import Callable, Coroutine
1213
from typing import Any
1314

1415
import pytest
@@ -19,7 +20,26 @@
1920
CASES = LOOP.run_until_complete(cases())
2021

2122

23+
def _drive(fn: Callable[[], Coroutine[Any, Any, Any]]) -> Callable[[], Any]:
24+
"""Run an async case to completion without the event loop, as long as it never waits on anything.
25+
26+
None of them does: their database answers from memory. Each `run_until_complete` would otherwise
27+
add two trips through the loop's selector, which outweigh the smaller cases and hide their changes.
28+
"""
29+
30+
def run() -> Any:
31+
coroutine = fn()
32+
try:
33+
coroutine.send(None)
34+
except StopIteration as done:
35+
return done.value
36+
coroutine.close()
37+
raise RuntimeError("an async case waited on something, so it needs the event loop")
38+
39+
return run
40+
41+
2242
@pytest.mark.parametrize("name", CASES)
2343
def test_cpu(benchmark: Any, name: str) -> None:
2444
fn = CASES[name]
25-
benchmark((lambda: LOOP.run_until_complete(fn())) if inspect.iscoroutinefunction(fn) else fn)
45+
benchmark(_drive(fn) if inspect.iscoroutinefunction(fn) else fn)

‎harness/bench/cases.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function cpuGroup(group: string) {
7878
/** Each group's cases. A case's group is its name's first part. */
7979
const groups: Record<string, () => Promise<void>> = {
8080
...Object.fromEntries(
81-
['query', 'query_json', 'parse', 'parse_json', 'mutate_cpu', 'z2s'].map(g => [g, cpuGroup(g)]),
81+
['query', 'query_json', 'build', 'map', 'parse', 'parse_json', 'mutate_cpu', 'z2s'].map(g => [g, cpuGroup(g)]),
8282
),
8383
// tx.run end to end: compile, query, decode.
8484
async run() {

‎harness/bench/cpu_cases.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {query} from '../query.ts';
77
import {schema} from '../schema.ts';
88
import {pushBodySchema, transformRequestMessageSchema, v} from '../zero.ts';
99
import {compile} from '../node_modules/@rocicorp/zero/out/z2s/src/compiler.js';
10+
import {mapAST} from '../node_modules/@rocicorp/zero/out/zero-protocol/src/ast.js';
11+
import {clientToServer} from '../node_modules/@rocicorp/zero/out/zero-schema/src/name-mapper.js';
1012
import {formatPgInternalConvert} from '../node_modules/@rocicorp/zero/out/z2s/src/sql.js';
1113
import {asQueryInternals} from '../node_modules/@rocicorp/zero/out/zql/src/query/query-internals.js';
1214
import {getServerSchema} from '../node_modules/@rocicorp/zero/out/zero-server/src/schema.js';
@@ -81,5 +83,14 @@ export async function cpuCases(): Promise<Map<string, () => unknown>> {
8183
const qi = asQueryInternals(build(p));
8284
out.set(`z2s.${name}`, () => formatPgInternalConvert(compile(serverSchema, schema, qi.ast, qi.format)));
8385
}
86+
// The query endpoint's two stages on their own: building a program's query, and mapping its AST to
87+
// server names.
88+
const mapper = clientToServer(schema.tables);
89+
for (const kind of ['simple', 'complex']) {
90+
const program = W.programs[kind];
91+
const ast = asQueryInternals(build(program)).ast;
92+
out.set(`build.${kind}`, () => build(program));
93+
out.set(`map.${kind}`, () => mapAST(ast, mapper));
94+
}
8495
return out;
8596
}

‎src/zero_server/protocol/mutate_server.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
(905d08516ba0caaf314cd28e93a5a47e8acdd482). Apache-2.0, Copyright Rocicorp.
55
"""
66

7+
from collections.abc import Mapping
78
from typing import Literal
89

910
from pydantic import Field, StrictStr
@@ -32,3 +33,12 @@ class MutateParams(WireModel):
3233

3334
schema_: StrictStr = Field(alias="schema")
3435
app_id: StrictStr = Field(alias="appID")
36+
37+
@classmethod
38+
def from_query(cls, query: Mapping[str, str]) -> "MutateParams":
39+
"""Parse the query string. Raises `ValidationError` on a malformed one."""
40+
schema, app_id = query.get("schema"), query.get("appID")
41+
# Field names pydantic would also read are left to it.
42+
if type(schema) is str and type(app_id) is str and "schema_" not in query and "app_id" not in query:
43+
return cls.trusted(schema_=schema, app_id=app_id)
44+
return cls.from_json(dict(query))

‎src/zero_server/protocol/wire.py‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ class WireModel(BaseModel):
132132
validate_by_name=True,
133133
serialize_by_alias=True,
134134
extra="ignore",
135+
# Validators are built on first use rather than at import, which keeps process startup short.
136+
defer_build=True,
135137
)
136138

137139
nullable: ClassVar[frozenset[str]] = frozenset()
@@ -168,6 +170,22 @@ def to_json(self) -> dict[str, JSONValue]:
168170
"""
169171
return _dump_model(self)
170172

173+
@classmethod
174+
def trusted(cls, **fields: Any) -> Self:
175+
"""A model of `fields`, by field name, without validating them: for values already checked.
176+
177+
What `model_construct` builds, without its per-field bookkeeping. Absent fields take their
178+
defaults, which must not be factories.
179+
"""
180+
model = cls.__new__(cls)
181+
defaults = _defaults(cls)
182+
# `fields` is this call's own dict, so it can become the model's.
183+
_set(model, "__dict__", {**defaults, **fields} if defaults else fields)
184+
_set(model, "__pydantic_fields_set__", set(fields))
185+
_set(model, "__pydantic_extra__", None)
186+
_set(model, "__pydantic_private__", None)
187+
return model
188+
171189
@classmethod
172190
def from_json(cls, value: object) -> Self:
173191
"""Validate `value` off the wire, as `parse_wire` does."""
@@ -187,6 +205,19 @@ def _refuse_null_for_optional(cls, data: Any, info: ValidationInfo) -> Any:
187205
return data
188206

189207

208+
_set = object.__setattr__
209+
210+
211+
@functools.cache
212+
def _defaults(cls: type[WireModel]) -> dict[str, Any]:
213+
out: dict[str, Any] = {}
214+
for name, field in cls.model_fields.items():
215+
if not field.is_required():
216+
assert field.default_factory is None
217+
out[name] = field.default
218+
return out
219+
220+
190221
@functools.cache
191222
def _adapter[M: WireModel](cls: type[M]) -> TypeAdapter[M]:
192223
return TypeAdapter(cls)
@@ -211,7 +242,9 @@ def _dump_model(model: WireModel) -> dict[str, Any]:
211242
return model._ordered(out)
212243

213244

214-
_ANY: TypeAdapter[Any] = TypeAdapter(Any)
245+
@functools.cache
246+
def _any() -> TypeAdapter[Any]: # python-only: for what only a Python caller hands a builder
247+
return TypeAdapter(Any)
215248

216249

217250
def dump_json(value: Any) -> Any:
@@ -228,7 +261,7 @@ def dump_json(value: Any) -> Any:
228261
if isinstance(value, StrEnum):
229262
return value.value
230263
# Anything else came through the builder into an AST node: serialize it as Pydantic does.
231-
return _jsonable(_ANY.dump_python(value)) # python-only: what only a Python caller hands a builder
264+
return _jsonable(_any().dump_python(value)) # python-only: what only a Python caller hands a builder
232265

233266

234267
def _jsonable(value: Any) -> Any: # python-only: a Python object a builder was handed, such as a dataclass

‎src/zero_server/query/registry.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ def define_queries(definitions: "QueryDefinitions | Registry", overrides: "Query
142142

143143
def get_query(queries: Registry, name: str) -> CustomQuery | Registry | None:
144144
"""The query at a dot- or bar-separated `name`, or, as upstream has it, the namespace there."""
145-
return value_at_path(queries, name, _SEPARATOR)
145+
# A plain dotted name, the usual kind, splits without the regex.
146+
return value_at_path(queries, name, _SEPARATOR if "|" in name else ".")
146147

147148

148149
def must_get_query(queries: Registry, name: str) -> CustomQuery | Registry:

0 commit comments

Comments
 (0)