Skip to content

Commit b7e3278

Browse files
perf(logging): cache colorEnabled instead of recomputing it per log line
colorEnabled re-read sys.env on every single log call
1 parent 55b814d commit b7e3278

8 files changed

Lines changed: 28 additions & 24 deletions

File tree

maven/src/main/scala/stryker4s/log/MavenMojoLogger.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ class MavenMojoLogger(mavenLogger: Log) extends Logger {
3131
case Level.Error => if mavenLogger.isErrorEnabled() then onError
3232
}
3333

34-
override val colorEnabled = MessageUtils.isColorEnabled() && !sys.env.contains("NO_COLOR")
34+
override lazy val colorEnabled = MessageUtils.isColorEnabled() && !sys.env.contains("NO_COLOR")
3535
}

maven/src/main/scala/stryker4s/maven/Stryker4sMavenRunner.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ class Stryker4sMavenRunner(
110110
val binaryVersion = ScalaVersions.binaryVersion(ScalaVersions.fullVersionUnsafe(project))
111111
resolver
112112
.resolveTransitively(s"io.stryker-mutator:stryker4s-testrunner_$binaryVersion:$version")
113-
.map(_.map(f => Path.fromNioPath(f.toPath)))
114113
}
115114

116115
/** JVM options for the forked test-runner: the parent's system properties, minus platform/internal ones (mirrors the

maven/src/main/scala/stryker4s/maven/runner/ArtifactResolver.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package stryker4s.maven.runner
22

33
import cats.effect.IO
4+
import fs2.io.file.Path
45
import org.apache.maven.execution.MavenSession
56
import org.apache.maven.project.MavenProject
67
import org.eclipse.aether.RepositorySystem
@@ -12,7 +13,6 @@ import org.eclipse.aether.util.artifact.JavaScopes
1213
import org.eclipse.aether.util.filter.DependencyFilterUtils
1314
import stryker4s.log.Logger
1415

15-
import java.io.File
1616
import scala.jdk.CollectionConverters.*
1717

1818
/** Resolves Maven artifacts (and their transitive runtime dependencies) at plugin runtime via Aether, against the
@@ -28,15 +28,15 @@ class ArtifactResolver(project: MavenProject, session: MavenSession, repoSystem:
2828
* @param coordinates
2929
* `groupId:artifactId[:extension[:classifier]]:version`
3030
*/
31-
def resolveArtifact(coordinates: String): IO[File] = {
31+
def resolveArtifact(coordinates: String): IO[Path] = {
3232
log.debug(s"Resolving artifact $coordinates")
3333
val request = new ArtifactRequest(new DefaultArtifact(coordinates), repositories, null)
3434
IO.blocking:
35-
repoSystem.resolveArtifact(repoSession, request).getArtifact().getFile()
35+
Path.fromNioPath(repoSystem.resolveArtifact(repoSession, request).getArtifact().getPath())
3636
}
3737

3838
/** Resolve an artifact together with all of its transitive runtime dependencies. */
39-
def resolveTransitively(coordinates: String): IO[Seq[File]] = {
39+
def resolveTransitively(coordinates: String): IO[Seq[Path]] = {
4040
log.debug(s"Resolving $coordinates with transitive dependencies")
4141
val root = new Dependency(new DefaultArtifact(coordinates), JavaScopes.RUNTIME)
4242
val collectRequest = new CollectRequest(root, repositories)
@@ -49,6 +49,6 @@ class ArtifactResolver(project: MavenProject, session: MavenSession, repoSystem:
4949
.getArtifactResults()
5050
.asScala
5151
.toSeq
52-
.map(_.getArtifact().getFile())
52+
.map(a => Path.fromNioPath(a.getArtifact().getPath()))
5353
}
5454
}

maven/src/main/scala/stryker4s/maven/runner/ZincCompiler.scala

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,22 @@ object ZincCompiler {
116116
.resolveTransitively(compilerCoords)
117117
.toResource
118118
.flatMap(makeScalaInstance(scalaVersion, _))
119-
scalac <- resolver.resolveArtifact(bridgeCoords).map(ZincUtil.scalaCompiler(scalaInstance, _)).toResource
119+
scalac <- resolver
120+
.resolveArtifact(bridgeCoords)
121+
.map(p => ZincUtil.scalaCompiler(scalaInstance, p.toNioPath))
122+
.toResource
120123
compilers = ZincUtil.compilers(scalaInstance, ClasspathOptionsUtil.boot(), None, scalac)
121124
} yield new ZincCompiler(scalaInstance, compilers)
122125
}
123126

124-
def makeScalaInstance(scalaVersion: String, scalaCompilerClasspath: Seq[File]): Resource[IO, ScalaInstance] = {
125-
val allJars = scalaCompilerClasspath.filterNot { jar =>
126-
val name = jar.getName()
127-
name.startsWith("compiler-interface") || name.startsWith("util-interface")
128-
}.toArray
127+
def makeScalaInstance(scalaVersion: String, scalaCompilerClasspath: Seq[Path]): Resource[IO, ScalaInstance] = {
128+
val allJars = scalaCompilerClasspath
129+
.filterNot { jar =>
130+
val name = jar.fileName.toString
131+
name.startsWith("compiler-interface") || name.startsWith("util-interface")
132+
}
133+
.map(_.toNioPath.toFile())
134+
.toArray
129135
val libraryJars = allJars.filter { jar =>
130136
val name = jar.getName()
131137
name.startsWith("scala-library") || name.startsWith("scala3-library") || name.startsWith("scala-reflect")

maven/src/test/scala/stryker4s/maven/runner/ZincCompilerTest.scala

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package stryker4s.maven.runner
22

33
import cats.effect.IO
4+
import fs2.io.file.Path
45
import stryker4s.testkit.Stryker4sIOSuite
56

6-
import java.io.File
7-
87
class ZincCompilerTest extends Stryker4sIOSuite {
98

109
private val jars = Seq(
11-
new File("scala-library-2.13.18.jar"),
12-
new File("scala3-library_3-3.3.4.jar"),
13-
new File("scala-reflect-2.13.18.jar"),
14-
new File("scala-compiler-2.13.18.jar"),
15-
new File("compiler-interface-2.0.0.jar"),
16-
new File("util-interface-2.0.0.jar")
10+
Path("scala-library-2.13.18.jar"),
11+
Path("scala3-library_3-3.3.4.jar"),
12+
Path("scala-reflect-2.13.18.jar"),
13+
Path("scala-compiler-2.13.18.jar"),
14+
Path("compiler-interface-2.0.0.jar"),
15+
Path("util-interface-2.0.0.jar")
1716
)
1817

1918
test("makeScalaInstance excludes the compiler-interface and util-interface jars from allJars") {

modules/api/src/main/scala/stryker4s/log/Logger.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ trait Logger {
3535
/** Whether colors are enabled in the log. Loggers can override this, a build tool might provide a flag to
3636
* disable/enable colors
3737
*/
38-
protected def colorEnabled: Boolean = {
38+
protected lazy val colorEnabled: Boolean = {
3939

4040
// Explicitly disable color https://no-color.org/
4141
val notNoColor = !sys.env.contains("NO_COLOR")

modules/mill/src/main/scala/stryker4s/log/MillLogger.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ class MillLogger(millLogger: MillInternalLogger, env: Map[String, String]) exten
2525
e.printStackTrace(pw)
2626
writer.toString()
2727

28-
override protected def colorEnabled: Boolean =
28+
override protected lazy val colorEnabled: Boolean =
2929
mill.api.daemon.loggerColorEnabled(millLogger) && !env.contains("NO_COLOR")
3030
}

modules/testkit/src/main/scala/stryker4s/testkit/TestLogger.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ protected[testkit] class TestLogger(printLogs: Boolean) extends Logger {
3737
}
3838

3939
// Always log with colors so we can test for color codes
40-
override val colorEnabled = true
40+
override lazy val colorEnabled = true
4141

4242
}

0 commit comments

Comments
 (0)