Skip to content

Granular complile-time caches for Scala 2 and 3 #350

Description

@neko-kai

We use caching to speed up LightTypeTag creation on Scala 2, we might want to port this technique to Scala 3 implementation also.

We can also improve Scala 2 caching, e.g. right now we cache individual AbstractReference's, but we don't cache basesDb or InheritanceDb (or we could cache the entire serialized LTT instead)

Implementation plan

Currently Scala 2 implementation caches individual AbstractReferences and Scala 3 caches nothing.

Important: some contributors tried to introduce caches at TagMacro. TagMacro is the high-level entrypoint, while we may try to introduce cache there, in my opinion that's the least beneficial location, because the internal machinery does not recurse to TagMacro while building tags. My advice would be to keep the TagMacro-level caches under a separate option (and port this technique to Scala 2), but we should concentrate on LightTypeTag and its underlying structures.

I think that we should do the following:

  1. Add tree-level caching at TagMacro level to Scala 2 implementation
  2. Explore the possibility of adding tree-level caching into LightTypeTag in both Scala versions
  3. Add individual value-level caching to Scala 3 implementation
  4. Explore our idea of caching the content of basesDbs and inheritanceDbs
  5. Probably the most beneficial we should try to cache serialized form of the type tags! Cache key is the type which is being inspected, cache value is the serialized form of the tag!
  6. For each type of cache we implement, we would need to introduce a separate on/off flag
  7. Relying on the benchmarks, we would have to figure out what cache types are the most beneficial and turn them on by default

I believe, that there is a good potential to significant compilation speed improvement.

Important notes:

  1. The values in our caches MUST be wrapped into SoftReferences. It's ok to recompute but it's not OK to crash the compiler by heap exhaustion.
  2. We should pay attention to using correct and efficient cache keys

For potential contributors: don't waste your tokens unless you have some understanding of what exactly you are doing! So far all the fully automated attempts to use LLMs failed on this issue, but in fact it's a simple mechanical job!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions