Scala2 part
In Scala2 compiler, the Typer phase will skip typing if the given tree is already typed.
Metals part
Cache compilation unit
-
Metals reuses compiler (Global) instance, storing the instance in
Copilers._compiler
. -
Only when the
_compiler == null
, Metals instantiate the new MetalsGlobal instance which is a subclass ofGlobal
. - In MetalsGlobal, it caches the compilation unit, and if the source content is the same, Metals Global reuse the compilation unit
Therefore, if the content is the same, we reuse the same compilation unit that is already typed in the previous run. That’s why Scala2 will skip typing for the given unit.
Cache purge on change
- When a file has changed, Metals call Compilers.restart.
- The restart method calls
shutdownCurrentCompiler
that set_compiler
tonull
..
That’s how Scala2 + Metals skip typing for the same input while cleaning up the compilation unit when we modify a file in the project.
It would be great if Scala3 also has a way to skip typer (or compilation) if the given tree is already typed (maybe it does?), we can implement this (kind of) cache mechanism in Metals.