Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

performance: avoid O(n^2) in PDEJavaHelper #747

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.ListIterator;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
Expand Down Expand Up @@ -246,12 +247,16 @@ private static IPackageFragment getExternalPackageFragment(String packageName, S
// else model is in folder form, try to find model's libraries on filesystem
} else {
IPluginLibrary[] libs = base.getPluginBase().getLibraries();
Map<IPath, IPackageFragmentRoot> rootsByPath = null;
for (IPluginLibrary lib : libs) {
if (IPluginLibrary.RESOURCE.equals(lib.getType())) {
continue;
}
String libName = ClasspathUtilCore.expandLibraryName(lib.getName());
IPackageFragmentRoot root = jp.findPackageFragmentRoot(path.append(libName));
if (rootsByPath == null) {
rootsByPath = getRootsByPath(jp);
}
IPackageFragmentRoot root = rootsByPath.get(path.append(libName));
if (root != null) {
IPackageFragment frag = root.getPackageFragment(packageName);
if (frag.exists()) {
Expand All @@ -265,6 +270,17 @@ private static IPackageFragment getExternalPackageFragment(String packageName, S
return searchWorkspaceForPackage(packageName, base);
}

private static Map<IPath, IPackageFragmentRoot> getRootsByPath(IJavaProject jp) throws JavaModelException {
Map<IPath, IPackageFragmentRoot> rootsByPath = new HashMap<>();
for (IPackageFragmentRoot classpathRoot : jp.getAllPackageFragmentRoots()) {
IPath classRootPath = classpathRoot.getPath();
if (classRootPath != null) {
rootsByPath.put(classRootPath, classpathRoot);
}
}
return rootsByPath;
}

private static IPackageFragment searchWorkspaceForPackage(String packageName, IPluginModelBase base) {
IPluginLibrary[] libs = base.getPluginBase().getLibraries();
ArrayList<IPath> libPaths = new ArrayList<>();
Expand All @@ -286,9 +302,13 @@ private static IPackageFragment searchWorkspaceForPackage(String packageName, IP
continue;
}
IJavaProject jp = JavaCore.create(projects[i]);
Map<IPath, IPackageFragmentRoot> rootsByPath = null;
ListIterator<IPath> li = libPaths.listIterator();
while (li.hasNext()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you already touched this, could you please convert this while loop and if you want the outer for loop over all projects into an enhanced for loop?
Btw. I wonder why there is no quick-fix that suggest to convert this iterator+while into an enhanced for-loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a cleanup, that could do this. but it currently fails. let's wait till that is fixed: eclipse-jdt/eclipse.jdt.ui#798

IPackageFragmentRoot root = jp.findPackageFragmentRoot(li.next());
if (rootsByPath == null) {
rootsByPath = getRootsByPath(jp);
}
IPackageFragmentRoot root = rootsByPath.get(li.next());
if (root != null) {
IPackageFragment frag = root.getPackageFragment(packageName);
if (frag.exists()) {
Expand Down
Loading