Skip to content

Commit

Permalink
[performance] determineIfOnClasspath with last project used eclipse-j…
Browse files Browse the repository at this point in the history
…dt#3470

Instead of randomly looping over all projects try the last successful
first.

Tested by JavaModelTests.testCreatePkgHandleInDifferentProject()

eclipse-jdt#3470
  • Loading branch information
jukzi committed Dec 18, 2024
1 parent ba2adeb commit 95a40ec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,13 @@ public void testCreatePkgHandleInDifferentProject() throws CoreException {
"x.y [in /P1/lib [in P2]]",
element
);
IFolder folder2 = createFolder("/P1/lib/x/z");
IJavaElement element2 = JavaCore.create(folder2);
assertElementEquals(
"Unexpected element",
"x.z [in /P1/lib [in P2]]",
element2
);
} finally {
deleteProjects(new String[] {"P1", "P2"});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ public static IJavaElement create(IFile file, IJavaProject project) {
}
return null;
}

private static volatile IJavaProject lastProjectUsed;
/**
* Returns the package fragment or package fragment root corresponding to the given folder,
* its parent or great parent being the given project.
Expand All @@ -999,6 +999,14 @@ public static IJavaElement create(IFolder folder, IJavaProject project) {
project = JavaCore.create(folder.getProject());
element = determineIfOnClasspath(folder, project);
if (element == null) {
IJavaProject lastProject = lastProjectUsed;
if (lastProject != null) {
// try to avoid searching through all projects
element = determineIfOnClasspath(folder, lastProject);
if (element != null) {
return element;
}
}
// walk all projects and find one that have the given folder on its classpath
IJavaProject[] projects;
try {
Expand All @@ -1007,10 +1015,13 @@ public static IJavaElement create(IFolder folder, IJavaProject project) {
return null;
}
for (IJavaProject p : projects) {
project = p;
element = determineIfOnClasspath(folder, project);
if (element != null)
break;
if (!p.equals(lastProject)) {
element = determineIfOnClasspath(folder, p);
if (element != null) {
lastProjectUsed = p;
return element;
}
}
}
}
} else {
Expand Down

0 comments on commit 95a40ec

Please sign in to comment.