From ce63951e73696f2a9451ac1a3ce30bfa0f66198a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Kubitz?= Date: Mon, 18 Sep 2023 15:00:35 +0200 Subject: [PATCH] performance: avoid O(n^2) in PDEJavaHelper findPackageFragmentRoot() searches through all PackageFragment Roots and is called for every libPaths. This can be slow due to involved file access. see https://github.com/eclipse-jdt/eclipse.jdt.core/issues/303 Instead call getAllPackageFragmentRoots() only once, index the result and use O(1) hash access. --- .../eclipse/pde/internal/core/util/PDEJavaHelper.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/PDEJavaHelper.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/PDEJavaHelper.java index 7818247653c..de7c880bf6f 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/PDEJavaHelper.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/PDEJavaHelper.java @@ -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; @@ -286,9 +287,16 @@ private static IPackageFragment searchWorkspaceForPackage(String packageName, IP continue; } IJavaProject jp = JavaCore.create(projects[i]); + Map rootsByPath = new HashMap<>(); + for (IPackageFragmentRoot classpathRoot : jp.getAllPackageFragmentRoots()) { + IPath classRootPath = classpathRoot.getPath(); + if (classRootPath != null) { + rootsByPath.put(classRootPath, classpathRoot); + } + } ListIterator li = libPaths.listIterator(); while (li.hasNext()) { - IPackageFragmentRoot root = jp.findPackageFragmentRoot(li.next()); + IPackageFragmentRoot root = rootsByPath.get(li.next()); if (root != null) { IPackageFragment frag = root.getPackageFragment(packageName); if (frag.exists()) {