Skip to content

Commit

Permalink
[eclipse-lsp4e#1207] cache IResource in EnablementTester
Browse files Browse the repository at this point in the history
Cache resource for URI because each call of
LSPEclipseUtils.findResourceFor(URI) takes ~300 microseconds. And it
gets called a lot of times.

fixes eclipse-lsp4e#1207
  • Loading branch information
ghentschke committed Feb 12, 2025
1 parent 2cc8e57 commit 98085ef
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
package org.eclipse.lsp4e.enablement;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

import org.eclipse.core.expressions.EvaluationContext;
Expand Down Expand Up @@ -40,6 +42,7 @@ public final class EnablementTester {
private final Expression expression;
private final String description;
private final Supplier<@Nullable IEvaluationContext> parent;
private final Map<URI, IResource> cache = new HashMap<>();

public EnablementTester(Expression expression, String description) {
this(() -> null, expression, description);
Expand Down Expand Up @@ -69,7 +72,7 @@ public boolean evaluate(@Nullable URI uri) {
IResource resource = null;
try {
IDocument document = null;
resource = LSPEclipseUtils.findResourceFor(uri);
resource = getResourceFor(uri);
if (resource != null) {
document = LSPEclipseUtils.getExistingDocument(resource);
if (document == null) {
Expand Down Expand Up @@ -104,4 +107,25 @@ public boolean evaluate(@Nullable URI uri) {
return false;
}

/**
* Cache resource for URI because each call to {@link LSPEclipseUtils#findResourceFor(URI)} takes ~300 microseconds.
* And the evaluate method gets called several thousand times in UI thread during editing a LS backed file.
* @param uri
* @return
*/
private @Nullable IResource getResourceFor(@Nullable URI uri) {
if (uri != null) {
var resource = cache.get(uri);
if (resource != null && resource.isAccessible()) {
return resource;
}
resource = LSPEclipseUtils.findResourceFor(uri);
if (resource != null) {
cache.put(uri, resource);
}
return resource;
}
return null;
}

}

0 comments on commit 98085ef

Please sign in to comment.