Repository navigation
[Data] Add batch_size to MCAPDatasource for streaming reads - #65787
lorenzozanee wants to merge 2 commits into
Conversation
MCAPDatasource previously buffered an entire file into a single block before yielding, which makes peak memory scale with file size and prevents downstream operators from overlapping with the read within a file. Add an optional batch_size to MCAPDatasource and ray.data.read_mcap. When set, _read_stream yields one block per batch (counted after filtering) and reduces peak memory to one batch. Default None preserves the existing single-block behavior. Closes #65641 Signed-off-by: lorenzozanee <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request adds a batch_size parameter to read_mcap and MCAPDatasource, allowing users to configure the number of messages per output block rather than yielding one block per file. Feedback on this change highlights a potential issue where boolean values (which are subclasses of int in Python) could bypass the type validation and be treated as a batch size of 1, potentially causing performance degradation. It is recommended to explicitly exclude boolean types from the batch_size validation.
| if not isinstance(batch_size, int) or batch_size <= 0: | ||
| raise ValueError( | ||
| f"batch_size must be a positive integer, got {batch_size!r}" | ||
| ) |
There was a problem hiding this comment.
In Python, bool is a subclass of int. Therefore, passing batch_size=True will pass the isinstance(batch_size, int) check and bypass the batch_size <= 0 check (since True > 0). This would result in a batch_size of 1 (as True behaves as 1 in numeric contexts), causing the reader to yield a block for every single message. For large MCAP files, this can lead to severe performance degradation, high memory overhead, or driver OOM due to millions of tiny blocks.
To prevent this, explicitly exclude bool types from the validation.
| if not isinstance(batch_size, int) or batch_size <= 0: | |
| raise ValueError( | |
| f"batch_size must be a positive integer, got {batch_size!r}" | |
| ) | |
| if not isinstance(batch_size, int) or isinstance(batch_size, bool) or batch_size <= 0: | |
| raise ValueError( | |
| f"batch_size must be a positive integer, got {batch_size!r}" | |
| ) |
MCAPDatasource previously buffered an entire file into a single block before yielding, which makes peak memory scale with file size and prevents downstream operators from overlapping with the read within a file. Add an optional batch_size to MCAPDatasource and ray.data.read_mcap. When set, _read_stream yields one block per batch (counted after filtering) and reduces peak memory to one batch. Default None preserves the existing single-block behavior. Closes #65641 Signed-off-by: lorenzozanee <[email protected]>
Description
MCAPDatasource._read_stream previously accumulated all messages of a file into a single block and yielded once at the end. For large MCAP recordings this makes peak memory scale with file size times concurrent read tasks and prevents downstream stages from overlapping with the read within a file.
This change adds an optional
batch_size(message count) toMCAPDatasourceandray.data.read_mcap. When set,_read_streamyields one block per batch instead of one block per file.batch_size=None(default) keeps the current behavior exactly; output schema and predicate pushdown are unchanged, only block boundaries change. The implementation follows the existingIterator[Block]protocol already used by CSV/JSON/Parquet datasources and aligns with Daft'sread_mcap(batch_size=1000)precedent.Related issues
Closes #65641
No open PR currently addresses this issue. The change is limited to the MCAP datasource and its public read API.
Additional information
python -m py_compileandgit diff --checkpass.Nonepreserves byte-identical behavior; existingtest_mcap.pyexpectations remain valid. Fulltest_mcap.pysuite requires a complete Ray build and is expected to run in CI.