From af7b876e2850666925233d4e44b0d9ec961993c8 Mon Sep 17 00:00:00 2001 From: Christian Scott Date: Mon, 18 Dec 2023 21:01:23 -0800 Subject: [PATCH] DigestUtils: avoid throwing on invalid digest function name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../devtools/build/lib/remote/util/DigestUtil.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/remote/util/DigestUtil.java b/src/main/java/com/google/devtools/build/lib/remote/util/DigestUtil.java index df905ff4f4f645..271b3f534b782d 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/util/DigestUtil.java +++ b/src/main/java/com/google/devtools/build/lib/remote/util/DigestUtil.java @@ -13,11 +13,13 @@ // limitations under the License. package com.google.devtools.build.lib.remote.util; +import static com.google.common.collect.ImmutableSet.toImmutableSet; import static java.nio.charset.StandardCharsets.UTF_8; import build.bazel.remote.execution.v2.Action; import build.bazel.remote.execution.v2.Digest; import build.bazel.remote.execution.v2.DigestFunction; +import com.google.common.collect.ImmutableSet; import com.google.common.hash.HashCode; import com.google.common.io.BaseEncoding; import com.google.devtools.build.lib.actions.cache.VirtualActionInput; @@ -30,6 +32,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; /** Utility methods to work with {@link Digest}. */ public class DigestUtil { @@ -43,12 +46,13 @@ public DigestUtil(XattrProvider xattrProvider, DigestHashFunction hashFn) { this.digestFunction = getDigestFunctionFromHashFunction(hashFn); } + private static final ImmutableSet DIGEST_FUNCTION_NAMES = + Arrays.stream(DigestFunction.Value.values()).map(Enum::name).collect(toImmutableSet()); + private static DigestFunction.Value getDigestFunctionFromHashFunction(DigestHashFunction hashFn) { for (String name : hashFn.getNames()) { - try { + if (DIGEST_FUNCTION_NAMES.contains(name)) { return DigestFunction.Value.valueOf(name); - } catch (IllegalArgumentException e) { - // continue. } } return DigestFunction.Value.UNKNOWN;