Skip to content

Commit dc3b0cf

Browse files
jnsdlsclaude
andcommitted
perf: walk tx.run results for unsafe integers only when one was parsed
extract_zql_result walked every decoded result looking for an integer outside JavaScript's safe range, calling js_keys on each object. On a 10k-row result the walk cost more than json.loads itself. parse_int now sets a flag when it builds an _Unsafe, and the walk runs only when the flag is set. The error path and message are unchanged. On the 10k-row bench against @rocicorp/zero 1.10.0 (asyncpg), flat went from 2.17x TS to 0.90x and related from 1.51x to 0.95x. The SQL tier now reaches the walk only through the pinned counter query, so two unit tests cover the rest of it: one checks the path in the message, and one checks that an unsafe integer replaced by a duplicate key is still ignored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent fecb718 commit dc3b0cf

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

‎src/zero_server/server/pg_query_executor.py‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ class _Unsafe(int):
2323
"""An integer JavaScript cannot hold exactly. Upstream parses it as a bigint and refuses it."""
2424

2525

26-
def _parse_int(text: str) -> int:
27-
value = int(text)
28-
return _Unsafe(value) if abs(value) > _MAX_SAFE else value
29-
30-
3126
def _parse_float(text: str) -> int | float:
3227
"""A whole number written with a fraction, as `numeric` epoch milliseconds are, comes back an `int`.
3328
@@ -53,8 +48,19 @@ def _path_to_unsafe(value: Any) -> str | None:
5348

5449

5550
def extract_zql_result(rows: list[Row]) -> Any:
56-
result = json.loads(rows[0][ZQL_RESULT_KEY], parse_int=_parse_int, parse_float=_parse_float)
57-
if (path := _path_to_unsafe(result)) is not None:
51+
unsafe = False
52+
53+
def parse_int(text: str) -> int:
54+
nonlocal unsafe
55+
value = int(text)
56+
if abs(value) <= _MAX_SAFE:
57+
return value
58+
unsafe = True
59+
return _Unsafe(value)
60+
61+
result = json.loads(rows[0][ZQL_RESULT_KEY], parse_int=parse_int, parse_float=_parse_float)
62+
# A duplicate key can drop a parsed unsafe integer, so the walk may still find none.
63+
if unsafe and (path := _path_to_unsafe(result)) is not None:
5864
raise Exception(f"Value exceeds safe Number range. {path}")
5965
return result
6066

‎tests/unit/test_api.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,18 @@ class Connection:
184184

185185
with pytest.raises(ValueError, match="'pg8000' is not supported"):
186186
await sqlalchemy_transaction(Connection()) # ty: ignore[invalid-argument-type]
187+
188+
189+
def test_zql_result_names_the_first_unsafe_integer() -> None:
190+
from zero_server.server.pg_query_executor import extract_zql_result
191+
192+
text = '[{"n": [1], "m": [2, -9007199254740993]}, {"n": 9007199254740993}]'
193+
with pytest.raises(Exception, match=r"^Value exceeds safe Number range\. \[0\]\['m'\]\[1\] = -9007199254740993$"):
194+
extract_zql_result([{"zql_result": text}])
195+
196+
197+
def test_zql_result_ignores_an_unsafe_integer_a_duplicate_key_replaced() -> None:
198+
from zero_server.server.pg_query_executor import extract_zql_result
199+
200+
text = '{"n": 9007199254740993, "n": 9007199254740991}'
201+
assert extract_zql_result([{"zql_result": text}]) == {"n": 9007199254740991}

0 commit comments

Comments
 (0)