Skip to content

Commit

Permalink
compiler batch: cache JrtFileSystem in ClasspathJrt.jrtFileSystem #2815
Browse files Browse the repository at this point in the history
to avoid ephemeral memory allocations

#2815
  • Loading branch information
EcljpseB0T authored and jukzi committed Sep 24, 2024
1 parent b8acd33 commit 035c1b3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public FileVisitResult postVisitDirectory(java.nio.file.Path dir, IOException ex
}
if (moduleName == null) {
// Delegate to the boss, even if it means inaccurate error reporting at times
List<String> mods = JRTUtil.getModulesDeclaringPackage(this.file, qualifiedPackageName, moduleName);
List<String> mods = JRTUtil.getModulesDeclaringPackage(this.jrtFileSystem, qualifiedPackageName, moduleName);
return CharOperation.toCharArrays(mods);
}
return singletonModuleNameIf(this.packageCache.contains(qualifiedPackageName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ void acceptModule(ClassFileReader reader, Map<String, IModule> cache) {
public synchronized char[][] getModulesDeclaringPackage(String qualifiedPackageName, String moduleName) {
if (this.jdklevel >= ClassFileConstants.JDK9) {
// Delegate to the boss, even if it means inaccurate error reporting at times
List<String> mods = JRTUtil.getModulesDeclaringPackage(this.file, qualifiedPackageName, moduleName);
List<String> mods = JRTUtil.getModulesDeclaringPackage(this.jrtFileSystem, qualifiedPackageName, moduleName);
return CharOperation.toCharArrays(mods);
}
if (this.packageCache == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@
import org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.ExternalAnnotationStatus;
import org.eclipse.jdt.internal.compiler.util.CtSym;
import org.eclipse.jdt.internal.compiler.util.JRTUtil;
import org.eclipse.jdt.internal.compiler.util.JrtFileSystem;

@SuppressWarnings({"rawtypes", "unchecked"})
public class ClasspathJrt extends ClasspathLocation implements IMultiModuleEntry {
public final File file;
protected final JrtFileSystem jrtFileSystem;
protected ZipFile annotationZipFile;
protected final boolean closeZipFileAtEnd;
protected static final Map<String, Map<String,IModule>> ModulesCache = new ConcurrentHashMap<>();
Expand All @@ -57,6 +59,11 @@ public ClasspathJrt(File file, boolean closeZipFileAtEnd,
AccessRuleSet accessRuleSet, String destinationPath) {
super(accessRuleSet, destinationPath);
this.file = file;
try {
this.jrtFileSystem= JRTUtil.getJrtSystem(file, null);
} catch (IOException e) {
throw new IllegalStateException("Failed to init packages for " + file, e); //$NON-NLS-1$
}
this.closeZipFileAtEnd = closeZipFileAtEnd;
this.moduleNamesCache = new HashSet<>();
}
Expand All @@ -67,12 +74,12 @@ public List fetchLinkedJars(FileSystem.ClasspathSectionProblemReporter problemRe
}
@Override
public char[][] getModulesDeclaringPackage(String qualifiedPackageName, String moduleName) {
List<String> modules = JRTUtil.getModulesDeclaringPackage(this.file, qualifiedPackageName, moduleName);
List<String> modules = JRTUtil.getModulesDeclaringPackage(this.jrtFileSystem, qualifiedPackageName, moduleName);
return CharOperation.toCharArrays(modules);
}
@Override
public boolean hasCompilationUnit(String qualifiedPackageName, String moduleName) {
return JRTUtil.hasCompilationUnit(this.file, qualifiedPackageName, moduleName);
return JRTUtil.hasCompilationUnit(this.jrtFileSystem, qualifiedPackageName, moduleName);
}
@Override
public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String moduleName, String qualifiedBinaryFileName) {
Expand All @@ -84,7 +91,7 @@ public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageN
return null; // most common case

try {
IBinaryType reader = ClassFileReader.readFromModule(this.file, moduleName, qualifiedBinaryFileName, this.moduleNamesCache::contains);
IBinaryType reader = JRTUtil.getClassfile(this.jrtFileSystem, qualifiedBinaryFileName, moduleName, this.moduleNamesCache::contains);

if (reader != null) {
reader = maybeDecorateForExternalAnnotations(qualifiedBinaryFileName, reader);
Expand Down Expand Up @@ -135,7 +142,7 @@ public char[][][] findTypeNames(final String qualifiedPackageName, final String
final ArrayList answers = new ArrayList();

try {
JRTUtil.walkModuleImage(this.file, new JRTUtil.JrtFileVisitor<java.nio.file.Path>() {
JRTUtil.walkModuleImage(this.jrtFileSystem, new JRTUtil.JrtFileVisitor<java.nio.file.Path>() {

@Override
public FileVisitResult visitPackage(java.nio.file.Path dir, java.nio.file.Path modPath, BasicFileAttributes attrs) throws IOException {
Expand Down Expand Up @@ -209,7 +216,7 @@ public void loadModules() {
Map<String, IModule> cache = ModulesCache.computeIfAbsent(this.file.getPath(), key -> {
HashMap<String,IModule> newCache = new HashMap<>();
try {
org.eclipse.jdt.internal.compiler.util.JRTUtil.walkModuleImage(this.file,
org.eclipse.jdt.internal.compiler.util.JRTUtil.walkModuleImage(this.jrtFileSystem,
new org.eclipse.jdt.internal.compiler.util.JRTUtil.JrtFileVisitor<Path>() {

@Override
Expand All @@ -227,7 +234,7 @@ public FileVisitResult visitFile(Path f, Path mod, BasicFileAttributes attrs)
@Override
public FileVisitResult visitModule(Path p, String name) throws IOException {
try {
ClasspathJrt.this.acceptModule(JRTUtil.getClassfile(ClasspathJrt.this.file, IModule.MODULE_INFO_CLASS, name), newCache);
ClasspathJrt.this.acceptModule(ClasspathJrt.this.jrtFileSystem.getClassfile(IModule.MODULE_INFO_CLASS, name), newCache);
} catch (ClassFormatException e) {
throw new IOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ protected void handleLocations() {
EclipseFileManager efm = (EclipseFileManager) standardJavaFileManager;
@SuppressWarnings("resource") // XXX EclipseFileManager should close jrtfs but it looks like standardJavaFileManager is never closed
// Was leaking new JrtFileSystem(classpathJrt.file):
JrtFileSystem jrtfs = efm.getJrtFileSystem(classpathJrt.file);
JrtFileSystem jrtfs = efm.getJrtFileSystem(classpathJrt.file); // XXX use classpathJrt.jrtFileSystem??
efm.locationHandler.newSystemLocation(StandardLocation.SYSTEM_MODULES, jrtfs);
} catch (IOException e) {
String error = "Failed to create JRTFS from " + classpathJrt.file; //$NON-NLS-1$
Expand Down

0 comments on commit 035c1b3

Please sign in to comment.