mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-19 00:10:00 +00:00
perf(java): prewarm configuration navigation and share results between hover and click (#947)
Configuration navigation from YAML/properties still waited for a cold index the first time and repeated the lookup between the ⌘-hover probe and the click. - Trusted projects with a Maven/Gradle/Eclipse build file start Java shortly after opening. Once JDT reports ServiceReady, the app calls the new openidea.java.configuration.warmup command: a low-priority background job builds the configuration index one project at a time (open YAML/properties modules first), waits for workspace builds, has a 30 s budget per project, and only fills free cache room. - Index builds take a cancellable per-project lock instead of one global lock, so a background build never delays another project, and a warmed snapshot is dropped if the Java model changed meanwhile. - In YAML/properties the editor underlines the whole key (hyphens and dots included), hover and Go to Definition share one bounded request per key, and the declaration chooser names the whole key. Bundle java-usages 1.6.0 is rebuilt reproducibly with build.py against JDT LS 1.63.0-202610191322 (SHA-256 33cfa1bb…). The real-JDT harness checks that the first lookup reuses the warmed index. Co-authored-by: qjlqyf <109515705+qjlqyf@users.noreply.github.com>
This commit is contained in:
@@ -4,7 +4,10 @@ import java.net.URI;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.zip.ZipFile;
|
||||
import com.google.gson.*;
|
||||
import org.eclipse.core.resources.ResourcesPlugin;
|
||||
@@ -29,6 +32,8 @@ public final class ConfigurationCommand implements IDelegateCommandHandler {
|
||||
private static final int MAX_UNITS = 20_000, MAX_ENTRIES = 100_000, MAX_BYTES = 2 * 1024 * 1024;
|
||||
private static final AtomicLong REVISION = new AtomicLong(), GENERATION = new AtomicLong();
|
||||
private static final Map<String, Index> CACHE = new LinkedHashMap<>();
|
||||
/** One build per project at a time; waiting lookups stay cancellable. */
|
||||
private static final Map<String, ReentrantLock> LOCKS = new ConcurrentHashMap<>();
|
||||
/** Unsaved edits are covered by the working-copy digest, so reconciles never
|
||||
* invalidate. Opening/closing a working copy (F_PRIMARY_WORKING_COPY) and
|
||||
* non-Java resources, reported as F_CONTENT on a project, root or package
|
||||
@@ -56,6 +61,7 @@ public final class ConfigurationCommand implements IDelegateCommandHandler {
|
||||
|
||||
@Override
|
||||
public Object executeCommand(String command, List<Object> args, IProgressMonitor monitor) throws Exception {
|
||||
if (command.equals("openidea.java.configuration.warmup")) return ConfigurationWarmup.request(args);
|
||||
String uri = (String) args.get(0);
|
||||
String key = args.get(1) instanceof String s ? s : null;
|
||||
int line = ((Number) args.get(2)).intValue(), column = ((Number) args.get(3)).intValue();
|
||||
@@ -138,7 +144,7 @@ public final class ConfigurationCommand implements IDelegateCommandHandler {
|
||||
(line < r.getEnd().getLine() || line == r.getEnd().getLine() && col < r.getEnd().getCharacter());
|
||||
}
|
||||
private static void check(IProgressMonitor monitor) { if (monitor.isCanceled()) throw new OperationCanceledException(); }
|
||||
private static IJavaProject project(String uri) {
|
||||
static IJavaProject project(String uri) {
|
||||
Path path;
|
||||
try { path = Path.of(URI.create(uri)).toFile().getCanonicalFile().toPath(); } catch (Exception e) { return null; }
|
||||
IJavaProject best = null;
|
||||
@@ -163,7 +169,33 @@ public final class ConfigurationCommand implements IDelegateCommandHandler {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static synchronized Index index(IJavaProject project, IProgressMonitor monitor) throws Exception {
|
||||
/** Builds (or reuses) the project's index in the background; its generation. */
|
||||
static long warm(IJavaProject project, IProgressMonitor monitor) throws Exception {
|
||||
return index(project, monitor, true).generation;
|
||||
}
|
||||
|
||||
/** Warming fills free cache room only; it never evicts an index a lookup built. */
|
||||
static boolean hasWarmupCapacity(IJavaProject project) {
|
||||
synchronized (CACHE) {
|
||||
return CACHE.containsKey(project.getHandleIdentifier()) || (CACHE.size() < 8 &&
|
||||
CACHE.values().stream().mapToInt(value -> value.entries.size()).sum() < MAX_ENTRIES);
|
||||
}
|
||||
}
|
||||
|
||||
private static Index index(IJavaProject project, IProgressMonitor monitor) throws Exception {
|
||||
return index(project, monitor, false);
|
||||
}
|
||||
|
||||
private static Index index(IJavaProject project, IProgressMonitor monitor, boolean background) throws Exception {
|
||||
ReentrantLock lock = LOCKS.computeIfAbsent(project.getHandleIdentifier(), ignored -> new ReentrantLock());
|
||||
while (!lock.tryLock(50, TimeUnit.MILLISECONDS)) check(monitor);
|
||||
try {
|
||||
check(monitor);
|
||||
return build(project, monitor, background);
|
||||
} finally { lock.unlock(); }
|
||||
}
|
||||
|
||||
private static Index build(IJavaProject project, IProgressMonitor monitor, boolean background) throws Exception {
|
||||
long revision = REVISION.get();
|
||||
String id = project.getHandleIdentifier();
|
||||
Map<String, ICompilationUnit> copies = new TreeMap<>();
|
||||
@@ -181,7 +213,8 @@ public final class ConfigurationCommand implements IDelegateCommandHandler {
|
||||
if (source != null) digest.update(source.getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
}
|
||||
String workingCopies = HexFormat.of().formatHex(digest.digest());
|
||||
Index cached = CACHE.get(id);
|
||||
Index cached;
|
||||
synchronized (CACHE) { cached = CACHE.get(id); }
|
||||
if (cached != null && cached.revision == revision && cached.workingCopies.equals(workingCopies)) {
|
||||
boolean valid = true;
|
||||
for (var entry : cached.files.entrySet()) {
|
||||
@@ -227,11 +260,19 @@ public final class ConfigurationCommand implements IDelegateCommandHandler {
|
||||
roots.add(project.getProject().getLocationURI().toString());
|
||||
builder.metadataDirectory(project.getProject().getLocation().toFile().toPath().resolve("src/main/resources"), project);
|
||||
Index result = new Index(revision, workingCopies, List.copyOf(builder.entries), List.copyOf(roots), Map.copyOf(builder.files), builder.incomplete, builder.parsed, GENERATION.incrementAndGet());
|
||||
CACHE.remove(id);
|
||||
while (!CACHE.isEmpty() && (CACHE.size() >= 8 ||
|
||||
CACHE.values().stream().mapToInt(value -> value.entries.size()).sum() + result.entries.size() > MAX_ENTRIES))
|
||||
CACHE.remove(CACHE.keySet().iterator().next());
|
||||
CACHE.put(id, result);
|
||||
check(monitor);
|
||||
// A warmed index that the model has already outdated is dropped, not published.
|
||||
if (background && revision != REVISION.get()) throw new OperationCanceledException();
|
||||
synchronized (CACHE) {
|
||||
if (background && ((!CACHE.containsKey(id) && CACHE.size() >= 8) ||
|
||||
CACHE.entrySet().stream().filter(e -> !e.getKey().equals(id)).mapToInt(e -> e.getValue().entries.size()).sum()
|
||||
+ result.entries.size() > MAX_ENTRIES)) throw new OperationCanceledException();
|
||||
CACHE.remove(id);
|
||||
while (!CACHE.isEmpty() && (CACHE.size() >= 8 ||
|
||||
CACHE.values().stream().mapToInt(value -> value.entries.size()).sum() + result.entries.size() > MAX_ENTRIES))
|
||||
CACHE.remove(CACHE.keySet().iterator().next());
|
||||
CACHE.put(id, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
package ai.openidea.configuration;
|
||||
|
||||
import java.util.*;
|
||||
import org.eclipse.core.resources.ResourcesPlugin;
|
||||
import org.eclipse.core.runtime.*;
|
||||
import org.eclipse.core.runtime.jobs.Job;
|
||||
import org.eclipse.jdt.core.*;
|
||||
|
||||
/** One bounded, cooperative queue per language-server process. No polling timer,
|
||||
* retained ASTs, or project builds; an ordinary lookup remains the fallback. */
|
||||
final class ConfigurationWarmup {
|
||||
private static final Map<String, IJavaProject> pending = new LinkedHashMap<>();
|
||||
private static final Set<String> attempted = new HashSet<>();
|
||||
private static final Map<String, Long> completed = new LinkedHashMap<>();
|
||||
private static boolean scheduled, stopped;
|
||||
private static String active;
|
||||
private static final Job job = new Job("Preparing configuration navigation") {
|
||||
@Override protected IStatus run(IProgressMonitor monitor) {
|
||||
// Compiler defaults/import can schedule a build after ServiceReady.
|
||||
// Yield to it instead of blocking a worker on the build job family.
|
||||
if (building()) {
|
||||
synchronized (ConfigurationWarmup.class) {
|
||||
if (!stopped) schedule(2000);
|
||||
else scheduled = false;
|
||||
}
|
||||
return Status.OK_STATUS;
|
||||
}
|
||||
IJavaProject project;
|
||||
synchronized (ConfigurationWarmup.class) {
|
||||
if (stopped || pending.isEmpty()) { scheduled = false; return Status.OK_STATUS; }
|
||||
String id = pending.keySet().iterator().next();
|
||||
project = pending.remove(id);
|
||||
active = id;
|
||||
}
|
||||
long deadline = System.nanoTime() + 30_000_000_000L;
|
||||
IProgressMonitor bounded = new ProgressMonitorWrapper(monitor) {
|
||||
@Override public boolean isCanceled() {
|
||||
return super.isCanceled() || System.nanoTime() >= deadline ||
|
||||
!project.exists() || !project.getProject().isOpen();
|
||||
}
|
||||
};
|
||||
try {
|
||||
if (ConfigurationCommand.hasWarmupCapacity(project)) {
|
||||
long build = ConfigurationCommand.warm(project, bounded);
|
||||
synchronized (ConfigurationWarmup.class) {
|
||||
if (completed.size() < 8) completed.put(project.getHandleIdentifier(), build);
|
||||
}
|
||||
}
|
||||
} catch (OperationCanceledException e) {
|
||||
// No partial index is published. A user lookup can retry later.
|
||||
} catch (Exception e) {
|
||||
org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin.logException("Configuration warmup skipped", e);
|
||||
} finally {
|
||||
synchronized (ConfigurationWarmup.class) {
|
||||
active = null;
|
||||
if (!stopped && !pending.isEmpty()) schedule(200);
|
||||
else scheduled = false;
|
||||
}
|
||||
}
|
||||
return Status.OK_STATUS;
|
||||
}
|
||||
};
|
||||
static { job.setSystem(true); job.setPriority(Job.DECORATE); }
|
||||
|
||||
private static boolean building() {
|
||||
for (Object family : new Object[] { ResourcesPlugin.FAMILY_AUTO_BUILD, ResourcesPlugin.FAMILY_MANUAL_BUILD })
|
||||
for (Job other : Job.getJobManager().find(family))
|
||||
if (other.getState() != Job.NONE) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
static synchronized Object request(List<Object> args) throws JavaModelException {
|
||||
String action = args.isEmpty() ? "start" : String.valueOf(args.get(0));
|
||||
if (action.equals("cancel")) {
|
||||
stopped = true;
|
||||
pending.clear();
|
||||
job.cancel();
|
||||
if (active == null) scheduled = false;
|
||||
} else if (action.equals("start") && !stopped) {
|
||||
// Open Java buffers identify the user's active modules. Keep those
|
||||
// ahead of the rest when the workspace exceeds the cache budget.
|
||||
Set<String> open = new HashSet<>();
|
||||
for (ICompilationUnit unit : JavaCore.getWorkingCopies(null)) open.add(unit.getJavaProject().getHandleIdentifier());
|
||||
for (ICompilationUnit unit : JavaCore.getWorkingCopies(
|
||||
org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin.getInstance().getWorkingCopyOwner()))
|
||||
open.add(unit.getJavaProject().getHandleIdentifier());
|
||||
Set<String> preferred = new HashSet<>();
|
||||
if (args.size() > 1 && args.get(1) instanceof List<?> uris) {
|
||||
for (Object uri : uris) if (uri instanceof String text) {
|
||||
IJavaProject project = ConfigurationCommand.project(text);
|
||||
if (project != null) preferred.add(project.getHandleIdentifier());
|
||||
}
|
||||
}
|
||||
List<IJavaProject> projects = new ArrayList<>(Arrays.asList(
|
||||
JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects()));
|
||||
projects.sort(Comparator.comparing((IJavaProject p) -> !preferred.contains(p.getHandleIdentifier()))
|
||||
.thenComparing(p -> !open.contains(p.getHandleIdentifier()))
|
||||
.thenComparing(IJavaProject::getElementName));
|
||||
for (IJavaProject project : projects) {
|
||||
String id = project.getHandleIdentifier();
|
||||
if (project.getProject().isOpen() && !attempted.contains(id) && pending.size() < 64) {
|
||||
attempted.add(id);
|
||||
pending.put(id, project);
|
||||
}
|
||||
}
|
||||
if (!scheduled && !pending.isEmpty()) { scheduled = true; job.schedule(2000); }
|
||||
}
|
||||
return Map.of("scheduled", scheduled, "pending", pending.size(), "active", active == null ? "" : active,
|
||||
"completed", new LinkedHashMap<>(completed));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user