Repository navigation
feat(napi): copy-into-cage Arrow result handoff mode - #156
Closed
msrathore-db wants to merge 1 commit into
Closed
msrathore-db wants to merge 1 commit into
msrathore-db wants to merge 1 commit into
Conversation
Adds a third result-handoff path to the napi binding alongside the
default IPC encode: "copy-into-cage". Instead of re-serialising each
RecordBatch to an Arrow IPC stream (StreamWriter -> Vec<u8> -> JS
RecordBatchReader decode, three O(result-size) passes), it allocates a
V8-owned ArrayBuffer per Arrow buffer (napi_create_arraybuffer, in the
V8 heap/"cage") and memcpy's the bytes in, plus a small descriptor tree.
The JS side rebuilds the RecordBatch via apache-arrow's makeData.
Why copy-into-cage (vs borrowed/external zero-copy): it is cage-safe
(works under the V8 sandbox / Electron, where external buffers are
rejected), finalizer-free (V8 owns the bytes; no Arc lifetime coupling
to native memory), and has no use-after-free / mutable-aliasing surface.
It costs one memcpy per buffer but still skips IPC's serialize and
JS-decode passes — on a 26.7 GiB TPC-DS result its handoff is ~half of
IPC's (one pass vs ~two).
Surface:
- Statement.fetchNextBatchCopycage() and AsyncResultHandle
.fetchNextBatchCopycage() (both fetch paths), returning a descriptor
{ numRows, columns: [{ length, offset, nullCount, validity?, buffers,
children }] } whose shape is identical to what the IPC path would
decode to.
- offset!=0 is compacted via concat_batches (defensive; arrow-rs folds
offset into buffers at to_data()).
- arrow-select dependency added for concat_batches.
Opt-in: the driver selects it; the default path is unchanged IPC.
Byte-identical to the IPC path across all tested types + edge cases.
Co-authored-by: Isaac
Signed-off-by: Madhavendra Rathore <madhavendra.rathore@databricks.com>
|
This PR has been marked as Stale because it has been open for 30 days with no activity. If you would like the PR to remain open, please remove the stale label or comment on the PR. |
|
This PR was closed because it has been inactive for 7 days since being marked as stale. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a third result-handoff path to the napi binding alongside the default Arrow-IPC encode: copy-into-cage.
Instead of re-serialising each
RecordBatchto an Arrow IPC stream (StreamWriter→Vec<u8>→ JSRecordBatchReaderdecode — three O(result-size) passes), copy-into-cage allocates a V8-ownedArrayBufferper Arrow buffer (napi_create_arraybuffer, inside the V8 heap / "cage") andmemcpys the bytes in, plus a small descriptor tree. The JS side rebuilds theRecordBatchvia apache-arrow'smakeData.Why copy-into-cage (vs borrowed/external zero-copy)
ArrayBuffers are rejected.Arc/finalizer lifetime coupling to native memory and no use-after-free or mutable-aliasing surface.memcpyper buffer but still skips IPC's serialize and JS-decode passes. On a 26.7 GiB TPC-DS result, the per-step handoff cost is ~half of IPC's (one pass vs ~two: serialize + copy).Surface
Statement.fetchNextBatchCopycage()andAsyncResultHandle.fetchNextBatchCopycage()(both fetch paths).{ numRows, columns: [{ length, offset, nullCount, validity?, buffers, children }] }whose shape is identical to what the IPC path decodes to, so the driver importer is the only consumer that needs to know the difference.offset != 0is compacted to offset-0 viaconcat_batches(defensive; arrow-rs folds offset into buffers atto_data()).arrow-selectdependency forconcat_batches.Opt-in: the driver selects the mode; the default
fetchNextBatch(IPC) path is unchanged.Verification
cargo fmt+clippy --all-targetsclean;cargo testgreen.This pull request and its description were written by Isaac.