performance: consult the remote repository filter only on a local repository miss (#2380)


## Summary

`DefaultArtifactResolver.resolve()` calls `RemoteRepositoryFilter.acceptArtifact()` for every remote repository of
a request before it checks the workspace and the local repository. Resolver 2.x enables the `prefixes` and
`groupId` filter sources by default, so this runs in every default Maven build. In a large build nearly every
request is a local hit, which means one filter call per remote repository for an artifact that's already on disk.

On a 1667-module project this change removes about 26 % of the allocation of dependency resolution and 7 % of its
wall time with a warm local repository.

This changes observable behaviour (see below). I asked about it on dev@maven.apache.org first and haven't had an
answer yet, so I'm opening the PR to make the discussion concrete.

## Change

Only release versions are affected. Snapshots, `RELEASE` and `LATEST` keep the current order, because their
version resolution reads metadata from the accepted repositories. For a release version:

- The workspace check and the local repository lookup move ahead of the filter. That early lookup uses all
  repositories of the request, not only the accepted ones.
- The filter then runs once: it checks that the repository the local copy came from is enabled and accepted. A
  locally installed artifact has no such repository, so it gets no filter call.
- Version resolution doesn't run when the artifact resolves from the workspace or the local repository.
- If the filter rejects the local copy's repository, the previous path runs unchanged. On a local miss it runs
  too, but it reuses the result of the first lookup, so a miss still costs one `LocalRepositoryManager.find()`.

## Behaviour

What stays the same: the resolved file, the repository reported in `ArtifactResult`, and the rule that a local
copy cached from a repository the filter rejects is not used.

What changes:

- An artifact that resolves from the workspace or the local repository no longer carries an
  `ArtifactFilteredOutException` for each rejected repository. Nothing was contacted for it.
- `VersionResolver.resolveVersion()` isn't called for such an artifact. Maven's `VersionResolver`
  (`maven-resolver-provider`) returns a plain release version unchanged and reads no metadata for it, so the call
  had no effect there. A custom implementation could behave differently. I don't know of one; neither Maven nor the
  IntelliJ IDEA Maven server replaces it.

## Tests

- `testResolveRemoteArtifactSkipsFilterForDisabledRepositoryPolicy` asserted that the local lookup gets the
  filtered repository list. The lookup now runs first, so I removed that assertion. The test keeps its main check
  (no `ArtifactFilteredOutException` for a repository with a disabled policy) and still checks that a local miss
  costs one lookup.
- New `testResolveLocalArtifactFromAcceptedRepositoryRecordsNoFilteredOutRepositories`: a copy cached from an
  accepted repository resolves without a download and without `ArtifactFilteredOutException`. It fails on master;
  that's the behaviour change.
- New `testResolveLocalArtifactFromRejectedRepositoryIsNotUsed`: a copy cached from a rejected repository isn't
  used, and the result has an `ArtifactFilteredOutException`. It passes on master too, so this guarantee is kept.

`mvn -Prun-its verify`: 2779 tests, 0 failures, 147 skipped; invoker IT passed.

## Benchmark

- Maven 3.10.2 with Resolver master `3bfffc10`, without and with the change. Only the Resolver jars differ.
- A synthetic project with 1667 modules and about 475 declared dependencies each. settings.xml declares 10 remote
  repositories; the dependency POMs add more.
- A core extension (`-Dmaven.ext.class.path`) resolves the dependencies of every reactor project with
  `ProjectDependenciesResolver.resolve(new DefaultDependencyResolutionRequest(project, session))` on 12 threads,
  then Maven runs `validate`. No plugin runs, nothing compiles. The extension reports the wall time, and the CPU
  time and allocated bytes of the 12 resolver threads (`com.sun.management.ThreadMXBean`). Young GC is what young
  and mixed collections reclaimed, from `-Xlog:gc`. Heap `-Xmx16g`.
- 2,535,614 artifact resolutions per run. The local repository holds 1,516 jars and 4,771 POMs (1.1 GB).
- Warm: each run starts from a copy of the same filled local repository. Clean: each run starts from an empty
  local repository, so its wall time includes the download of 1.1 GB.
- Median of 3 interleaved runs on an Apple M-series laptop. In brackets: the difference to master.

Warm local repository:

| metric | master | this change | this change + #2379 |
|---|---|---|---|
| allocated, GB | 403.7 | 299.0 (−25.9 %) | 296.4 (−26.6 %) |
| young GC, GB | 425.8 | 319.7 (−24.9 %) | 317.8 (−25.4 %) |
| GC pauses | 183 | 168 (−8.2 %) | 172 (−6.0 %) |
| GC pause time, s | 2.8 | 2.6 (−7.1 %) | 2.6 (−7.1 %) |
| CPU, s | 864 | 799 (−7.5 %) | 821 (−5.0 %) |
| wall, s | 113 | 105 (−7.1 %) | 105 (−7.1 %) |

Clean local repository:

| metric | master | this change | this change + #2379 |
|---|---|---|---|
| allocated, GB | 404.2 | 302.2 (−25.2 %) | 300.2 (−25.7 %) |
| young GC, GB | 429.5 | 326.2 (−24.1 %) | 325.1 (−24.3 %) |
| GC pauses | 194 | 175 (−9.8 %) | 169 (−12.9 %) |
| GC pause time, s | 2.8 | 2.6 (−7.1 %) | 2.6 (−7.1 %) |
| CPU, s | 853 | 803 (−5.9 %) | 850 (−0.4 %) |
| wall, s | 384 | 378 (−1.6 %) | 409 (+6.5 %) |

- Allocation is the reliable number: it varies by less than 1.5 GB between runs of one variant.
- CPU and wall time vary by 40–60 s between runs of one variant. With a warm repository the ranges don't overlap:
  master 110–115 s, this change 99–109 s. With a clean repository the download dominates the wall time, so the
  difference is within the noise.
- Every run resolved the same number of artifacts.
- #2379 removes the per-call allocation of `DefaultRemoteRepositoryFilterManager` and doesn't change behaviour.
  With this change the filter runs only on local misses, so #2379 adds little on top; on its own it saves 5–6 %.
- The benchmark ran on an earlier revision of this change, which did a second local lookup on a miss. Even with a
  clean repository a miss happens only once per unique artifact (about 6,300 of 2.5M lookups, 0.25 %), so I don't
  expect that to move these numbers.

I haven't measured this change in the IntelliJ IDEA import test yet.
3 files changed
tree: 59b61c21ccb3790dd401f599e631f592e1965f1b
  1. .github/
  2. .mvn/
  3. maven-resolver-api/
  4. maven-resolver-connector-basic/
  5. maven-resolver-demos/
  6. maven-resolver-generator-gnupg/
  7. maven-resolver-generator-sigstore/
  8. maven-resolver-impl/
  9. maven-resolver-named-locks/
  10. maven-resolver-named-locks-hazelcast/
  11. maven-resolver-named-locks-ipc/
  12. maven-resolver-named-locks-redisson/
  13. maven-resolver-spi/
  14. maven-resolver-supplier-mvn3/
  15. maven-resolver-supplier-mvn4/
  16. maven-resolver-test-http/
  17. maven-resolver-test-util/
  18. maven-resolver-tools/
  19. maven-resolver-transport-apache/
  20. maven-resolver-transport-classpath/
  21. maven-resolver-transport-file/
  22. maven-resolver-transport-jdk-parent/
  23. maven-resolver-transport-jetty/
  24. maven-resolver-transport-minio/
  25. maven-resolver-transport-url/
  26. maven-resolver-transport-wagon/
  27. maven-resolver-util/
  28. src/
  29. .asf.yaml
  30. .git-blame-ignore-revs
  31. .gitignore
  32. AGENTS.md
  33. class-overview.svg
  34. Jenkinsfile
  35. LICENSE
  36. pom.xml
  37. README.md
  38. SECURITY.md
README.md

Contributing to Apache Maven Artifact Resolver

Apache License, Version 2.0, January 2004 Maven Central Reproducible Builds Jenkins Status Jenkins tests

You have found a bug, or you have an idea for a cool new feature? Contributing code is a great way to give something back to the open source community. Before you dig right into the code, there are a few guidelines that we need contributors to follow so that we can have a chance of keeping on top of things.

Getting Started

  • Make sure you have a GitHub account.
  • If you‘re planning to implement a new feature, it makes sense to discuss your changes on the dev list first. This way you can make sure you’re not wasting your time on something that isn‘t considered to be in Apache Maven’s scope.
  • Submit a ticket for your issue, assuming one does not already exist.
    • Clearly describe the issue, including steps to reproduce when it is a bug.
    • Make sure you fill in the earliest version that you know has the issue.
  • Fork the repository on GitHub.

Making and Submitting Changes

We accept Pull Requests via GitHub. The developer mailing list is the main channel of communication for contributors. There are some guidelines which will make applying PRs easier for us:

  • Create a topic branch from where you want to base your work (this is usually the master branch). Push your changes to a topic branch in your fork of the repository.
  • Make commits of logical units.
  • Respect the original code style: by using the same codestyle, patches should only highlight the actual difference, not being disturbed by any formatting issues:
    • Only use spaces for indentation.
    • Create minimal diffs - disable on save actions like reformat source code or organize imports. If you feel the source code should be reformatted, create a separate PR for this change.
    • Check for unnecessary whitespace with git diff --check before committing.
  • Make sure you have added the necessary tests (JUnit/IT) for your changes.
  • Run all the tests with mvn -Prun-its verify to assure nothing else was accidentally broken.
  • Submit a pull request to the repository in the Apache organization.

If you plan to contribute on a regular basis, please consider filing a contributor license agreement.

Additional Resources