Shave O(500ms) off of sbt startup

It turns out that it can take roughly one second to instantiate a
scala.nsc.tools.Global instance for the first time. When sbt is starting
up, it also takes nearly 2 seconds to initialize logging. We can speed
up the boot time by doing these two things concurrently. On my machine,
I saw on average a 500ms decrease in startup time after this change.
This commit is contained in:
Ethan Atkins 2019-05-13 11:02:08 -07:00
parent 529ba8a3f2
commit 418e7e09fd
2 changed files with 9 additions and 1 deletions

View File

@ -44,6 +44,14 @@ final class xMain extends xsbti.AppMain {
val instance = clazz.getField("MODULE$").get(null)
val runMethod = clazz.getMethod("run", classOf[xsbti.AppConfiguration])
try {
new Thread("sbt-load-global-instance") {
setDaemon(true)
override def run(): Unit = {
// This preloads the scala.tools.nsc.Global as a performance optimization"
loader.loadClass("sbt.internal.parser.SbtParser$").getField("MODULE$").get(null)
()
}
}.start()
runMethod.invoke(instance, modifiedConfiguration).asInstanceOf[xsbti.MainResult]
} catch {
case e: InvocationTargetException =>

View File

@ -108,7 +108,7 @@ private[sbt] object SbtParser {
private[sbt] final val globalReporter = new UniqueParserReporter
private[sbt] var scalacGlobalInitReporter: Option[ConsoleReporter] = None
private[sbt] final lazy val defaultGlobalForParser = {
private[sbt] final val defaultGlobalForParser = {
val options = "-cp" :: s"$defaultClasspath" :: "-Yrangepos" :: Nil
val reportError = (msg: String) => System.err.println(msg)
val command = new CompilerCommand(options, reportError)