diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2018-02-25 13:51:50 -0800 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2018-02-25 13:51:50 -0800 |
commit | 554840f9b5cd30a8e3209cb18bdf9925f364cc68 (patch) | |
tree | c573183911e02b89ff21c806def8a1b5ce0d9b46 /main | |
parent | 19219cbbd18efb819e45b0af221f08065ad5c982 (diff) | |
download | mill-554840f9b5cd30a8e3209cb18bdf9925f364cc68.tar.gz mill-554840f9b5cd30a8e3209cb18bdf9925f364cc68.tar.bz2 mill-554840f9b5cd30a8e3209cb18bdf9925f364cc68.zip |
A few attempts at micro-optimizing the current hot spots
Diffstat (limited to 'main')
-rw-r--r-- | main/src/mill/main/MainRunner.scala | 45 | ||||
-rw-r--r-- | main/src/mill/main/RunScript.scala | 15 | ||||
-rw-r--r-- | main/test/src/mill/eval/ModuleTests.scala | 4 | ||||
-rw-r--r-- | main/test/src/mill/util/TestGraphs.scala | 4 | ||||
-rw-r--r-- | main/test/src/mill/util/TestUtil.scala | 2 |
5 files changed, 45 insertions, 25 deletions
diff --git a/main/src/mill/main/MainRunner.scala b/main/src/mill/main/MainRunner.scala index a4a54df8..4205b6dc 100644 --- a/main/src/mill/main/MainRunner.scala +++ b/main/src/mill/main/MainRunner.scala @@ -1,13 +1,15 @@ package mill.main import java.io.{InputStream, OutputStream, PrintStream} +import ammonite.Main import ammonite.interp.{Interpreter, Preprocessor} import ammonite.ops.Path import ammonite.util._ import mill.eval.{Evaluator, PathRef} - import mill.util.PrintLogger +import scala.annotation.tailrec + /** * Customized version of [[ammonite.MainRunner]], allowing us to run Mill @@ -35,8 +37,27 @@ class MainRunner(val config: ammonite.main.Cli.Config, while(statAll()) Thread.sleep(100) } + /** + * Custom version of [[watchLoop]] that lets us generate the watched-file + * signature only on demand, so if we don't have config.watch enabled we do + * not pay the cost of generating it + */ + @tailrec final def watchLoop2[T](isRepl: Boolean, + printing: Boolean, + run: Main => (Res[T], () => Seq[(Path, Long)])): Boolean = { + val (result, watched) = run(initMain(isRepl)) + + val success = handleWatchRes(result, printing) + if (!config.watch) success + else{ + watchAndWait(watched()) + watchLoop2(isRepl, printing, run) + } + } + + override def runScript(scriptPath: Path, scriptArgs: List[String]) = - watchLoop( + watchLoop2( isRepl = false, printing = true, mainCfg => { @@ -58,12 +79,22 @@ class MainRunner(val config: ammonite.main.Cli.Config, result match{ case Res.Success(data) => - val (eval, evaluationWatches, res) = data + val (eval, evalWatches, res) = data stateCache = Some(Evaluator.State(eval.rootModule, eval.classLoaderSig, eval.workerCache, interpWatched)) - - (Res(res), interpWatched ++ evaluationWatches) - case _ => (result, interpWatched) + val watched = () => { + val alreadyStale = evalWatches.exists(p => p.sig != PathRef(p.path, p.quick).sig) + // If the file changed between the creation of the original + // `PathRef` and the current moment, use random junk .sig values + // to force an immediate re-run. Otherwise calculate the + // pathSignatures the same way Ammonite would and hand over the + // values, so Ammonite can watch them and only re-run if they + // subsequently change + if (alreadyStale) evalWatches.map(_.path -> util.Random.nextLong()) + else evalWatches.map(p => p.path -> Interpreter.pathSignature(p.path)) + } + (Res(res), () => interpWatched ++ watched()) + case _ => (result, () => interpWatched) } } ) @@ -104,7 +135,7 @@ class MainRunner(val config: ammonite.main.Cli.Config, | // doesn't get picked up during reflective child-module discovery | def millSelf = Some(this) | - | implicit def millDiscover: mill.define.Discover[this.type] = mill.define.Discover[this.type] + | implicit lazy val millDiscover: mill.define.Discover[this.type] = mill.define.Discover[this.type] |} | |sealed trait $wrapName extends mill.main.MainModule{ diff --git a/main/src/mill/main/RunScript.scala b/main/src/mill/main/RunScript.scala index 85930e9b..553f5b69 100644 --- a/main/src/mill/main/RunScript.scala +++ b/main/src/mill/main/RunScript.scala @@ -29,7 +29,7 @@ object RunScript{ scriptArgs: Seq[String], stateCache: Option[Evaluator.State], log: Logger) - : (Res[(Evaluator[Any], Seq[(Path, Long)], Either[String, Seq[Js.Value]])], Seq[(Path, Long)]) = { + : (Res[(Evaluator[Any], Seq[PathRef], Either[String, Seq[Js.Value]])], Seq[(Path, Long)]) = { val (evalState, interpWatched) = stateCache match{ case Some(s) if watchedSigUnchanged(s.watched) => Res.Success(s) -> s.watched @@ -58,18 +58,7 @@ object RunScript{ evaluator <- evalRes (evalWatches, res) <- Res(evaluateTasks(evaluator, scriptArgs, multiSelect = false)) } yield { - val alreadyStale = evalWatches.exists(p => p.sig != PathRef(p.path, p.quick).sig) - // If the file changed between the creation of the original - // `PathRef` and the current moment, use random junk .sig values - // to force an immediate re-run. Otherwise calculate the - // pathSignatures the same way Ammonite would and hand over the - // values, so Ammonite can watch them and only re-run if they - // subsequently change - val evaluationWatches = - if (alreadyStale) evalWatches.map(_.path -> util.Random.nextLong()) - else evalWatches.map(p => p.path -> Interpreter.pathSignature(p.path)) - - (evaluator, evaluationWatches, res.map(_.flatMap(_._2))) + (evaluator, evalWatches, res.map(_.flatMap(_._2))) } (evaluated, interpWatched) } diff --git a/main/test/src/mill/eval/ModuleTests.scala b/main/test/src/mill/eval/ModuleTests.scala index d2fa7184..f089a251 100644 --- a/main/test/src/mill/eval/ModuleTests.scala +++ b/main/test/src/mill/eval/ModuleTests.scala @@ -13,7 +13,7 @@ object ModuleTests extends TestSuite{ object inner extends mill.Module{ def y = T{17} } - def millDiscover = Discover[this.type] + lazy val millDiscover = Discover[this.type] } object Build extends TestUtil.BaseModule{ def z = T{ ExternalModule.x() + ExternalModule.inner.y() } @@ -36,7 +36,7 @@ object ModuleTests extends TestSuite{ object Build extends mill.define.ExternalModule { def z = T{ ExternalModule.x() + ExternalModule.inner.y() } - def millDiscover = Discover[this.type] + lazy val millDiscover = Discover[this.type] } intercept[java.lang.AssertionError]{ Build } diff --git a/main/test/src/mill/util/TestGraphs.scala b/main/test/src/mill/util/TestGraphs.scala index 750ef015..11f72d02 100644 --- a/main/test/src/mill/util/TestGraphs.scala +++ b/main/test/src/mill/util/TestGraphs.scala @@ -180,7 +180,7 @@ object TestGraphs{ object canOverrideSuper extends TestUtil.BaseModule with BaseModule { override def foo = T{ super.foo() ++ Seq("object") } override def cmd(i: Int) = T.command{ super.cmd(i)() ++ Seq("object" + i) } - def millDiscover: Discover[this.type] = Discover[this.type] + override lazy val millDiscover: Discover[this.type] = Discover[this.type] } trait TraitWithModule extends Module{ outer => @@ -193,7 +193,7 @@ object TestGraphs{ // Make sure nested objects inherited from traits work object TraitWithModuleObject extends TestUtil.BaseModule with TraitWithModule{ - def millDiscover: Discover[this.type] = Discover[this.type] + override lazy val millDiscover: Discover[this.type] = Discover[this.type] } diff --git a/main/test/src/mill/util/TestUtil.scala b/main/test/src/mill/util/TestUtil.scala index b30d5d51..d06c7f6a 100644 --- a/main/test/src/mill/util/TestUtil.scala +++ b/main/test/src/mill/util/TestUtil.scala @@ -32,7 +32,7 @@ object TestUtil { millName0: sourcecode.Name, overrides: Overrides) extends mill.define.BaseModule(getSrcPathBase() / millModuleEnclosing0.value.split("\\.| |#")){ - def millDiscover: Discover[this.type] = Discover[this.type] + lazy val millDiscover: Discover[this.type] = Discover[this.type] } object test{ |