Skip to content

Commit cb08d62

Browse files
christianscottcopybara-github
authored andcommitted
DigestUtils: avoid throwing on invalid digest function name
9% of samples in a profile of one of our builds were inside the `fillInStackTrace()` method. Collecting the valid names into a hashset avoids needing to construct errors every time an invalid digest function name is passed into this function. Tested with Bazel 6.4.0. Our codebase is not yet compatible with Bazel 7. I have not investigated why this function was receiving so many invalid names. Before: ![Screenshot 2023-12-15 at 2 39 01 pm](https://github.com/bazelbuild/bazel/assets/18002432/be4bd311-ca73-46ec-a06d-93bb0ca9c6ba) After: ![Screenshot 2023-12-15 at 2 43 10 pm](https://github.com/bazelbuild/bazel/assets/18002432/64b15739-538f-4752-aafd-6b2c94886595) My understanding is that this will not speed up builds directly, but it will allow BEP events to be processed more quickly. Closes #20574. PiperOrigin-RevId: 592094151 Change-Id: Ie23241c9ec40e59ba2aac1fc83e4830340260f45
1 parent fcdbab8 commit cb08d62

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

src/main/java/com/google/devtools/build/lib/remote/util/DigestUtil.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
// limitations under the License.
1414
package com.google.devtools.build.lib.remote.util;
1515

16+
import static com.google.common.collect.ImmutableSet.toImmutableSet;
1617
import static java.nio.charset.StandardCharsets.UTF_8;
1718

1819
import build.bazel.remote.execution.v2.Action;
1920
import build.bazel.remote.execution.v2.Digest;
2021
import build.bazel.remote.execution.v2.DigestFunction;
22+
import com.google.common.collect.ImmutableSet;
2123
import com.google.common.hash.HashCode;
2224
import com.google.common.io.BaseEncoding;
2325
import com.google.devtools.build.lib.actions.cache.VirtualActionInput;
@@ -30,6 +32,7 @@
3032
import java.io.ByteArrayOutputStream;
3133
import java.io.IOException;
3234
import java.io.OutputStream;
35+
import java.util.Arrays;
3336

3437
/** Utility methods to work with {@link Digest}. */
3538
public class DigestUtil {
@@ -43,12 +46,13 @@ public DigestUtil(XattrProvider xattrProvider, DigestHashFunction hashFn) {
4346
this.digestFunction = getDigestFunctionFromHashFunction(hashFn);
4447
}
4548

49+
private static final ImmutableSet<String> DIGEST_FUNCTION_NAMES =
50+
Arrays.stream(DigestFunction.Value.values()).map(Enum::name).collect(toImmutableSet());
51+
4652
private static DigestFunction.Value getDigestFunctionFromHashFunction(DigestHashFunction hashFn) {
4753
for (String name : hashFn.getNames()) {
48-
try {
54+
if (DIGEST_FUNCTION_NAMES.contains(name)) {
4955
return DigestFunction.Value.valueOf(name);
50-
} catch (IllegalArgumentException e) {
51-
// continue.
5256
}
5357
}
5458
return DigestFunction.Value.UNKNOWN;

0 commit comments

Comments
 (0)