Repository navigation
Commit ca665da
perf: build queries up to 2.7x faster by reusing ones already built (#40)
## Summary
Ports upstream 1.11.3's query interning to the builder. When a step
repeats one taken before, the builder returns the query it built then
instead of building it again.
```text
query.where(field, op, value)
key = (step, every argument, type-tagged) # 1 / 1.0 / True / "1" stay apart, floats by hex
if parent._children[key] is alive: return it # children held weakly, parent strongly
build the child, remember it under key
hold it in _RECENT (last 4096 built) # CPython frees a dropped chain at once; V8 waits for GC
```
Subqueries (`related`, `exists`) are keyed by the identity of their
query's AST. The new `CorrelatedSubquery.source` field keeps that query
alive. A two-hop `exists` builds its junction AST new every time, so
it's keyed by its junction condition instead. Python's keys are exact
where upstream's aren't: upstream folds a `flip` of `'yes'` into `true`.
Stacked on #39, which this wins back the builder slowdown for.
## Evidence
Wall-clock, Python median, same machine:
| case | `main` (1.10.0) | #39 | this PR |
| --- | --- | --- | --- |
| build.simple | 3.5 µs | 4.6 µs | 1.3 µs |
| build.complex | 20.8 µs | 25.2 µs | 13.7 µs |
| query.simple.b100 | 834 µs | 907 µs | 547 µs |
| query.complex.b100 | 3.69 ms | 4.17 ms | 2.78 ms |
- `test_rebuilt_queries_match_upstream`: the same programs three times
in one batch through one builder, compared with the npm package. They
cover every step kind: scalar and float `where`, `whereFn` with a static
parameter and `exists`, two-hop `whereExists`, a raw condition,
`related` with and without a callback, `orderBy`, nested `start`,
`limit` and `one`.
- **Before** the two-hop key fix (0d4781b): fails. `scalar: true` then
`false` returned the first query.
- **After:** passes.
- `uv run pytest`: 784 passed, 100% coverage. `uv run python -m
tests.compat`: fully exercised.
## Merge Danger
**Door:** two-way
**Blast Radius:** builder
Queries built from the same builder now share objects. Their ASTs equal
what fresh building gives, including when a caller later mutates a list
it passed in, which upstream does the same way. Memory is bounded:
children are held weakly, plus at most 4096 recently built queries.
Reviewed with sol-loop: 2 rounds with gpt-6.1-sol. Sol verdict: approved
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- codesmith:footer -->
---
<a
href="https://app.blacksmith.sh/agent-labs-dev/codesmith/zero-python/pr/40?autoLogin=true&ref=codesmith_pr_footer"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://pr-comments-assets.blacksmith.sh/codesmith/view-with-codesmith-dark-v3.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://pr-comments-assets.blacksmith.sh/codesmith/view-with-codesmith-light-v3.svg"><img
alt="View with [code]smith"
src="https://pr-comments-assets.blacksmith.sh/codesmith/view-with-codesmith-dark-v3-3x.png"
width="162" height="28"></picture></a> <a
href="https://backend.blacksmith.sh/track/enable-autofix?expires=1795813402&installation_model_id=434381&pr_number=40&ref=codesmith_pr_footer&repository=agent-labs-dev%2Fzero-python&return_to=https%3A%2F%2Fgithub.com%2Fagent-labs-dev%2Fzero-python%2Fpull%2F40&signature=2340a83be99f9f3903c8c5f9ca8c50963658bc75b1f75129e8997f65aa18a38e"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-light.svg"><img
alt="Autofix with [code]smith"
src="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark-3x.png"
width="190" height="28"></picture></a>
<sup>Need help on this PR? Tag <code>@codesmith-bot</code> with what you
need.</sup>
<!-- codesmith:autofix:disabled -->
<!-- /codesmith:footer -->
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>1 parent 9e95bb1 commit ca665da
5 files changed
Lines changed: 259 additions & 32 deletions
File tree
- docs
- src/zero_server
- protocol
- query
- tests
- unit
- wire
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
| 73 | + | |
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
192 | 192 | | |
193 | 193 | | |
194 | 194 | | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
195 | 198 | | |
196 | 199 | | |
197 | 200 | | |
| |||
0 commit comments