Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
Improved compilation speed by reusing cached info about classpaths
Browse files Browse the repository at this point in the history
  • Loading branch information
itsaky committed Aug 9, 2022
1 parent a3ce2d0 commit a3ad91a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public interface JarPackageProvider {
/**
* Get the package map from the given archive path.
* @return The cached package entries. Should return null or empty map to walk the archive file tree instead.
* The keys must be <code>com.sun.tools.javac.file.RelativePath.RelativeDirectory</code>
*/
public Map<String, Path> getPackages(Path archivePath);
public Map<? extends Object, Path> getPackages(Path archivePath);
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import java.io.StringWriter;

import static java.nio.file.FileVisitOption.FOLLOW_LINKS;
import java.util.Optional;

import static javax.tools.StandardLocation.*;

Expand Down Expand Up @@ -180,7 +181,7 @@ public void setContext(Context context) {

fsInfo = FSInfo.instance(context);
jarPackageProvider = context.get(JarPackageProvider.class);
if(jarPackageProvider == null) {
if (jarPackageProvider == null) {
jarPackageProvider = archivePath -> Collections.emptyMap();
}

Expand Down Expand Up @@ -338,11 +339,27 @@ synchronized Container getContainer(Path path) throws IOException {

BasicFileAttributes attr = null;

try {
attr = Files.readAttributes(realPath, BasicFileAttributes.class);
} catch (IOException ex) {
//non-existing
fs = MISSING_CONTAINER;
// AndroidIDE changed: Prefer cached attributes as computing file attributes is a bit expensive
// 'else' part is mostly not reached in AndroidIDE
if (fsInfo instanceof CacheFSInfo) {
Optional<BasicFileAttributes> attrs = ((CacheFSInfo) fsInfo).getAttributes(realPath);
if (attrs.isPresent()) {
attr = attrs.get();
if (attr == null) {
//non-existing
fs = MISSING_CONTAINER;
}
} else {
//non-existing
fs = MISSING_CONTAINER;
}
} else {
try {
attr = Files.readAttributes(realPath, BasicFileAttributes.class);
} catch (IOException ex) {
//non-existing
fs = MISSING_CONTAINER;
}
}

if (attr != null) {
Expand Down Expand Up @@ -602,22 +619,21 @@ public ArchiveContainer(Path archivePath) throws IOException, ProviderNotFoundEx
FileSystemProvider jarFSProvider = fsInfo.getJarFSProvider();
Assert.checkNonNull(jarFSProvider, "should have been caught before!");
this.fileSystem = jarFSProvider.newFileSystem(archivePath, env);
this.packages = new HashMap<>();


if (jarPackageProvider == null) {
this.packages = new HashMap<>();
walkArchive();
return;
}
final Map<String, Path> cachedPackages = jarPackageProvider.getPackages(archivePath);

final Map<? extends Object, Path> cachedPackages = jarPackageProvider.getPackages(archivePath);
if (cachedPackages == null || cachedPackages.isEmpty()) {
this.packages = new HashMap<>();
walkArchive();
return;
}

for(Map.Entry<String, Path> entry : cachedPackages.entrySet()) {
packages.put(new RelativeDirectory(entry.getKey()), entry.getValue());
}

packages = (Map<RelativeDirectory, Path>) cachedPackages;
}

private void walkArchive() throws IOException {
Expand All @@ -636,7 +652,7 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
});
}
}

private String trace(Throwable err) {
final StringWriter sw = new StringWriter();
err.printStackTrace(new PrintWriter(sw));
Expand Down Expand Up @@ -841,6 +857,11 @@ public Iterable<JavaFileObject> list(Location location,
nullCheck(packageName);
nullCheck(kinds);

if ("ANDROIDIDE_CACHE_LOCATION".equals(packageName)) {
pathsAndContainersByLocationAndRelativeDirectory.computeIfAbsent(location, this::indexPathsAndContainersByRelativeDirectory);
return Collections.emptyList();
}

RelativeDirectory subdirectory = RelativeDirectory.forPackage(packageName);
ListBuffer<JavaFileObject> results = new ListBuffer<>();

Expand Down Expand Up @@ -1285,7 +1306,7 @@ public Path asPath(FileObject file) {
}

/**
* Enforces the specification of a "relative" name as used in null {@linkplain #getFileForInput(Location,String,String)
* Enforces the specification of a "relative" name as used in null null null {@linkplain #getFileForInput(Location,String,String)
* getFileForInput}. This method must follow the rules defined in that
* method, do not make any changes without consulting the specification.
*/
Expand Down Expand Up @@ -1430,7 +1451,7 @@ public File next() {

@Override
public boolean handleOption(Option option, String value) {
if (javacFileManagerOptions.contains(option)) {
if (javacFileManagerOptions.contains(option) && option != Option.MULTIRELEASE) {
pathsAndContainersByLocationAndRelativeDirectory.clear();
nonIndexingContainersByLocation.clear();
}
Expand Down

0 comments on commit a3ad91a

Please sign in to comment.