Skip to content

[Data] Skip filtered-out WebDataset tar member payloads - #68403

Merged
owenowenisme merged 4 commits into
ray-project:masterfrom
Yin-1022:data/webdataset-filter-before-read
Nov 27, 2026
Merged

owenowenisme merged 4 commits into
ray-project:masterfrom
Yin-1022:data/webdataset-filter-before-read

Conversation

@Yin-1022

@Yin-1022 Yin-1022 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR addresses unnecessary memory allocations in Ray Data's WebDataset TAR reader, where _tar_file_iterator() reads the full payload of each regular TAR member before applying fileselect, including members that are ultimately excluded.

The change moves filename filtering ahead of payload extraction, avoiding materialization of filtered-out data while preserving existing behavior.

Changes

  • Moved extractfile().read() after filerename and fileselect checks in _tar_file_iterator().
  • Preserved the existing semantics where filerename is applied before fileselect.
  • Added a regression test verifying that only selected TAR members have their payloads extracted, including cases where filename renaming affects selection.

Before / After Comparison

Behavior:

Before After
Payload extraction Materializes all regular member payloads before filtering Only materializes selected payloads
fileselect output Correct Unchanged
filerename semantics Applied before filtering Preserved
Regression test Failed Passed

Memory benchmark:

Using a TAR archive containing a 64 MiB excluded member and a small selected .txt member, a standalone benchmark compared the original reader against an early-filtering implementation.

Round Before After (early filtering)
1 128.80 MiB 0.06 MiB
2 128.78 MiB 0.06 MiB
3 128.78 MiB 0.06 MiB

Measurements represent peak Python-tracked memory allocations collected with tracemalloc, not process RSS. No consistent execution-time improvement is claimed.

Related issues

Closes #68352

Additional information

Test results

Ran the full test_webdataset.py test suite on both the modified branch and the unmodified master (Windows environment).

Result Master Modified branch
Passed 12 13
Failed 13 13

The modified branch introduced one additional passing regression test, with no new test failures compared to master.

The same 13 tests failed on both branches:

  • 3 tests failed due to the missing torch dependency.
  • 10 tests failed with ValueError involving Windows temporary paths.

The new test_webdataset_fileselect_skips_unselected_payloads regression test passed on the modified branch and failed before the fix, confirming that excluded TAR members are no longer unnecessarily extracted while preserving filerename and fileselect behavior.

Environment: Windows, Ray master commit 8b69afe261.

@Yin-1022
Yin-1022 requested review from a team as code owners November 20, 2026 09:31

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request optimizes the WebDataset datasource by delaying the extraction of file payloads until after the filename suffix is checked, preventing unnecessary reads. A corresponding unit test was also added to verify this behavior. Feedback on the changes points out a redundant duplicate check of _check_suffix that was left in the code after moving the extraction logic, which should be removed.

Comment on lines +148 to +151
if not _check_suffix(fname, fileselect):
continue

data = stream.extractfile(tarinfo).read()

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.

medium

The check if not _check_suffix(fname, fileselect): is now performed twice: once before extracting the file payload (lines 148-149) and once after (lines 152-153). Since fname and fileselect do not change, the second check is completely redundant and should be removed to keep the code clean.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching. Removed the redundant fileselect check and kept the filtering logic before payload extraction.

@Yin-1022
Yin-1022 force-pushed the data/webdataset-filter-before-read branch from 71b44f8 to ea90750 Compare November 20, 2026 10:46
@ray-gardener ray-gardener Bot added data Ray Data-related issues community-contribution Contributed by the community labels Nov 20, 2026

@Efutrrionpy Efutrrionpy 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.

LGTM. Could we say “Only materializes selected payloads” in the table? In streaming mode, tarfile still reads through the skipped members.

@Yin-1022

Copy link
Copy Markdown
Contributor Author

LGTM. Could we say “Only materializes selected payloads” in the table? In streaming mode, tarfile still reads through the skipped members.

Thanks for the clarification! I've updated the comparison table to say "Only materializes selected payloads" to distinguish memory allocation from the underlying streaming reads.

chia7712 pushed a commit to opensource4you/readme that referenced this pull request Nov 25, 2026
fellowship: 提名 Yin-1022 (ray, 2026-11)

建議等級:2 萬
- [x] 關係人宣告:提名人與被提名者無親屬、僱傭、財務、業務合作或其他可能影響公正判斷之利害關係
- 在兩個月內自己找到 8 個 Ray Data Issue 並開了 PR,其中 6 個 PR 被 Merged ([Merged PRs](https://github.com/ray-project/ray/pulls?q=is%3Apr+author%3AYin-1022+is%3Amerged+merged%3A%3C%3D2026-11-24))
- 兩個月內共 review 28 個 PR,其中 10 個提出的問題有被作者採納修正([#68402](https://github.com/ray-project/ray/pull/68402)、[#68290](https://github.com/ray-project/ray/pull/68290)、[#67524](https://github.com/ray-project/ray/pull/67524))
- 大部分 PR 都有清楚列出 Before/After 的差距,performance 相關的會附上數據 ([#67935](ray-project/ray#67935), [#68403](ray-project/ray#68403))
- 對於 Maintainer 的 Review 會清楚了解之後回覆 ([#67935 comment](ray-project/ray#67935 (comment)), [#66878 comment](ray-project/ray#66878 (comment)), [#67505 comment](ray-project/ray#67505 (comment)))
@owenowenisme owenowenisme added the go add ONLY when ready to merge, run all tests label Nov 26, 2026
@owenowenisme
owenowenisme enabled auto-merge (squash) November 26, 2026 11:45
auto-merge was automatically disabled November 26, 2026 16:51

Head branch was pushed to by a user without write access

@Yin-1022

Copy link
Copy Markdown
Contributor Author

I resolved the merge conflict by adapting the fix to the shared WebDataset utilities introduced by the DataSource V2 migration.

The original filtering behavior is preserved, and the fix now applies to both V1 and V2.

Revalidated on Linux:

  • Regression test against upstream: failed as expected.
  • Full WebDataset test suite with the fix: 47 passed.

No additional behavioral changes were introduced.

@owenowenisme
owenowenisme merged commit e3ecf92 into ray-project:master Nov 27, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community data Ray Data-related issues go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Data] Avoid reading filtered-out WebDataset tar members into memory

3 participants