Large workspaces pose a challenge to Java language servers: in the traditional approach the LS builds a full cross-reference index before they will answer requests. In a workspace with hundreds of thousands of types, this startup phase can easily extend to many minutes or even hours.
SCIP (Sourcegraph) or LSIF (Microsoft) try to address the problem by precomputing the results of LSP requests as a side-effect of the compilation of the project. This works well for browsing read-only code bases, but imagine you change the JDK of your project from version 25 to 21. All the resolved references to JDK classes in your code would have to be recomputed. In addtion, it’s quite hard to selectively recompute the SCIP files, so you’ll probably reindex most of your workspace. So we seem to be stuck with either large up-front indexing costs or slow operations in regular usage.
Metals V2 tries to address the issue by precompiling just the declarations of types in the workspace with Google’s Turbine header compiler. This is quite fast and allows the LS to compute many requests “on the fly” without large pre-indexing costs.
My prototype java-ls language server takes this idea and turns the knob to 11. Instead of pre-compiling the source files in the workspace, java-ls just computes an in-memory index of the declarations found in source and class files in the workspace without resolving any imported symbols. Let me explain…
How It Works
When java-ls starts up, it reads a description of the workspace from an mbt.json file in the workspace. It then proceeds to parse all source and jar files and puts all the type declarations in those files into an in-memory index without resolving any external types. So if you write
public class Foo extends Bar {
It will not try to resolve Bar to my.package.Bar but will just record the simple name of the superclass. The index also contains the methods and fields declared in those types.
When the language server needs to compute a request like “Go to declaration”, it parses the source file where the request is made and gets the
compiler to fully resolve all the types in the parse tree. So instead of just seeing List foo = null;, we know that the type is actually
java.util.List and we know the exact location on disk where the class file is read from. This is usually a costly process, as we need to find the
class file and load it into the compiler. And not just for a single import, but for all imports and transitively for all super-types. That can amount to analyzing thousands of types for each request.
The “trick” java-ls employs is this: we overwrite the component in the compiler that loads class files and make it load the type definitions from the in-memory index. Since the index is optimized for the needs of the compiler and there is no parsing required, this is very fast. So to implement “Go to declaration” we get the resolved element from the parse tree. We know it’s just a wrapper around a declaration index element, so we can just extract the declaration location from the wrapped index entry. Job done. For class files, there is a second step involved that looks up the attached source file for the jar and finds the declaration in there.
One important thing to note is that we maintain a precise class path and we use that compile class path to resolve the correct type in the parse tree. So if a type is not on the compile class path, you get an error. If two types with the same name are on the class path, you get the correct one according to Java class path semantics.
This works well for declarations, but what about cross-references? Let’s say the user puts the cursor on List in the above example and does
“Find all references”. We’ll have to find all the places the identifier “List” is used and we have to verify that it’s not java.awt.List and it’s not java.util.List, but from the subproject where you compile against a different version of the JDK. Doing so in every file in the workspace would again take a very long time, so when we index the declarations at startup, we also compute a Bloom-filter index of all identifiers in the source files. A bloom filter can tell us whether a particular identifier occurs in any given file. We then again get the fully resolved parse tree for each candidate file and verify that the wrapped declaration index entry points indeed the element we’re computing references to.
How Well It Works
Let’s look at how these ideas translate into the real world. As an example, I like to use the trino project. It contains 11044 source files and 979 jar dependencies. Computing the indices of all these roughly 350k types takes around 25 seconds on my mid-range Lenovo laptop.
When I do “Find all references” for java.lang.String, I find ca. 54000 references from 6063 bloom filter candidates in around 26 seconds.
The language server uses around 750mb of heap memory at steady state for storing its indices. As far as I know, these numbers are better than any Java
language server currently available.
How Does It Scale
Everything in java-ls scales linearly in both time and space. If you have twice the number of source files in your workspace, indexing them will take twice as long and use twice the memory, not four times or more. Since we only look at a single file independently when we index it, we can trivially parallelize the indexing.
The same applies when we compute cross-references: computing the candidates scales with the number of files in the Bloom filter index. But computing the candidates is in practice a very fast operation. We then compute the references one file at a time (or a small number in parallel). Therefore the max heap size needed does not really grow a lot. The time needed to compute the references correlates with the number of bloom hits.
Architectural Benefits
There are a couple of beneficial consequences of indexing unresolved declarations. The most immediate advantage is that updating the index is trivial. If you detect that a source or class file has changed, you only need to find the entries in the index that have been produced from the changed file. You can also use this property to store a “baseline index” with a timestamp. To update the index for a workspace with changes in it, you just have to find all files with a newer timestamp and reindex those.
The second benefit is that you can trivially combine partial indices. If you have indices for two versions of a jar, you can throw them both into the global index, since the class path lookup mechanism will choose the correct one. We could also imagine an “index daemon” that can serve index results on a machine that serves multiple cloud developer environments. Since the index is context free, it’s all the same to the language server.
I have made the decision to treat the index as binary types: to the compiler they look like class files. This opens the way for java-ls to be a natively polyglot language server for the JVM. We could write an indexer for Scala source and the language server could immediately correctly resolve symbols from Scala source. Of course, if we wanted to navigate to a type declaration in Scala source, we’d have to provide some component that could map from the JVM binary reference in Java to the corresponding Scala source construct.
Architectural Drawbacks
The only real drawback I see right now is with annotation processing: annotation processors require fully resolved types, so you cannot run them during the indexing phase. Since annotation processors are not allowed to change the parse tree of the annotated source, the declaration index will still be correct, but you might be missing some source files until they are generated by the annotation processors. But once you run your build, everything should be picked up correctly. One can also imagine running the annotation processors in some background task after startup. The annotation processors might even be faster, because they could get the resolved parse tree using the index. There are some ill-behaved processors like Lombok that do not comply with the Java spec where this is not true. One can imagine patching the index for such processors. That would have to be a special case hack, though.
What’s There
The language server currently implements “Hover”, “Go to declaration”, “Find References”, “Type Hierarchy” and “Code completion”, “Call Hierarchy”. In my opinion, these requests should cover anything you can do in LSP as far as capabilities and performance are concerned. You can install vscode-java-ls from the VS Code Marketplace or from Open VSX. There are settings that allow you to choose the indexer (turbine/javac/ecj) and the “front end” (ecj or javac).
Be aware that this is a proof of concept, so YMMV.
One thing java-ls is not and will never be is a build system. If you have a maven build, you should use maven to build the project. If we want to provide UI elements representing the build system, they should be implemented outside of java-ls.
What’s Missing
java-ls currently does only include an importer for maven projects. If you open a workspace containing a maven project, it will generate an mbt.json
und use it. For other build systems, you need an mbt.json to be present in the workspace. Fortunately, you can generate
one simply by opening the project with the Metals VS Code extension
installed. You’ll have to follow the instructions for using the latest build of Metals V2. Or maybe just ask an AI to generate you one?
Code completion is very rudimentary and refactorings are not implemented at all. However, I consider these less important in the age of AI programming.
There is also no persistence of the indices and the file change tracking mechanism needs some work.
There is no API right now and no hooks for extensibility. If there is to be an API, it would probably offer functionality for searching the index and for getting resolve parse trees efficiently. I do not foresee any in-process extensibility, though, as that way lies madness.
Credits
The main idea of context-free indexing comes from my time at Red Hat working on Eclipse Che. We were thinking about sharing data among language server instances in order to preserve resources when serving developer workspaces. These ideas came back to me when talking with Ólafur Geirsson from Databricks about his work on making Metals a Java language server that scales (read up on it here).
The idea of using a file for a minimal description
of the workspace (mbt.json) grew out of those conversations. I think it’s a brilliant idea, because it could do for build system import what LSP
has done for language servers: instead of writing importers again for each IDE, we could have a single one for each build system. The mbt.json format
takes inspiration from Build Server Protocol, but somehow, implementing good BSP servers seems to be a challenge.
Using a Bloom filter index for cross referencing is taken straight from Metals.
I owe a debt of gratitude to Cursor. This project grew out of a year-long effort on behalf of Cursor to improve Java support for large codebases and they allowed me the freedom to investigate alternative approaches.
Brother Can You Spare a Dime
I’d be very interested in pursuing java-ls further. Unfortunately, the project goes way beyond what I can do in my spare time. If you are intersted in financing further work in this direction, drop me an email
