-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mitigate slow performance of
findLibrary
(#941)
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
Showing
2 changed files
with
83 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
minimal-plugin/src/main/java/io/micronaut/gradle/internal/VersionCatalogLookupCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |