Memoize per-fragment checksum digests across clones #31377
In our large builds this is a 9-10% reduction in total bytes allocated and ~2.5% reduction in analysis time
Is there any chance that we can move optionsToCacheKey to a method on FragmentOptions/OptionsBase, then make the cacheKeyDigest field private?
This is what https://github.com/bazelbuild/bazel/pull/31231/changes is doing.
final
Why do we need this method in addition to the one that returns byte[]? I don't see any callers outside of tests. It would be much simpler to not expose both a cached and uncached method.
Looking good, just one more remaining comment.
While doing this, I realized that instead of building all these strings, we could just fingerprint directly. That also resolved a bunch of cases in which Collection.toString() didn't distinguish different contents.
Is the +1 to differentiate from a non-collection with the same string representation? If that's the case, I think it's more clear to leave off the +1 and use -1 for the non-collection case.
That's a good point. I'm not sure why it was done with the temporary string, but if it meaningfully contributed to performance we would have seen it in a profile by now. Perhaps there's some open source builds where there are way more configurations.
There's probably also a way to avoid the temporary map if you're interested in more savings.
Approved these changes.
There's probably also a way to avoid the temporary map if you're interested in more savings.
Certainly, but the toMap function has other problematic callers that I will clean up in a follow-up (including one that constructs the map in a loop just to get a particular option from it).
Approved these changes.
justinhorvitz
fmeum
Description
BuildOptions#equalsand#hashCodeare based onchecksum(), which serializes every option of every native fragment into a string and hashes it. Transitions derive newBuildOptionsinstances viaclone()ortoBuilder(), which deep-clone all fragments, so interning the result recomputes the full checksum even if only a single option changed or the result equals an existing configuration. For Starlark transitions, this dominated the cost of a transition cache miss.This PR memoizes the digest of each fragment's cache key on the
FragmentOptionsinstance and hasBuildOptions#checksumcombine these digests instead of the full cache keys. Clones inherit the memoized digest and any change to an option value resets it, so a checksum only rehashes the fragments that were actually changed:OptionsBase#onOptionChanged()is called by the generated setters (if the new value isn't identical to the old one) and byOptionDefinition#setValue, which are the only ways to change the private fields of a generated options class.FragmentOptionsresets its memoized digest in it.Note that this changes the values of configuration checksums.
Motivation
Microbenchmark based on
BuildViewTestCase(14 fragments, 257 native options) that applies a Starlark rule transition with an emptyStarlarkTransitionCacheand computes the checksum of the result:| | Before | After |
|---|---:|---:|
| No-op transition (Starlark flag set to its default) | 57 µs, 186 KB | 10.9 µs, 24 KB |
| Transition changing a Starlark flag | 58 µs, 187 KB | 11.2 µs, 25 KB |
| Transition setting a native flag to its current value | 62 µs, 189 KB | 12.6 µs, 27 KB |
| Transition changing
--compilation_mode| 61 µs, 193 KB | 23.3 µs, 68 KB ||
BuildOptions#clone().checksum()| 31.6 µs, 144 KB | 0.8 µs, 4 KB |Analysis of a large real-world project, mean change compared to a baseline without this PR:
| Metric | Mean change |
|---|---:|
| Java allocations | −72.98 GB (−9.37%, from 778.91 GB) |
| Analysis time | −3.40 s (−2.35%) |
| Peak RSS | +11 MB (effectively flat) |
| Retained heap | −4.15 MB (effectively flat) |
Build API Changes
No
Release Notes
RELNOTES: None