Skip to content

Avoid a quadratic excludes check in pkg_files - #1088

Open
fmeum wants to merge 1 commit into
bazelbuild:mainfrom
fmeum:pkg-files-excludes-dict
Open

fmeum wants to merge 1 commit into
bazelbuild:mainfrom
fmeum:pkg-files-excludes-dict

Conversation

@fmeum

@fmeum fmeum commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

f not in ctx.files.excludes scans the list of excluded files for every source file, so analyzing a pkg_files target takes time proportional to the product of the number of sources and excludes. This change checks against a dict built once from the excludes instead.

Bazel's own //:dist-srcs has 14,411 sources and 8,807 excludes, which amounts to about 127M File comparisons on a single thread at the end of analysis. With this change, the cold analysis wall time of bazel build --nobuild //src/... in the Bazel repo drops from 14.40s to 13.92s on average (n=6, --lockfile_mode=off).

`f not in ctx.files.excludes` scans the list of excluded files for every
source file, so analyzing a `pkg_files` target takes time proportional
to the product of the number of sources and excludes.

Bazel's own `//:dist-srcs` has 14,411 sources and 8,807 excludes, which
amounts to about 127M `File` comparisons on a single thread at the end
of analysis. Checking against a dict instead reduces the cold analysis
wall time of `bazel build --nobuild //src/...` in the Bazel repo from
14.40s to 13.92s on average (n=6, `--lockfile_mode=off`).
@fmeum
fmeum marked this pull request as ready for review October 17, 2026 19:12
@fmeum
fmeum requested review from aiuto and cgrindel as code owners October 17, 2026 19:12
@fmeum

fmeum commented Oct 17, 2026

Copy link
Copy Markdown
Contributor Author

FYI @meisterT

@fmeum

fmeum commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Could you cut a release with the fix? This can go up to 10% of Bazel's analysis time.

@cgrindel

Copy link
Copy Markdown
Collaborator

LGTM. I left it unmerged so that @aiuto can take a look.

This branch has not been deployed

No deployments
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