Skip to content

Commit

Permalink
Performance: cache JARs during UI Operations- jdt.core#1614
Browse files Browse the repository at this point in the history
Improves performance of Call-Hierarchy, Type-Hierarchy, Hover, ...

eclipse-jdt/eclipse.jdt.core#1614
  • Loading branch information
EcljpseB0T authored and jukzi committed Dec 12, 2023
1 parent a88cfbb commit fc6a25a
Show file tree
Hide file tree
Showing 27 changed files with 163 additions and 58 deletions.
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;

/**
Expand Down Expand Up @@ -87,8 +88,13 @@ public class JavaElementPropertyTester extends PropertyTester {
public static final String PROJECT_OPTION = "projectOption"; //$NON-NLS-1$


@SuppressWarnings("boxing")
@Override
public boolean test(Object receiver, String method, Object[] args, Object expectedValue) {
return JavaCore.callReadOnly(() -> testCached(receiver, method, args, expectedValue));
}

private boolean testCached(Object receiver, String method, Object[] args, Object expectedValue) {
if (!(receiver instanceof IJavaElement)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.jdt.ui/META-INF/MANIFEST.MF
Expand Up @@ -97,7 +97,7 @@ Require-Bundle:
org.eclipse.core.filesystem;bundle-version="[1.2.0,2.0.0)",
org.eclipse.core.resources;bundle-version="[3.14.0,4.0.0)",
org.eclipse.core.variables;bundle-version="[3.2.200,4.0.0)",
org.eclipse.jdt.core;bundle-version="[3.36.0,4.0.0)",
org.eclipse.jdt.core;bundle-version="[3.37.0,4.0.0)",
org.eclipse.search;bundle-version="[3.10.0,4.0.0)",
org.eclipse.debug.core;bundle-version="[3.10.0,4.0.0)",
org.eclipse.debug.ui;bundle-version="[3.11.0,4.0.0)",
Expand Down
Expand Up @@ -38,6 +38,7 @@
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;

import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
Expand Down Expand Up @@ -249,7 +250,7 @@ final class CodeResolveRunnable implements IRunnableWithProgress {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException {
try {
result= codeResolve(input, selection);
result= JavaCore.callReadOnly(() -> codeResolve(input, selection));
} catch (JavaModelException e) {
throw new InvocationTargetException(e);
}
Expand Down
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.Widget;

import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.MenuManager;
Expand All @@ -30,6 +31,8 @@

import org.eclipse.ui.IWorkbenchPartSite;

import org.eclipse.jdt.core.JavaCore;

import org.eclipse.jdt.internal.corext.callhierarchy.CallerMethodWrapper;
import org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper;

Expand Down Expand Up @@ -197,4 +200,10 @@ void expandConstructorNode() {
fConstructorToExpand= null;
}
}

@Override
protected void internalAdd(Widget widget, Object parentElement, Object[] childElements) {
JavaCore.runReadOnly(() -> super.internalAdd(widget, parentElement, childElements));
}

}
Expand Up @@ -23,6 +23,8 @@
import org.eclipse.ui.progress.IDeferredWorkbenchAdapter;
import org.eclipse.ui.progress.IElementCollector;

import org.eclipse.jdt.core.JavaCore;

import org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper;

import org.eclipse.jdt.internal.ui.JavaPlugin;
Expand Down Expand Up @@ -75,7 +77,7 @@ public void fetchDeferredChildren(Object object, IElementCollector collector, IP
final DeferredMethodWrapper deferredMethodWrapper= (DeferredMethodWrapper)object;
try {
fProvider.startFetching();
collector.add((Object[]) deferredMethodWrapper.getCalls(monitor), monitor);
JavaCore.runReadOnly(() -> collector.add((Object[]) deferredMethodWrapper.getCalls(monitor), monitor));
collector.done();
} catch (OperationCanceledException e) {
final MethodWrapper methodWrapper= deferredMethodWrapper.getMethodWrapper();
Expand Down
Expand Up @@ -438,7 +438,7 @@ public int open() {
}
}
}
return super.open();
return JavaCore.callReadOnly(super::open).intValue();
}

/**
Expand Down
Expand Up @@ -60,8 +60,10 @@

import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchCommandConstants;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionContext;
import org.eclipse.ui.actions.ActionGroup;
Expand Down Expand Up @@ -649,11 +651,18 @@ protected IEditorInput transformEditorInput(IEditorInput input) {
return input;
}

@Override
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
JavaCore.runReadOnly(() -> super.init(site, input));
}
/*
* @see AbstractTextEditor#doSetInput(IEditorInput)
*/
@Override
protected void doSetInput(IEditorInput input) throws CoreException {
JavaCore.runReadOnly(() -> doSetInputCached(input));
}
private void doSetInputCached(IEditorInput input) throws CoreException {
uninstallOccurrencesFinder();

input= transformEditorInput(input);
Expand Down Expand Up @@ -736,11 +745,19 @@ protected IStatus run(IProgressMonitor monitor) {
job.schedule();
}

@Override
protected void selectionChanged() {
JavaCore.runReadOnly(() -> super.selectionChanged());
}
/*
* @see IWorkbenchPart#createPartControl(Composite)
*/
@Override
public void createPartControl(Composite parent) {
JavaCore.runReadOnly(() -> createPartControlCached(parent));
}

private void createPartControlCached(Composite parent) {
fParent= new Composite(parent, SWT.NONE);
fStackLayout= new StackLayout();
fParent.setLayout(fStackLayout);
Expand Down Expand Up @@ -949,7 +966,7 @@ public void dispose() {
*/
@Override
public void setFocus() {
super.setFocus();
JavaCore.runReadOnly(super::setFocus);

if (fSourceAttachmentForm != null && !fSourceAttachmentForm.isDisposed()) {
fSourceAttachmentForm.setFocus();
Expand Down
Expand Up @@ -45,8 +45,13 @@ public ClassFileEditorInputFactory() {
@Override
public IAdaptable createElement(IMemento memento) {
String identifier= memento.getString(KEY);
if (identifier == null)
if (identifier == null) {
return null;
}
return JavaCore.callReadOnly(() -> createElementCached(identifier));
}

private IAdaptable createElementCached(String identifier) {

IJavaElement element= JavaCore.create(identifier);
try {
Expand Down
Expand Up @@ -21,6 +21,7 @@
import java.lang.StackWalker.StackFrame;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.BreakIterator;
import java.text.CharacterIterator;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -30,8 +31,6 @@
import java.util.Map.Entry;
import java.util.function.Predicate;

import java.text.BreakIterator;

import org.osgi.service.prefs.BackingStoreException;

import org.eclipse.help.IContextProvider;
Expand Down Expand Up @@ -1210,15 +1209,19 @@ private class ActivationListener implements IWindowListener {
@Override
public void windowActivated(IWorkbenchWindow window) {
if (window == getEditorSite().getWorkbenchWindow() && fMarkOccurrenceAnnotations && isActivePart()) {
fForcedMarkOccurrencesSelection= getSelectionProvider().getSelection();
ITypeRoot inputJavaElement= getInputJavaElement();
if (inputJavaElement != null) {
IProgressMonitor monitor = getProgressMonitor();
try {
updateOccurrenceAnnotations((ITextSelection)fForcedMarkOccurrencesSelection, SharedASTProviderCore.getAST(inputJavaElement, SharedASTProviderCore.WAIT_NO, monitor));
} finally {
monitor.done();
}
JavaCore.runReadOnly(this::windowActivatedCached);
}
}

private void windowActivatedCached() {
fForcedMarkOccurrencesSelection= getSelectionProvider().getSelection();
ITypeRoot inputJavaElement= getInputJavaElement();
if (inputJavaElement != null) {
IProgressMonitor monitor = getProgressMonitor();
try {
updateOccurrenceAnnotations((ITextSelection)fForcedMarkOccurrencesSelection, SharedASTProviderCore.getAST(inputJavaElement, SharedASTProviderCore.WAIT_NO, monitor));
} finally {
monitor.done();
}
}
}
Expand Down Expand Up @@ -2174,9 +2177,13 @@ public void synchronizeOutlinePageSelection() {
synchronizeOutlinePage(computeHighlightRangeSourceReference());
}

@SuppressWarnings("unchecked")
@Override
public <T> T getAdapter(Class<T> required) {
return JavaCore.callReadOnly(() -> getAdapterCached(required));
}

@SuppressWarnings("unchecked")
private <T> T getAdapterCached(Class<T> required) {

if (IContentOutlinePage.class.equals(required)) {
if (fOutlinePage == null && getSourceViewer() != null && isCalledByOutline())
Expand Down Expand Up @@ -3070,8 +3077,10 @@ public Object getViewPartInput() {
*/
@Override
protected void doSetSelection(ISelection selection) {
super.doSetSelection(selection);
synchronizeOutlinePageSelection();
JavaCore.runReadOnly(() -> {
super.doSetSelection(selection);
synchronizeOutlinePageSelection();
});
}

boolean isFoldingEnabled() {
Expand Down
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.jdt.core.ICodeAssist;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.BreakStatement;
Expand Down Expand Up @@ -81,6 +82,10 @@ public class JavaElementHyperlinkDetector extends AbstractHyperlinkDetector {
*/
@Override
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
return JavaCore.callReadOnly(() -> detectHyperlinksCached(region));
}

private IHyperlink[] detectHyperlinksCached(IRegion region) {
ITextEditor textEditor= getAdapter(ITextEditor.class);
if (region == null || !(textEditor instanceof JavaEditor))
return null;
Expand Down
Expand Up @@ -930,6 +930,10 @@ private void registerToolbarActions(IActionBars actionBars) {
*/
@Override
public void createControl(Composite parent) {
JavaCore.runReadOnly(() -> createControlCached(parent));
}

private void createControlCached(Composite parent) {

Tree tree= new Tree(parent, SWT.MULTI);

Expand Down
Expand Up @@ -157,6 +157,10 @@ public IFormattingContext createFormattingContext() {
*/
@Override
public void doOperation(int operation) {
JavaCore.runReadOnly(()->doOperationCached(operation));
}

private void doOperationCached(int operation) {
if (getTextWidget() == null)
return;

Expand Down
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.ui.IWorkbenchPartSite;

import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;
Expand Down Expand Up @@ -621,8 +622,10 @@ protected IStatus run(IProgressMonitor monitor) {
}
if (monitor.isCanceled())
return Status.CANCEL_STATUS;
CompilationUnit ast= SharedASTProviderCore.getAST(element, SharedASTProviderCore.WAIT_YES, monitor);
reconciled(ast, false, monitor);
JavaCore.runReadOnly(() -> {
CompilationUnit ast= SharedASTProviderCore.getAST(element, SharedASTProviderCore.WAIT_YES, monitor);
reconciled(ast, false, monitor);
});
synchronized (fJobLock) {
// allow the job to be gc'ed
if (fJob == this)
Expand Down
Expand Up @@ -18,6 +18,8 @@

import org.eclipse.ui.PlatformUI;

import org.eclipse.jdt.core.JavaCore;

import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.javaeditor.breadcrumb.IBreadcrumb;
Expand Down Expand Up @@ -48,9 +50,10 @@ public void run() {
IBreadcrumb breadcrumb= fEditor.getBreadcrumb();
if (breadcrumb == null)
return;

IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
store.setValue(getPreferenceKey(), true);
JavaCore.runReadOnly(() -> {
IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
store.setValue(getPreferenceKey(), true);
});

breadcrumb.activate();
}
Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.ui.texteditor.TextEditorAction;

import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.JavaCore;

import org.eclipse.jdt.ui.IWorkingCopyManager;
import org.eclipse.jdt.ui.PreferenceConstants;
Expand Down Expand Up @@ -126,7 +127,7 @@ public void setEditor(ITextEditor editor) {
fStore= null;
}

update();
JavaCore.runReadOnly(() -> update());
}

/**
Expand Down
Expand Up @@ -26,6 +26,8 @@

import org.eclipse.ui.IEditorPart;

import org.eclipse.jdt.core.JavaCore;

import org.eclipse.jdt.ui.PreferenceConstants;
import org.eclipse.jdt.ui.text.java.hover.IJavaEditorTextHover;

Expand Down Expand Up @@ -127,7 +129,7 @@ public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
*/
@Override
public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
return getHoverInfo2(textViewer, hoverRegion, false);
return JavaCore.callReadOnly(() -> getHoverInfo2(textViewer, hoverRegion, false));
}

/**
Expand Down
Expand Up @@ -664,7 +664,7 @@ public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
}

private JavadocBrowserInformationControlInput internalGetHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
IJavaElement[] elements= getJavaElementsAt(textViewer, hoverRegion);
IJavaElement[] elements= JavaCore.callReadOnly(() -> getJavaElementsAt(textViewer, hoverRegion));
if (elements == null || elements.length == 0)
return null;

Expand Down

0 comments on commit fc6a25a

Please sign in to comment.