Skip to content

Add note about ctx.files memory impact#28753

Open
hauserx wants to merge 1 commit intobazelbuild:masterfrom
hauserx:ctx-files-potentially-mem-intensive
Open

Add note about ctx.files memory impact#28753
hauserx wants to merge 1 commit intobazelbuild:masterfrom
hauserx:ctx-files-potentially-mem-intensive

Conversation

@hauserx
Copy link
Contributor

@hauserx hauserx commented Feb 23, 2026

Description

Usage of ctx.files in rules can be memory heavy in case of a lot of inputs. According to current documentation using ctx.files is shortcut for

[f for t in ctx.attr.<ATTR> for f in t.files]

and indeed when creating some syntetic benchmarks noticed high memory usage that could be explained by file depset being eagerly resolved.

I have updated documentation with info about it and small sample of creating depset from ctx.attr as an alternative.

I have also found in Kleaf documentation note that also recommends using depsets in all cases but when small number of inputs is guaranteed: https://android.googlesource.com/kernel/build/+/refs/heads/main/kleaf/docs/kleaf_development.md#prefer-depset-over-in-rules

Motivation

When investigating high memory usage for some of our heavy builds (~1M actions) noticed high memory usage in loading/analysis phase on pretty basic synthetic benchmarks. Benchmark is basically 100k-1M small actions, each using the same 1k-10k-100k files. Manipulating number of actions and number of input files shows that around 8 bytes is needed per each occurrence of an input file. So e.g. for 100k actions each using same 10k input files, bazel reports used-heap-size-after-gc: around 8GB after the analysis phase (bazel build --nobuild //...) . Taking heap dumps showed that ~95% of memory is consumed by NestedSets. Using solution described in this PR reduced memory to ~400MB.

Build API Changes

No

Release Notes

RELNOTES: None

@github-actions github-actions bot added the awaiting-review PR is awaiting review from an assigned reviewer label Feb 23, 2026
@fmeum
Copy link
Collaborator

fmeum commented Feb 23, 2026

The ideal solution would be to migrate these to return special lists that only flatten the depset when needed. I don't know whether that's feasible though.

@meisterT meisterT requested a review from brandjon February 24, 2026 15:33
@meisterT meisterT added the team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts label Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-review PR is awaiting review from an assigned reviewer team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants