Repository navigation
Native count(): arcadedb_n_count returns the number of rows still in the library, no row is encoded or built (rank 2 of #388) - #389
Conversation
…ts (refs #388 and #315) count() walked the whole result with `for _ in self`: one NativeResult per row, built and thrown away. These tests pin the number it returns (the rows the same query gives through to_list(), at sizes around the 256-row first reply and the 2048-row later replies, for typed and wide rows, aggregates, a Cypher result and a command), the state it leaves the result in (only the rows not yet read are counted, an empty or exhausted result counts 0, a closed result or database raises), the error an engine failure met after the first reply raises, and that no NativeResult is built for a counted row. The last one fails on the parent. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…lt (refs #388 and #315) count() read the result with `for _ in self`, so every counted row was a tuple popped from the buffer and a NativeResult built to be thrown away. It now adds len() of the buffer and of each reply the cursor gives, through _fill(into) as to_list() does. The replies are still read and parsed (the library encodes every row), which is what the next commit removes. Checked without the library: the old and the new count return the same number and leave the result in the same state (closed, exhausted, no cursor) for 0, 1, 255, 256, 257, 2303, 2304, 2305 and 5000 rows and for an error met after the first reply; the new one builds 0 NativeResult where the old one built one per row. Not measured: the speed claim waits for a timed A/B. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… call, arcadedb_n_count (refs #388 and #315) After the previous commit count() still reads every reply past the first one as a JSON text (arcadedb_n_next_bin: Java encodes each row, Python parses it) just to take len(). These tests pin the target: a result with rows left in the library is counted with one call of the entry point arcadedb_n_count and no other (the library drains and closes the cursor, so no second close is queued), for a query cursor and for a command cursor; a result whose first reply holds every row, and an exhausted one, make no crossing at all; no reply text is read and no NativeResult is built. They fail on the previous commit (the calls are arcadedb_n_next_bin, and the entry point does not exist). The behaviour tests of the first commit stay the oracle for the number, the state and the errors. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…the library as one long (refs #388 and #315) After the previous commit count() still asked the library for every reply past the first one: Java encoded each row into a JSON text (arcadedb_n_next_bin), Python parsed it into one dict per row, and the dicts were only measured with len(). The new entry point arcadedb_n_count(thread, cursor) drains the cursor in Java (hasNext and next, no row encoded) and closes it, and returns the count as a long; -1 is an error whose text arcadedb_n_last_error holds, built by the same errorJson as the "rerr" of a JSON reply, so the exception count() raises, and the state it leaves the result in, do not change. Like arcadedb_n_tx and arcadedb_n_cmd it returns no reply text: one foreign call, no malloc, no arcadedb_free, no decode. count() asks it only when the cursor still has rows: a result whose first reply holds every row, and an exhausted one, make no crossing. The rows are no longer encoded to be counted, so a row the codec could not encode no longer fails a count(); that was an accident of the old route, not a behaviour. Not measured: the speed and memory claims wait for a timed A/B against 26.11.1.post3. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…w, so its facets survive (refs #388 and #315) Main now appends the facets of a FACETS query to the reply that drains the cursor, and a facets() after the rows are read returns them. arcadedb_n_count drains and closes the cursor in the library and carries no facets, so count() followed by facets() returned None on the rebased branch. A result records at creation whether its statement text holds the word FACETS (any case, a string literal included, so a false positive costs speed only); count() reads such a result with the iterating route of main and uses the new entry for every other result. Tests: count() then facets() equals facets() after to_list() on an engine that has the clause (skipped before 26.12.1), a statement that mentions the word makes no arcadedb_n_count call, and a statement without it still makes the single call. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
31f7738 to
3574efc
Compare
…hing an error text On the installed 26.11.1 engine the statement is refused with a message that does not say 'syntax error', so the skip never fired and the parity job failed (and the suite accepts no skips anyway). The test now asks SELECT version(): below 26.12.1 it pins the refusal, from 26.12.1 on it checks count() and facets() as before (refs #388).
|
I timed this on the mini. Setup: one native library for all three arms (the wheel CI built at
Limits: one run on one host, a result of two columns (an integer and a short string), and the library is the one CI built for this branch with engine I am updating the description with this table and taking the PR out of draft so CI runs on it. |
What does this PR do?
On the native wheel,
ResultSet.count()walked the whole result: every reply past the first came back from the library as a JSON text (arcadedb_n_next_bin), Java encoded every row into it, Python parsed it into one dict per row, and aNativeResultwas built for every row, only to be counted and dropped. Acount()of a million-row result was about 500 crossings of 2,048 rows each and a million objects built for nothing.Now the rows already in the result object (the first reply, up to 256) are counted with
len, and the rows still in the library with one new entry point,arcadedb_n_count(thread, cursor), which returns along: the cursor is drained in Java (hasNextandnext, no row encoded) and closed there. A result whose first reply holds every row, and an exhausted one, make no crossing at all. This is the rank 2 item of #388.The entry point is the way
arcadedb_n_txandarcadedb_n_cmdalready return a number: no reply text, so one foreign call with no malloc, noarcadedb_freeand no JSON decode.-1is an error, and its text is read witharcadedb_n_last_error, built by the sameerrorJsonthat builds thererrof a JSON reply. So the exceptioncount()raises and the state it leaves the result in (closed, exhausted, no cursor) are the ones iteration gave before; the tests compare them.Two commits are Python only and need no new library: the second (count
len(batch)of each reply, noNativeResult) is a step of its own. The fourth adds the entry point,lib.countand the call inNativeResultSet.count. The first and the third are the tests, written before the change they test.A row that the codec could not encode used to fail a
count()with a read error; now it is not encoded, so it does not. That was a side effect of the old route, not a behaviour anyone relied on.Related issues
Refs #388 and #315.
Performance changes
Measured with a paired A/B on the mini: one native library for all three arms (the wheel CI built at
3574efc6dd,arcadedb-26.12.1.dev0-py3-none-manylinux_2_28_x86_64.whl), with only the Python files this PR changes swapped per arm: A is the merge base097436a095, B is3574efc6dd(the tip when the study ran; one test-only commit came after it), C is A again as the control. I used one library because two native builds of the same commit can differ in speed by tens of percent, so two builds would not be a fair pair. 12 interleaved rounds, a bootstrap 95% interval over the rounds, and the rule of #317, read row by row: a row counts only when its own control is inside 0.97 to 1.03. The process was confined to the two cores of one compare lane of the mini (Cpus_allowed_list: 4,6). Another study ran on a second lane at the same time and the host worker recorded 34 s of thermal throttle; every control still stayed inside the band.count_100000count_1count_100to_list_100to_list_100000count()of a 100,000-row result takes 65% less time. In A no call reaches the new entry point, in B every call does, and the answers are the same in all arms.count_1andcount_100were registered as rows that should not move, because neither arm makes a crossing there. They moved anyway: A still builds one result object per row before counting and B takes the length of the first reply. So they are a second gain (58% and 86% less time), and not controls.to_listrows, a path this PR does not touch: both NO CHANGE.count_100000.Limits: one run on one host, a result of two columns (an integer and a short string), and the library is the one CI built for this branch with engine
26.11.1.The figure as registered before the run, for the record (the run differs in using one library instead of the released wheel as the baseline):
db.query("sql", "SELECT id, txt FROM R").count(), the full result, on the native wheel (arcadedb).count()call on a freshly run query (the query itself is outside the timed part, or is the same in both arms).NativeResultper row, so the memory figure should move too.26.11.1.post3, ABBA with an A/A control.NativeResultper row; the new code holds nothing per row. It allocates no array and no copy: the library returns a number.count()returns is compared withlen(to_list())of the same query at 0, 1, 255, 256, 257, 2,303, 2,304, 2,305 and 5,000 rows, for plain, typed and wide rows, aggregates, a Cypher result and a command cursor, and the state of the result is compared after it.How I checked it
tests/test_native_count.py(native wheel only, soconftest.pydoes not collect it on the JVM wheel). The first commit holds the number, state and error tests and one that fails on the parent: noNativeResultis built for a counted row. The third commit holds the crossing tests, which fail on the second commit: a result with rows in the library is counted with the single entry callarcadedb_n_count, a command cursor too, no reply text is read, no second close is queued, and a result held in full by its first reply, or exhausted, makes no foreign call.len(to_list())at the edge sizes of the batches (0, 1, 255, 256, 257, 2,303, 2,304, 2,305, 5,000), for typed wide rows (date, datetime,Decimal, bytes, an integer beyond 2^53, a vector, a nested map), aGROUP BYand a projection of@rid, a CypherMATCH, andINSERT INTO B FROM SELECT FROM R, where the count has to run the statement to its end (the rows are inBafter it).next, and after 300, past the first reply), a second count is 0, a result closed early or on a closed database raises the same errors, and a division by zero at row 2,999 raisesReading the result set failed: ...with the result closed and the database still usable.tests/test_resultset_read_errors.pyalready runscount()over the engine errors on both wheels.count()against a fake library for the sizes above, for a lazy command cursor and for an error after the first reply (same number, same final state, noNativeResultbuilt, no read and no close).uvx pre-commit run --fileson the changed files, and bandit as the JVM workflow runs it.CI: to be filled in once the native workflow has run.
Docs
No page under
docs/or the README describes howcount()crosses on the native wheel, and the documented behaviour does not change.Checklist
uvx pre-commit run --files <changed files>passes