How to find out which implicits are used in my scala code

Description of the problem:

  • I read in several sources / articles that imply a scala disk compilation time

  • I want to remove / minimize them to see what compilation time would look like without them (the code base is about 1000 files of varying complexity based on scalaz and akka and slick).

  • I do not know what static analysis I can perform. Any sympathy / links to existing tools are highly appreciated.

+5
source share
2 answers

It is true that implicits can degrade compilation speed, especially for code that uses them for level-level computing. It is definitely worth evaluating their impact. Unfortunately, it can be difficult to track down criminals. There are tools that can help:

  • Run scalac with -Ystatistics:typer to see how many tree nodes are processed during type checking. For instance. you can check the number of ApplyToImplicitArgs and ApplyImplicitView against the total number (and maybe compare this to a different code base).

  • Scala Center efforts are currently being strengthened to improve the status quo hosted on scalacenter / scalac-profiling . It includes an sbt plugin that should be able to give you an idea of โ€‹โ€‹implicit search time, but it is still in its infancy (not yet published at the time of writing). I have not tested it myself, but you can still try it.

  • You can also compile with -Xlog-implicits , transfer the output to a file, and analyze the logs. It will display a message for each implicit candidate that has been reviewed but not completed, complete with its original position, search type, and reason for failure. Such unsuccessful searches are expensive. You can write a simple script with your favorite scripting language (why not Scala?), To summarize the data and even draw it using some nice graphics.

Also: how to allow a specific implicit instance?

Just use reify and good println debugging:

 import scala.collection.SortedSet import scala.reflect.runtime.universe._ println(showCode(reify { SortedSet(1,2,3) }.tree)) // => SortedSet.apply(1, 2, 3)(Ordering.Int) 
+1
source

Scalar profiling occurs using the scala center: https://scalacenter.imtqy.com/scalac-profiling/

0
source

Source: https://habr.com/ru/post/1274434/


All Articles