How does scala2 compiler cache/invalidate the typeChecking result for the given tree?


Hi, I’m currently trying to optimize the InteractiveDriver of Scala3.
Currently, every time developers hover on symbols in Metals in the Scala3 project, Metals asks InteractiveDriver to compile the unit with to make sure the compilation unit has the latest semantic information.

However,, especially the type checking, is CPU-intensive, which makes showing hover information in Metals a bit CPU-intensive, even though this feature is frequently used.

In Scala2 …

I realized it is the problem only in Scala3 projects, and Metals doesn’t consume so much CPU in Scala2 projects, even though Metals call typeCheck every time users hover on symbols in Scala2 projects.

I profiled using async-profiler and realized that scalac doesn’t run typed1 if the given tree is already typed, and that’s why typeCheck with scalac in IDE environment is blazingly fast.


I also realized that when we modify the type in a dependent unit, scalac will run typed1.
For example, when we have two files A.scala and B.scala.

// A.scala
object A {
  val aaa =

// B.scala
object B {
  def foo: Int = ???

If users hover on it shows, it’s type is Int. Then, modify the def foo: Int = ??? to def foo: String = ???.
Now, hover on again, scalac takes some time to run typeCheck(unit) because now typed1 will be called. Which means when we modify a B.scala, alreadyTyped will be false.

Question is, how does scalac detect something has changed since the last typeChecking, and make alreadyTyped = false? around here ?

Additional Context

I was considering trying not to run if the “input hasn’t changed since the last compilation” but if we could implement the same kind of optimization in Scala3, we don’t need to remember the compilation unit is up-to-date on Metals side.


It might be don in Metals side I’ll check more :bowing_man:

1 Like

Ahh, never mind, I realized this is done by Metals side + Scala2. I’ll describe later.

1 Like

Scala2 part

In Scala2 compiler, the Typer phase will skip typing if the given tree is already typed.

Metals part

Cache 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

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.


As it looks like there’s no such thing in Scala3 (skip typechecking if it’s already typed)
I would go for caching the compilation on Metals.