Skip to content

Commit

Permalink
Improve compile-time optimizations (zio#9021)
Browse files Browse the repository at this point in the history
* Enable inlining from scala std lib and all of zio

* Enable optimizers when running mima checks
  • Loading branch information
kyri-petrou authored Jul 19, 2024
1 parent 0e799a9 commit 63980f2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ jobs:
uses: coursier/cache-action@v6
- name: Mima Checks
if: ${{ !startsWith(matrix.scala, '3.') }}
run: sbt -v ++${{ matrix.scala }} mimaChecks
# Enable optimizers as inlining might affect binary compatibility in rare cases
run: CI_RELEASE_MODE=1 sbt -v ++${{ matrix.scala }} mimaChecks
- name: Test 2.12
if: ${{ startsWith(matrix.scala, '2.12.') }}
run: sbt -v ++${{ matrix.scala }} test${{ matrix.platform }}
Expand Down Expand Up @@ -300,7 +301,8 @@ jobs:
PGP_SECRET: ${{ secrets.PGP_SECRET }}
SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }}
SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }}

CI_RELEASE_MODE: 1 # Needed to enable optimizers

publish-website:
needs: [ci]
if: github.event_name != 'pull_request'
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/internal/FiberRuntime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1521,7 +1521,7 @@ object FiberRuntime {
* For Scala 3, `-X-elide-below` is ignored, and therefore we need to use an
* '''inlinable''' build-time constant to disable assertions
*/
private final val DisableAssertions = !BuildInfo.isSnapshot
private final val DisableAssertions = BuildInfo.optimizationsEnabled

private type EvaluationSignal = Int
private object EvaluationSignal {
Expand Down
25 changes: 21 additions & 4 deletions project/BuildHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ object BuildHelper {
val Scala213: String = "2.13.13"
val Scala3: String = "3.3.3"

lazy val isRelease = {
val value = sys.env.get("CI_RELEASE_MODE").isDefined
if (value) println("Detected CI_RELEASE_MODE envvar, enabling optimizations")
value
}

private val stdOptions = Seq(
"-deprecation",
"-encoding",
Expand Down Expand Up @@ -40,7 +46,8 @@ object BuildHelper {
Seq(
"-opt:l:method",
"-opt:l:inline",
"-opt-inline-from:zio.internal.**",
"-opt-inline-from:zio.**",
"-opt-inline-from:scala.**",
// To remove calls to `assert` in releases. Assertions are level 2000
"-Xelide-below",
"2001"
Expand All @@ -51,7 +58,16 @@ object BuildHelper {
Seq(
// BuildInfoOption.ConstantValue required to disable assertions in FiberRuntime!
buildInfoOptions += BuildInfoOption.ConstantValue,
buildInfoKeys := Seq[BuildInfoKey](organization, moduleName, name, version, scalaVersion, sbtVersion, isSnapshot),
buildInfoKeys := Seq[BuildInfoKey](
organization,
moduleName,
name,
version,
scalaVersion,
sbtVersion,
isSnapshot,
BuildInfoKey("optimizationsEnabled" -> isRelease)
),
buildInfoPackage := packageName
)

Expand Down Expand Up @@ -106,7 +122,8 @@ object BuildHelper {
)
case Some((2, 13)) =>
Seq(
"-Ywarn-unused:params,-implicits"
"-Ywarn-unused:params,-implicits",
"-Ybackend-parallelism:4"
) ++ std2xOptions ++ optimizerOptions(optimize)
case Some((2, 12)) =>
Seq(
Expand Down Expand Up @@ -172,7 +189,7 @@ object BuildHelper {
name := s"$prjName",
crossScalaVersions := Seq(Scala212, Scala213, Scala3),
ThisBuild / scalaVersion := Scala213,
scalacOptions ++= stdOptions ++ extraOptions(scalaVersion.value, optimize = !isSnapshot.value),
scalacOptions ++= stdOptions ++ extraOptions(scalaVersion.value, optimize = isRelease || !isSnapshot.value),
scalacOptions --= {
if (scalaVersion.value == Scala3)
List("-Xfatal-warnings")
Expand Down

0 comments on commit 63980f2

Please sign in to comment.