Skip to content

Commit

Permalink
Use Iterator.remove instead of List copy
Browse files Browse the repository at this point in the history
To point is that when we already know from some paths to be just
regular files, but not directories then at the end we do not need
to check if these are directories. But rather than copying the
list we can use iterators to remove the already discovered files.
  • Loading branch information
Attila Puskas committed Apr 19, 2024
1 parent 9bf48dc commit 3d2ebc2
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/main/java/io/github/classgraph/ClasspathElementDir.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -409,11 +410,13 @@ private void scanPathRecursively(final Path path, final LogNode log) {
// Only scan files in directory if directory is not only an ancestor of an accepted path
if (parentMatchStatus != ScanSpecPathMatch.ANCESTOR_OF_ACCEPTED_PATH) {
// Do preorder traversal (files in dir, then subdirs), to reduce filesystem cache misses
for (final Path subPath : new ArrayList<>(pathsInDir)) {
final Iterator<Path> pathsIterator = pathsInDir.iterator();
while (pathsIterator.hasNext()) {
final Path subPath = pathsIterator.next();
// Process files in dir before recursing
BasicFileAttributes fileAttributes = getFileAttributes.get(subPath);
if (fileAttributes.isRegularFile()) {
pathsInDir.remove(subPath);
pathsIterator.remove();
final Path subPathRelative = classpathEltPath.relativize(subPath);
final String subPathRelativeStr = FastPathResolver.resolve(subPathRelative.toString());
// If this is a modular jar, ignore all classfiles other than "module-info.class" in the
Expand Down Expand Up @@ -452,11 +455,13 @@ private void scanPathRecursively(final Path path, final LogNode log) {
}
} else if (scanSpec.enableClassInfo && dirRelativePathStr.equals("/")) {
// Always check for module descriptor in package root, even if package root isn't in accept
for (final Path subPath : new ArrayList<>(pathsInDir)) {
final Iterator<Path> pathsIterator = pathsInDir.iterator();
while (pathsIterator.hasNext()) {
final Path subPath = pathsIterator.next();
if (subPath.getFileName().toString().equals("module-info.class")) {
BasicFileAttributes fileAttributes = getFileAttributes.get(subPath);
if (fileAttributes.isRegularFile()) {
pathsInDir.remove(subPath);
pathsIterator.remove();
final Resource resource = newResource(subPath, fileAttributes);
addAcceptedResource(resource, parentMatchStatus, /* isClassfileOnly = */ true, subLog);
try {
Expand Down

0 comments on commit 3d2ebc2

Please sign in to comment.