Skip to content

Commit

Permalink
Drastically increase performance through caching (#353)
Browse files Browse the repository at this point in the history
* Cache in ResourceLoader

* Fix scalafix

* Add java 17 tests

* Try java 17 in CI
  • Loading branch information
etspaceman authored Jul 31, 2023
1 parent 53c983e commit e010ba0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 42 deletions.
56 changes: 28 additions & 28 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
matrix:
os: [ubuntu-latest]
scala: [2.13, 3, 2.12]
java: [temurin@8]
java: [temurin@17]
runs-on: ${{ matrix.os }}
timeout-minutes: 60
steps:
Expand All @@ -38,39 +38,39 @@ jobs:
with:
fetch-depth: 0

- name: Setup Java (temurin@8)
id: setup-java-temurin-8
if: matrix.java == 'temurin@8'
- name: Setup Java (temurin@17)
id: setup-java-temurin-17
if: matrix.java == 'temurin@17'
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 8
java-version: 17
cache: sbt

- name: sbt update
if: matrix.java == 'temurin@8' && steps.setup-java-temurin-8.outputs.cache-hit == 'false'
if: matrix.java == 'temurin@17' && steps.setup-java-temurin-17.outputs.cache-hit == 'false'
run: sbt '++ ${{ matrix.scala }}' reload +update

- name: Check that workflows are up to date
run: sbt githubWorkflowCheck

- name: Check headers and formatting
if: matrix.java == 'temurin@8' && matrix.os == 'ubuntu-latest'
if: matrix.java == 'temurin@17' && matrix.os == 'ubuntu-latest'
run: sbt '++ ${{ matrix.scala }}' headerCheckAll scalafmtCheckAll 'project /' scalafmtSbtCheck

- name: Check scalafix lints
if: matrix.java == 'temurin@8' && matrix.os == 'ubuntu-latest'
if: matrix.java == 'temurin@17' && matrix.os == 'ubuntu-latest'
run: sbt '++ ${{ matrix.scala }}' 'scalafixAll --check'

- name: Test
run: sbt '++ ${{ matrix.scala }}' test

- name: Check binary compatibility
if: matrix.java == 'temurin@8' && matrix.os == 'ubuntu-latest'
if: matrix.java == 'temurin@17' && matrix.os == 'ubuntu-latest'
run: sbt '++ ${{ matrix.scala }}' mimaReportBinaryIssues

- name: Generate API documentation
if: matrix.java == 'temurin@8' && matrix.os == 'ubuntu-latest'
if: matrix.java == 'temurin@17' && matrix.os == 'ubuntu-latest'
run: sbt '++ ${{ matrix.scala }}' doc

- name: Make target directories
Expand All @@ -95,25 +95,25 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
java: [temurin@8]
java: [temurin@17]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (full)
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup Java (temurin@8)
id: setup-java-temurin-8
if: matrix.java == 'temurin@8'
- name: Setup Java (temurin@17)
id: setup-java-temurin-17
if: matrix.java == 'temurin@17'
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 8
java-version: 17
cache: sbt

- name: sbt update
if: matrix.java == 'temurin@8' && steps.setup-java-temurin-8.outputs.cache-hit == 'false'
if: matrix.java == 'temurin@17' && steps.setup-java-temurin-17.outputs.cache-hit == 'false'
run: sbt reload +update

- name: Download target directories (2.13)
Expand Down Expand Up @@ -176,25 +176,25 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
java: [temurin@8]
java: [temurin@17]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (full)
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup Java (temurin@8)
id: setup-java-temurin-8
if: matrix.java == 'temurin@8'
- name: Setup Java (temurin@17)
id: setup-java-temurin-17
if: matrix.java == 'temurin@17'
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 8
java-version: 17
cache: sbt

- name: sbt update
if: matrix.java == 'temurin@8' && steps.setup-java-temurin-8.outputs.cache-hit == 'false'
if: matrix.java == 'temurin@17' && steps.setup-java-temurin-17.outputs.cache-hit == 'false'
run: sbt reload +update

- name: Submit Dependencies
Expand All @@ -208,25 +208,25 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
java: [temurin@8]
java: [temurin@17]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (full)
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup Java (temurin@8)
id: setup-java-temurin-8
if: matrix.java == 'temurin@8'
- name: Setup Java (temurin@17)
id: setup-java-temurin-17
if: matrix.java == 'temurin@17'
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 8
java-version: 17
cache: sbt

- name: sbt update
if: matrix.java == 'temurin@8' && steps.setup-java-temurin-8.outputs.cache-hit == 'false'
if: matrix.java == 'temurin@17' && steps.setup-java-temurin-17.outputs.cache-hit == 'false'
run: sbt reload +update

- name: Generate site
Expand Down
6 changes: 3 additions & 3 deletions .mergify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ pull_request_rules:
conditions:
- author=scala-steward
- body~=labels:.*early-semver-patch
- status-success=Build and Test (ubuntu-latest, 2.13, temurin@8)
- status-success=Build and Test (ubuntu-latest, 3, temurin@8)
- status-success=Build and Test (ubuntu-latest, 2.12, temurin@8)
- status-success=Build and Test (ubuntu-latest, 2.13, temurin@17)
- status-success=Build and Test (ubuntu-latest, 3, temurin@17)
- status-success=Build and Test (ubuntu-latest, 2.12, temurin@17)
actions:
merge:
method: squash
Expand Down
3 changes: 3 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ inThisBuild(
tlCiHeaderCheck := true,
tlCiScalafmtCheck := true,
tlCiScalafixCheck := true,
githubWorkflowJavaVersions := Seq(
JavaSpec.temurin("17")
),
tlCiReleaseBranches := Seq("main"),
tlSonatypeUseLegacyHost := true,
mergifyStewardConfig := Some(
Expand Down
11 changes: 6 additions & 5 deletions src/main/scala/faker/ResourceLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@

package faker

import scala.collection.concurrent
import scala.reflect.ClassTag

import java.util.Locale

import pureconfig._

final class ResourceLoader(private[faker] val locale: Locale) {
private[faker] val cache: concurrent.Map[String, Any] =
concurrent.TrieMap.empty

private val country: Option[String] = {
val cty = Option(locale.getCountry)
if (cty.exists(_.isEmpty)) None else cty
Expand Down Expand Up @@ -58,21 +62,19 @@ final class ResourceLoader(private[faker] val locale: Locale) {
.withFallback(langConf.optional)
.withFallback(defaultConfig)
case (Some(langConf), _) => langConf.optional.withFallback(defaultConfig)
// $COVERAGE-OFF$
case (_, Some(localeConf)) =>
localeConf.optional.withFallback(defaultConfig)
case _ => defaultConfig
// $COVERAGE-ON$
}

@SuppressWarnings(Array("scalafix:DisableSyntax.asInstanceOf"))
def loadKey[A: ClassTag](key: String)(implicit
CR: ConfigReader[A]
): A =
conf.at(key).loadOrThrow[A]
cache.getOrElseUpdate(key, conf.at(key).loadOrThrow[A]).asInstanceOf[A]
}

object ResourceLoader {
// $COVERAGE-OFF$
val default: ResourceLoader = new ResourceLoader(SupportedLocales.default)

val ar: ResourceLoader = new ResourceLoader(SupportedLocales.ar)
Expand Down Expand Up @@ -134,5 +136,4 @@ object ResourceLoader {
object Implicits {
implicit val defaultResourceLoader: ResourceLoader = default
}
// $COVERAGE-ON$
}
6 changes: 0 additions & 6 deletions src/test/scala/faker/FakerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ import weaver._
import weaver.scalacheck._

trait FakerSpec extends SimpleIOSuite with Checkers {
override lazy val checkConfig =
super.checkConfig.copy(
minimumSuccessful = 3,
maximumDiscardRatio = 40
)

def doTest[A, B](
desc: String,
fakerF: Faker => B,
Expand Down

0 comments on commit e010ba0

Please sign in to comment.