Skip to content

Commit

Permalink
Mitigate slow performance of findLibrary (#941)
Browse files Browse the repository at this point in the history
This commit introduces a cache for the version catalog lookups. It
appears that Gradle's findLibrary method is extremely inefficient,
causing dramatic build time increases, in particular in multiproject
setups.

The fix isn't great though, since it involves a static cache, but
for now there's no better API until Gradle 8.7 is shipped. Even so,
ideally there should be an API to get the library coordinates directly.

See gradle/gradle#27444
See gradle/gradle#25226

Fixes #932
  • Loading branch information
melix authored Feb 7, 2024
1 parent a2409ff commit 91064d6
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ public record AutomaticDependency(
String coordinates,
Optional<ConfigurableVersionProperty> versionProperty
) {

public void applyTo(Project p) {
p.getPlugins().withType(MicronautComponentPlugin.class, unused -> {
p.afterEvaluate(unusedProject -> VersionCatalogLookupCache.get().clear());
var dependencyHandler = p.getDependencies();
var micronautExtension = p.getExtensions().getByType(MicronautExtension.class);
var ignoredDependencies = micronautExtension.getIgnoredAutomaticDependencies();
Expand All @@ -65,13 +67,11 @@ public void applyTo(Project p) {
if (versionCatalogs != null) {
Optional<VersionCatalog> mn = versionCatalogs.find("mn");
if (mn.isPresent()) {
// The cache should ideally use Gradle build services, but that's currently impossible due to
// https://github.com/gradle/gradle/issues/17559
var catalogCache = VersionCatalogLookupCache.get();
VersionCatalog micronautCatalog = mn.get();
Optional<Provider<MinimalExternalModuleDependency>> dependencyProvider = micronautCatalog.getLibraryAliases()
.stream()
.map(micronautCatalog::findLibrary)
.map(Optional::get)
.filter(d -> coordinates.equals(d.get().getModule().toString()))
.findFirst();
Optional<Provider<MinimalExternalModuleDependency>> dependencyProvider = catalogCache.findDependencyFromCatalog(micronautCatalog, coordinates);
if (dependencyProvider.isPresent()) {
return List.of(dependencyProvider.get().get());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2003-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.gradle.internal;

import org.gradle.api.artifacts.MinimalExternalModuleDependency;
import org.gradle.api.artifacts.VersionCatalog;
import org.gradle.api.provider.Provider;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;

/**
* This class is used as a workaround for very poor performance
* of version catalog scanning in Gradle.
* See https://github.com/micronaut-projects/micronaut-gradle-plugin/issues/932
* and https://github.com/gradle/gradle/pull/27444/files
*/
class VersionCatalogLookupCache implements AutoCloseable {
private final AtomicReference<VersionCatalog> oldCatalog = new AtomicReference<>();
private final Map<String, Provider<MinimalExternalModuleDependency>> coordsToDependencyProvider = new ConcurrentHashMap<>();

static VersionCatalogLookupCache get() {
class Holder {
private static final VersionCatalogLookupCache INSTANCE = new VersionCatalogLookupCache();
}
return Holder.INSTANCE;
}

private VersionCatalogLookupCache() {

}

void clear() {
oldCatalog.set(null);
coordsToDependencyProvider.clear();
}

private void memoize(VersionCatalog catalog) {
if (oldCatalog.compareAndSet(null, catalog)) {
coordsToDependencyProvider.clear();
catalog.getLibraryAliases()
.stream()
.parallel()
.forEach(alias -> {
var library = catalog.findLibrary(alias);
library.ifPresent(lib ->
coordsToDependencyProvider.put(lib.get().getModule().toString(), lib)
);
});
}
}

public Optional<Provider<MinimalExternalModuleDependency>> findDependencyFromCatalog(VersionCatalog catalog, String coordinates) {
memoize(catalog);
return Optional.ofNullable(coordsToDependencyProvider.get(coordinates));
}

@Override
public void close() {
coordsToDependencyProvider.clear();
}
}

0 comments on commit 91064d6

Please sign in to comment.