Revert "Cache ASTParser per project to speed up CU parsing"
This reverts commit cb7621b208.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2017, 2019 Pivotal, Inc.
|
||||
* Copyright (c) 2017, 2018 Pivotal, Inc.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
@@ -15,7 +15,6 @@ import java.net.URI;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
|
||||
@@ -36,7 +35,6 @@ import org.springframework.ide.vscode.commons.java.IJavaProject;
|
||||
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
|
||||
import org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver;
|
||||
import org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService;
|
||||
import org.springframework.ide.vscode.commons.util.text.LanguageId;
|
||||
import org.springframework.ide.vscode.commons.util.text.TextDocument;
|
||||
|
||||
import com.google.common.cache.Cache;
|
||||
@@ -51,7 +49,6 @@ public final class CompilationUnitCache implements DocumentContentProvider {
|
||||
private ProjectObserver projectObserver;
|
||||
private Cache<URI, CompilationUnit> uriToCu;
|
||||
private Cache<IJavaProject, Set<URI>> projectToDocs;
|
||||
private Cache<IJavaProject, ASTParser> parsers;
|
||||
private ProjectObserver.Listener projectListener;
|
||||
private SimpleTextDocumentService documents;
|
||||
|
||||
@@ -66,7 +63,6 @@ public final class CompilationUnitCache implements DocumentContentProvider {
|
||||
|
||||
// PT 154618835 - Avoid retaining the CU in the cache as it consumes memory if it hasn't been
|
||||
// accessed after some time
|
||||
parsers = CacheBuilder.newBuilder().build();
|
||||
uriToCu = CacheBuilder.newBuilder()
|
||||
.expireAfterWrite(CU_ACCESS_EXPIRATION, TimeUnit.MINUTES)
|
||||
.build();
|
||||
@@ -77,9 +73,8 @@ public final class CompilationUnitCache implements DocumentContentProvider {
|
||||
writeLock = lock.writeLock();
|
||||
|
||||
if (documents != null) {
|
||||
documents.onDidOpen(this::handleDocOpened);
|
||||
documents.onDidChangeContent(change -> handleDocChanged(change.getDocument()));
|
||||
documents.onDidClose(this::handleDocClosed);
|
||||
documents.onDidChangeContent(doc -> invalidateCuForJavaFile(doc.getDocument().getId().getUri()));
|
||||
documents.onDidClose(doc -> invalidateCuForJavaFile(doc.getId().getUri()));
|
||||
}
|
||||
|
||||
if (this.projectObserver != null) {
|
||||
@@ -116,9 +111,7 @@ public final class CompilationUnitCache implements DocumentContentProvider {
|
||||
|
||||
try {
|
||||
cu = uriToCu.get(uri, () -> {
|
||||
ASTParser parser = parser(project);
|
||||
String uriPath = uri.getPath();
|
||||
CompilationUnit cUnit = parse(parser, fetchContent(uri).toCharArray(), uri.toString(), uriPath.substring(uriPath.lastIndexOf("/")));
|
||||
CompilationUnit cUnit = parse(uri.toString(), fetchContent(uri).toCharArray(), project);
|
||||
projectToDocs.get(project, () -> new HashSet<>()).add(uri);
|
||||
return cUnit;
|
||||
});
|
||||
@@ -145,30 +138,7 @@ public final class CompilationUnitCache implements DocumentContentProvider {
|
||||
|
||||
return requestor.apply(null);
|
||||
}
|
||||
|
||||
private void handleDocChanged(TextDocument doc) {
|
||||
if (LanguageId.JAVA.equals(doc.getLanguageId())) {
|
||||
invalidateCuForJavaFile(doc.getUri());
|
||||
}
|
||||
}
|
||||
|
||||
private void handleDocOpened(TextDocument doc) {
|
||||
}
|
||||
|
||||
private void handleDocClosed(TextDocument doc) {
|
||||
if (LanguageId.JAVA.equals(doc.getLanguageId())) {
|
||||
invalidateCuForJavaFile(doc.getUri());
|
||||
}
|
||||
}
|
||||
|
||||
private ASTParser parser(IJavaProject project) {
|
||||
try {
|
||||
return parsers.get(project, () -> createParser(getClasspathEntries(project)));
|
||||
} catch (ExecutionException e) {
|
||||
logger.error("{}", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void invalidateCuForJavaFile(String uriStr) {
|
||||
URI uri = URI.create(uriStr);
|
||||
@@ -180,21 +150,21 @@ public final class CompilationUnitCache implements DocumentContentProvider {
|
||||
}
|
||||
}
|
||||
|
||||
public static CompilationUnit parse(TextDocument document, IJavaProject project) throws Exception {
|
||||
String[] classpathEntries = getClasspathEntries(project);
|
||||
String docURI = document.getUri();
|
||||
String unitName = docURI.substring(docURI.lastIndexOf("/"));
|
||||
char[] source = document.get(0, document.getLength()).toCharArray();
|
||||
return parse(source, docURI, unitName, classpathEntries);
|
||||
}
|
||||
|
||||
public static CompilationUnit parse(String uri, char[] source, IJavaProject project) throws Exception {
|
||||
String[] classpathEntries = getClasspathEntries(project);
|
||||
String unitName = uri.substring(uri.lastIndexOf("/"));
|
||||
return parse(source, uri, unitName, classpathEntries);
|
||||
}
|
||||
|
||||
public static CompilationUnit parse(char[] source, String docURI, String unitName, String[] classpathEntries) throws Exception {
|
||||
ASTParser parser = createParser(classpathEntries);
|
||||
return parse(parser, source, docURI, unitName);
|
||||
}
|
||||
|
||||
public static CompilationUnit parse(ASTParser parser, char[] source, String docURI, String unitName) throws Exception {
|
||||
parser.setUnitName(unitName);
|
||||
parser.setSource(source);
|
||||
|
||||
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
|
||||
|
||||
return cu;
|
||||
}
|
||||
|
||||
public static ASTParser createParser(String[] classpathEntries) {
|
||||
ASTParser parser = ASTParser.newParser(AST.JLS11);
|
||||
Map<String, String> options = JavaCore.getOptions();
|
||||
JavaCore.setComplianceOptions(JavaCore.VERSION_11, options);
|
||||
@@ -206,7 +176,13 @@ public final class CompilationUnitCache implements DocumentContentProvider {
|
||||
|
||||
String[] sourceEntries = new String[] {};
|
||||
parser.setEnvironment(classpathEntries, sourceEntries, null, false);
|
||||
return parser;
|
||||
|
||||
parser.setUnitName(unitName);
|
||||
parser.setSource(source);
|
||||
|
||||
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
|
||||
|
||||
return cu;
|
||||
}
|
||||
|
||||
private static String[] getClasspathEntries(IJavaProject project) throws Exception {
|
||||
@@ -228,7 +204,6 @@ public final class CompilationUnitCache implements DocumentContentProvider {
|
||||
try {
|
||||
uriToCu.invalidateAll(docUris);
|
||||
projectToDocs.invalidate(project);
|
||||
parsers.invalidate(project);
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user