Skip to content

perf(py_wheel): defer depset expansion to execution time#3599

Merged
rickeylev merged 3 commits intobazel-contrib:mainfrom
bd-dstodolsky:perf-py-wheel-defer-depset
Feb 15, 2026
Merged

perf(py_wheel): defer depset expansion to execution time#3599
rickeylev merged 3 commits intobazel-contrib:mainfrom
bd-dstodolsky:perf-py-wheel-defer-depset

Conversation

@bd-dstodolsky
Copy link
Contributor

@bd-dstodolsky bd-dstodolsky commented Feb 14, 2026

This is the py_wheel counterpart to the analysis-time performance work done
for py_binary/py_test in #3381 and #3442, deferring depset
expansion to execution time. Tested under Bazel 8.5.1 and 9.0.0.

Replace analysis-time depset.to_list() loop with Args.add_all(map_each=...)
and ctx.actions.write(Args), deferring depset expansion to execution time.

The old code built the input file list via string concatenation in a loop,
which is O(n^2) in the total string length. For large py_wheel targets this
dominated analysis time (~18s for ~18k transitive files in a production
monorepo).

Tested under Bazel 8.5.1 and 9.0.0.

Add tests/py_wheel_performance/ with:
- A correctness test verifying wheel contents (100 deps)
- A scaling regression test (tagged manual) asserting analysis time
  grows linearly, not quadratically, with dep count (5k vs 10k deps)

Scaling test results (5k vs 10k deps, best of 3):

  Before: 2825 ms / 8616 ms = 3.05x (quadratic)
  After:   502 ms /  776 ms = 1.55x (linear)
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @bd-dstodolsky, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the performance of py_wheel targets in Bazel by optimizing how large dependency sets are processed. It refactors the _py_wheel_impl rule to defer depset expansion from the analysis phase to the execution phase, eliminating a major bottleneck that caused quadratic scaling with increasing dependencies. This change results in substantial improvements in analysis time, particularly for projects with extensive dependency trees, and is supported by new performance and correctness tests.

Highlights

  • Performance Improvement for py_wheel: Replaced an analysis-time depset.to_list() loop with Args.add_all(map_each=...) in _py_wheel_impl to defer depset expansion to execution time, addressing a significant performance bottleneck.
  • Elimination of Quadratic Scaling: The previous implementation caused near-quadratic scaling for py_wheel targets with large dependency trees due to expensive to_list() and string concatenation during the analysis phase, which is now resolved.
  • Significant Analysis Time Reduction: Benchmarks demonstrate a reduction in analysis time ratio from 3.05x (quadratic) to 1.55x (linear) for 10k vs 5k dependencies, with a reported 18-second improvement for 18k files in a production monorepo.
  • Comprehensive Testing and Validation: New performance tests, correctness verification for wheel contents, and byte-identical output confirmation have been added, along with profiling to validate the effectiveness of the fix.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • CHANGELOG.md
    • (wheel) py_wheel no longer expands the input depset during analysis, improving analysis performance for targets with large dependency trees.
Activity
  • Pull request created by bd-dstodolsky.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 introduces a significant performance improvement to the py_wheel rule by deferring depset expansion to execution time. The implementation correctly replaces an analysis-time depset.to_list() loop with Args.add_all(map_each=...), which is the recommended approach for handling large dependency sets in Bazel. The change is well-supported by a comprehensive new test suite that includes both correctness and scaling performance tests, ensuring the change is both safe and effective. The pull request is well-documented and the changes are excellent.

Copy link
Collaborator

@rickeylev rickeylev left a comment

Choose a reason for hiding this comment

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

Thanks for the optmization, +1 to it in general.

This looks entirely AI generated, so I'm not looking at it particularly deeply.

Trim the PR description. It becomes the commit description, so it needs to be concise and relevant for future people reading through the commit log. If there's information to keep, post it as a comment.

Remove the tests that rely on timing. CI is not a reliable environment for performance tests.

Do not create shell tests that invoke bazel as a subprocess. Such integration tests are expensive and difficult to debug.

If any test is created, it should be an analysis test, using rules_testing, that verifies the args are passed to the action as a manifest file.

I'm fine with no tests here; how the content is passed is an implementation detail, so existing tests should verify correctness.

@bd-dstodolsky
Copy link
Contributor Author

Thanks. Tests removed and comments shortened. Note the performance test was tagged manual so it would not run in CI, but happy to remove; I had originally done it just as a benchmark but then decided to convert to a test, but happy to eliminate. Quick review appreciated!

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@rickeylev rickeylev added this pull request to the merge queue Feb 15, 2026
@rickeylev
Copy link
Collaborator

Thanks, looks good!

@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Feb 15, 2026
@rickeylev rickeylev added this pull request to the merge queue Feb 15, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Feb 15, 2026
@rickeylev rickeylev added this pull request to the merge queue Feb 15, 2026
Merged via the queue into bazel-contrib:main with commit cd34111 Feb 15, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants