Skip to content

Commit

Permalink
[6.4.0] Download BazelRegistryJson only once per registry (#19300)
Browse files Browse the repository at this point in the history
By caching `BazelRegistryJson` in `IndexRegistry` and caching
`IndexRegistry` instances per registry URL, `bazel_registry.json` is
only downloaded once per registry instead of once for each module in the
final dependency graph in `computeFinalDepGraph`.

On my local machine, this shaves 4s off of the time spent on module
resolution for Bazel itself.

Closes #19292.

Commit
8337dd7

PiperOrigin-RevId: 558940780
Change-Id: I89b03a4c246b10f39b89a79852c922a6504f00bf

Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
  • Loading branch information
bazel-io and fmeum authored Aug 22, 2023
1 parent 443cbcb commit 6c231ff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/events",
"//src/main/java/com/google/devtools/build/lib/util:os",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//third_party:caffeine",
"//third_party:gson",
"//third_party:guava",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class IndexRegistry implements Registry {
private final DownloadManager downloadManager;
private final Map<String, String> clientEnv;
private final Gson gson;
private volatile Optional<BazelRegistryJson> bazelRegistryJson;

public IndexRegistry(URI uri, DownloadManager downloadManager, Map<String, String> clientEnv) {
this.uri = uri;
Expand Down Expand Up @@ -140,9 +141,6 @@ private <T> Optional<T> grabJson(String url, Class<T> klass, ExtendedEventHandle
public RepoSpec getRepoSpec(
ModuleKey key, RepositoryName repoName, ExtendedEventHandler eventHandler)
throws IOException, InterruptedException {
Optional<BazelRegistryJson> bazelRegistryJson =
grabJson(
constructUrl(getUrl(), "bazel_registry.json"), BazelRegistryJson.class, eventHandler);
Optional<SourceJson> sourceJson =
grabJson(
constructUrl(
Expand All @@ -157,14 +155,32 @@ public RepoSpec getRepoSpec(
String type = sourceJson.get().type;
switch (type) {
case "archive":
return createArchiveRepoSpec(sourceJson, bazelRegistryJson, key, repoName);
return createArchiveRepoSpec(sourceJson, getBazelRegistryJson(eventHandler), key, repoName);
case "local_path":
return createLocalPathRepoSpec(sourceJson, bazelRegistryJson, key, repoName);
return createLocalPathRepoSpec(
sourceJson, getBazelRegistryJson(eventHandler), key, repoName);
default:
throw new IOException(String.format("Invalid source type for module %s", key));
}
}

@SuppressWarnings("OptionalAssignedToNull")
private Optional<BazelRegistryJson> getBazelRegistryJson(ExtendedEventHandler eventHandler)
throws IOException, InterruptedException {
if (bazelRegistryJson == null) {
synchronized (this) {
if (bazelRegistryJson == null) {
bazelRegistryJson =
grabJson(
constructUrl(getUrl(), "bazel_registry.json"),
BazelRegistryJson.class,
eventHandler);
}
}
}
return bazelRegistryJson;
}

private RepoSpec createLocalPathRepoSpec(
Optional<SourceJson> sourceJson,
Optional<BazelRegistryJson> bazelRegistryJson,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package com.google.devtools.build.lib.bazel.bzlmod;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.devtools.build.lib.bazel.repository.downloader.DownloadManager;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -25,6 +27,7 @@
public class RegistryFactoryImpl implements RegistryFactory {
private final DownloadManager downloadManager;
private final Supplier<Map<String, String>> clientEnvironmentSupplier;
private final Cache<String, Registry> registries = Caffeine.newBuilder().build();

public RegistryFactoryImpl(
DownloadManager downloadManager, Supplier<Map<String, String>> clientEnvironmentSupplier) {
Expand All @@ -43,7 +46,9 @@ public Registry getRegistryWithUrl(String url) throws URISyntaxException {
case "http":
case "https":
case "file":
return new IndexRegistry(uri, downloadManager, clientEnvironmentSupplier.get());
return registries.get(
url,
unused -> new IndexRegistry(uri, downloadManager, clientEnvironmentSupplier.get()));
default:
throw new URISyntaxException(uri.toString(), "Unrecognized registry URL protocol");
}
Expand Down

0 comments on commit 6c231ff

Please sign in to comment.