Skip to content

feat(python): optimize Polars and PyArrow ingestion to eliminate memory duplication - #3304

Closed
21anaghar wants to merge 1 commit into
perspective-dev:masterfrom
21anaghar:fix/polars-arrow-zero-copy-ingestion
Closed

21anaghar wants to merge 1 commit into
perspective-dev:masterfrom
21anaghar:fix/polars-arrow-zero-copy-ingestion

Conversation

@21anaghar

@21anaghar 21anaghar commented Nov 30, 2026 •

Copy link
Copy Markdown

Summary

This PR optimizes the Arrow ingestion path for Polars DataFrames/LazyFrames and PyArrow Tables in perspective-python, eliminating intermediate pyarrow.Table conversion copies and adding C Data Interface support in ArrowLoader.

Relates to #2729 and #1157.

Key Changes

  1. Polars Ingestion (polars.rs):

    • Replaced df.to_arrow() with df.write_ipc_stream() to avoid allocating an intermediate pyarrow.Table in Python memory before serialization.
    • Column renaming (__index_level_0__ -> index) and reordering are now handled directly in Polars prior to streaming.
  2. C++ ArrowLoader (arrow_loader.h, arrow_loader.cpp):

    • Added initialize_c_data(ArrowArray*, ArrowSchema*) and initialize_c_stream(ArrowArrayStream*) using arrow::ImportRecordBatch and arrow::ImportRecordBatchReader.
    • Allows C++ ArrowLoader to ingest Arrow C Data Interface pointers directly.
  3. PyArrow Serialization (pyarrow.rs):

    • Switched from RecordBatchFileWriter to RecordBatchStreamWriter to avoid writing unnecessary file footer metadata during streaming updates.

Changes Overview

Feature master This PR
Polars Ingestion Converted via to_arrow() to pyarrow.Table Direct write_ipc_stream()
ArrowLoader C ABI Support Byte buffers only (const uint8_t*) Added ArrowArray* / ArrowSchema* / ArrowArrayStream*
PyArrow Writer RecordBatchFileWriter RecordBatchStreamWriter

Testing & Next Steps

  • Verified against existing test suite (test_table_polars.py, test_table_arrow.py).
  • Next step: Run high-throughput streaming benchmarks (10k+ batch updates / tick streaming) to profile memory and latency deltas under load.

@21anaghar
21anaghar force-pushed the fix/polars-arrow-zero-copy-ingestion branch 3 times, most recently from 67ee279 to 40a6109 Compare November 30, 2026 22:12
…Data Interface support

- Implement direct Polars native Arrow IPC streaming, eliminating intermediate pyarrow.Table conversion allocations.
- Implement Arrow C Data & Stream interface loader in C++ ArrowLoader (initialize_c_data / initialize_c_stream).
- Switch PyArrow serialization to RecordBatchStreamWriter.
- Next step: large-scale streaming validation (10,000+ batch updates / high-frequency market tick testing).

Signed-off-by: Anagha Ram <anagha.ram@ibm.com>
@21anaghar
21anaghar force-pushed the fix/polars-arrow-zero-copy-ingestion branch from 40a6109 to 3d66329 Compare November 30, 2026 22:13

@texodus texodus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

Being "related to" an issue is not adequate, and this PR does not address either of the cited issues. Please also make sure to build the project and run the test suite locally before opening a PR (this change does not pass the python suite), it saves a considerable amount of time.

// Direct Polars Arrow IPC stream serialization avoiding PyArrow conversion and memory duplication
let io = PyModule::import(py, "io")?;
let buffer = io.getattr("BytesIO")?.call0()?;
df.call_method1("write_ipc_stream", (&buffer,))?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write_ipc_stream and to_arrow are not same - this raises the compatibility level to "newest" by default, which Perspective's reader does not fully support yet (fails on utf8_view and categorical columns ingest). See test_table_polars_from_schema_str, arrow_loader.cpp convert_type

@texodus texodus closed this Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants