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

[#1207] cache IResource in EnablementTester #1208

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ghentschke
Copy link
Contributor

Cache resource for URI because each call of
LSPEclipseUtils.findResourceFor(URI) takes ~300 microseconds. And it gets called a lot of times.

fixes #1207

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
@@ -69,7 +72,7 @@ public boolean evaluate(@Nullable URI uri) {
IResource resource = null;
try {
IDocument document = null;
resource = LSPEclipseUtils.findResourceFor(uri);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there are several places where we map resources to URIs. Should we not benefit by having the cache reused by all consumers of the method? If so, what about building the cache as an static map in LSPEclipseUtils?

if (resource != null && resource.isAccessible()) {
return resource;
}
resource = LSPEclipseUtils.findResourceFor(uri);
Copy link
Contributor

Choose a reason for hiding this comment

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

could it happen that a resource is moved after being cached? Would that not invalidate the cache? If that could happen, should we not either detect to be able to invalidate the cache or have some eviction policy?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

could it happen that a resource is moved after being cached?

Yes, but shouldn't it be handled by the resource.isAccessible call:

Returns whether this resource is accessible. For files and folders, this is equivalent to existing;

Copy link
Contributor

Choose a reason for hiding this comment

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

and what if the resource has been moved to a place where it is still accessible? Can that happen?

Copy link
Contributor Author

@ghentschke ghentschke Feb 5, 2025

Choose a reason for hiding this comment

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

If the resource has been moved, the uri will be changed as well. This leads to an new entry and to an obsolete entry in the cache.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, you are correct that its URI will change, but the cache will still have an entry for the old URI and will return the resource, will it not?

Copy link
Contributor Author

@ghentschke ghentschke Feb 5, 2025

Choose a reason for hiding this comment

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

IMO that's not a problem, if the old URI isn't cached anywhere. I'll check that

@martinlippert
Copy link
Contributor

One question: is there a mechanism to clean up the cache? It looks like it is an ever growing map that keeps references to all those IResource objects forever. Could that be a problem memory-wise over time?

@mickaelistria
Copy link
Contributor

LSPEclipseUtils.findResourceFor(URI) takes ~300 microseconds. And it gets called a lot of times.

Shouldn't such a strategy be implemented directly in LSPEclipseUtils.findResourceFor(uri) instead? And isn't there some other possible improvement to find there, or even in Eclipse Platform?

@ghentschke
Copy link
Contributor Author

One question: is there a mechanism to clean up the cache? It looks like it is an ever growing map that keeps references to all those IResource objects forever. Could that be a problem memory-wise over time?

Yes, I'll add a clear mechanism which could be triggered when the expression.evaluate returns false for the given URI.

@ghentschke
Copy link
Contributor Author

Shouldn't such a strategy be implemented directly in LSPEclipseUtils.findResourceFor(uri) instead? And isn't there some other possible improvement to find there, or even in Eclipse Platform?

Yes. I'll consider it.

@@ -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<>();
Copy link
Member

Choose a reason for hiding this comment

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

How about using Guava Cache instead which supports expiring entries after read access and is thread safe. LSP4E already depends on guava so no new dependency needs to be added.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the hint. I'll take a closer look at it.

Copy link
Contributor

Choose a reason for hiding this comment

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

@sebthom The Javadocs for Guava's says to prefer using Caffeine instead.

AFAICT nothing contributes caffeine to SimRel yet, but some projects do use it (e.g. Mylyn, m2e's tests) and it is in orbit aggregator for 2025-03 too

Any thoughts on best way to proceed?

Copy link
Member

@sebthom sebthom Feb 10, 2025

Choose a reason for hiding this comment

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

I personally would be pragmatic and just use guava instead of introducing another dependency. I think it is good enough. Esp. since it does not look like guava will go away from lsp4e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[performance] EnablementTester.evaluate takes way to long in UI thread
6 participants