Skip to content

perf(python): stream unfiltered batch sampler ranges - #11744

Merged
Xuanwo merged 1 commit into
lance-format:mainfrom
zhangyue19921010:perf/python-stream-shard-ranges
Dec 2, 2026
Merged

Xuanwo merged 1 commit into
lance-format:mainfrom
zhangyue19921010:perf/python-stream-shard-ranges

Conversation

@zhangyue19921010

Copy link
Copy Markdown
Collaborator

Problem

Unfiltered batch sharding builds the whole rank's list of row ranges before returning a reader. This makes even a single-batch read allocate in proportion to the shard size.

Solution

Generate each range when the native reader asks for it. Randomized sharding keeps a shuffled list of start offsets and computes the corresponding ends on demand.

Result

On two fragments totaling 4,194,307 rows, first-batch peak traced Python allocation fell from about 16 MiB to under 4 KiB for sequential reads (two ranks, 16 rows per batch, readahead 4). The sampler suite passed all 135 tests, including seeded order across deletions and a partial final batch.

In a separate full-shard check, both implementations used the same optimized native reader at 38040f844. With 262,147 source rows and 256-row batches, median elapsed time over 48 runs went from 49.2 to 49.9 ms sequentially and from 49.4 to 50.4 ms with shuffling. With 16-row batches, the medians over 12 runs went from 676 to 689 ms and from 684 to 703 ms, respectively.

Fixes #11743

Problem

ShardedBatchSampler allocates every range assigned to the rank before
returning the first unfiltered batch.

Solution

Keep sequential batch starts in a range and construct each pair of bounds
as the native reader consumes it. Shuffle only the starts when requested.

Result

Reading a prefix no longer visits every sequential range. Seeded order
and partial batches are preserved after deletions.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@github-actions github-actions Bot added A-python Python bindings performance labels Dec 2, 2026

@lance-gatekeeper lance-gatekeeper Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Gate recommendation: approve.

No blocking findings. Local tests confirmed on-demand range consumption and preserved seeded order across deletions and a partial batch. A same-reader comparison reduced sequential first-batch peak Python allocation from about 16 MiB to 2.5 KiB, with small full-shard timing differences.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Dec 2, 2026
@Xuanwo
Xuanwo merged commit 0ce6622 into lance-format:main Dec 2, 2026
22 checks passed
@zhangyue19921010

Copy link
Copy Markdown
Collaborator Author

@Xuanwo Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-python Python bindings K-approved Latest Gatekeeper recommendation permits acceptance. performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: ShardedBatchSampler builds every range before the first unfiltered batch

2 participants