Skip to content

Commit

Permalink
D2 caching and drop 2.12 (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
keynmol authored Dec 2, 2023
1 parent 0adc46c commit 842cc41
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 32 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ jobs:
- name: Pull cache
run: $SBTN pullRemoteCache-$SCALA_PLATFORM || echo 'remote cache not found'

- name: Test 2.12
run: $SBTN "test-2_12-$SCALA_PLATFORM;codeQuality-2_12-$SCALA_PLATFORM"

- name: Test 2.13
run: $SBTN "test-2_13-$SCALA_PLATFORM;codeQuality-2_13-$SCALA_PLATFORM"

Expand Down
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "3.7.14"
version = "3.7.17"

runner.dialect = scala213source3

Expand Down
17 changes: 8 additions & 9 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ val Ver = new {
val `2_13` = "2.13.12"
val `3` = "3.3.1"

val only_2 = Seq(`2_12`, `2_13`)
val only_2_13 = Seq(`2_13`)
val all = only_2 :+ `3`
val all = only_2_13 :+ `3`
}
}

Expand Down Expand Up @@ -328,7 +327,7 @@ lazy val plugin = projectMatrix
.withId("plugin")
.settings(
sbtPlugin := true,
pluginCrossBuild / sbtVersion := "1.4.4"
pluginCrossBuild / sbtVersion := "1.9.7"
)
.jvmPlatform(scalaVersions = Seq(Ver.Scala.`2_12`))
.settings(
Expand All @@ -342,12 +341,12 @@ lazy val plugin = projectMatrix
.settings(
publishLocal := publishLocal
.dependsOn(
core.jvm(Ver.Scala.`2_12`) / publishLocal,
builders.jvm(Ver.Scala.`2_12`) / publishLocal,
searchIndex.jvm(Ver.Scala.`2_12`) / publishLocal,
searchFrontendPack.jvm(Ver.Scala.`2_12`) / publishLocal,
searchShared.jvm(Ver.Scala.`2_12`) / publishLocal,
searchRetrieve.jvm(Ver.Scala.`2_12`) / publishLocal
core.jvm(Ver.Scala.`3`) / publishLocal,
builders.jvm(Ver.Scala.`3`) / publishLocal,
searchIndex.jvm(Ver.Scala.`3`) / publishLocal,
searchFrontendPack.jvm(Ver.Scala.`3`) / publishLocal,
searchShared.jvm(Ver.Scala.`3`) / publishLocal,
searchRetrieve.jvm(Ver.Scala.`3`) / publishLocal
)
.value
)
Expand Down
1 change: 1 addition & 0 deletions modules/builders/src/main/scala/Builder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package builders

trait Builder {
val contentRoot: os.Path
val cache: Cache
val assetsRoot: Option[os.Path] = None
val base: SitePath = SiteRoot
val highlighting: SyntaxHighlighting = SyntaxHighlighting.PrismJS.default
Expand Down
119 changes: 119 additions & 0 deletions modules/builders/src/main/scala/Cache.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 2020 Anton Sviridov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package subatomic.builders

import subatomic.Logger

import upickle.default._

trait Cache {
def produce[I: ReadWriter, O: ReadWriter](params: I)(compute: => O): O

def purge(): Unit

def delete[I: ReadWriter](params: I): Unit
}

object Cache {
object NoCaching extends Cache {
def produce[I: ReadWriter, O: ReadWriter](params: I)(compute: => O) =
compute

def purge(): Unit = ()
def delete[I: ReadWriter](params: I): Unit = ()
}

object InMemory extends Cache {
private val mem = scala.collection.mutable.Map.empty[Any, Any]
def produce[I: ReadWriter, O: ReadWriter](params: I)(compute: => O) =
mem.getOrElseUpdate(params, compute).asInstanceOf[O]

def purge(): Unit = mem.clear()

def delete[I: ReadWriter](params: I): Unit = mem.remove(params)
}

class TrackingCache(cache: Cache) extends Cache {
def produce[I: ReadWriter, O: ReadWriter](params: I)(compute: => O): O = {
var hit = false
val newCompute = () => {
hit = true
compute
}
val result = cache.produce(params)(newCompute())

if (hit) session += params

result
}

def purge(): Unit = {
cache.purge()
clearSession()
}

def delete[I: ReadWriter](params: I): Unit = {
cache.delete(params)
session.filterInPlace(_ == params)
}

private val session = collection.mutable.ListBuffer.empty[Any]

def clearSession(): Unit = session.clear()
def allTracked: List[Any] = session.toList

}

def track(cache: Cache): TrackingCache = new TrackingCache(cache)

def verbose(cache: Cache) = new Cache {
override def produce[I: ReadWriter, O: ReadWriter](
params: I
)(compute: => O): O = {
var miss = false
val newCompute = () => {
miss = true
compute
}
val result = cache.produce(params)(newCompute())

if (miss) Logger.default.logLine(s"Cache miss: ${params}")
else Logger.default.logLine(s"Cache hit: ${params}")

result
}

override def purge(): Unit = cache.purge()

override def delete[I: ReadWriter](params: I): Unit = cache.delete(params)

}

def labelled(label: String, cache: Cache): Cache = new Cache {
override def produce[I: ReadWriter, O: ReadWriter](params: I)(
compute: => O
): O =
cache.produce((label, params))(compute)

override def purge(): Unit = cache.purge()

override def delete[I: ReadWriter](params: I): Unit =
cache.delete((label, params))

}

}
22 changes: 13 additions & 9 deletions modules/builders/src/main/scala/D2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ import java.nio.file.Files
import com.indoorvivants.detective.Platform
import com.indoorvivants.yank.tools

class D2(binary: os.Path) {
class D2(binary: os.Path, cache: Cache) {
def diagram(code: String, arguments: List[String] = Nil): String = {
os.proc(Seq(binary.toString()) ++ arguments ++ Seq("-", "-"))
.call(
stdin = os.ProcessInput.SourceInput(code)
)
.out
.text()
def compute =
os.proc(Seq(binary.toString()) ++ arguments ++ Seq("-", "-"))
.call(
stdin = os.ProcessInput.SourceInput(code)
)
.out
.text()

cache.produce((code, arguments))(compute)
}
}

Expand All @@ -38,8 +41,9 @@ object D2 {
val default: Config = Config(version = "0.6.1")
}

def bootstrap(config: Config): D2 =
def bootstrap(config: Config, cache: Cache): D2 =
new D2(
os.Path(tools.D2.bootstrap(tools.D2.Config(version = config.version)))
os.Path(tools.D2.bootstrap(tools.D2.Config(version = config.version))),
cache
)
}
11 changes: 8 additions & 3 deletions modules/builders/src/main/scala/blog/BlogBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ case class Blog(
additionalMarkdownExtensions: Vector[Extension] = Vector.empty,
rssConfig: Option[RSSConfig] = None,
d2Config: D2.Config = D2.Config.default,
tailwindConfig: TailwindCSS.Config = TailwindCSS.Config.default
tailwindConfig: TailwindCSS.Config = TailwindCSS.Config.default,
override val cache: Cache = Cache.NoCaching
) extends subatomic.builders.Builder {
def markdownExtensions =
RelativizeLinksExtension(base.toRelPath) +:
Expand Down Expand Up @@ -273,8 +274,12 @@ object Blog {
buildConfig: cli.BuildConfig,
extra: Site[Doc] => Site[Doc]
): Unit = {
val tailwind = TailwindCSS.bootstrap(siteConfig.tailwindConfig)
val d2 = D2.bootstrap(siteConfig.d2Config)
val tailwind = TailwindCSS.bootstrap(siteConfig.tailwindConfig)
val d2 =
D2.bootstrap(
siteConfig.d2Config,
Cache.verbose(Cache.labelled("d2", siteConfig.cache))
)
val d2Resolver = BuilderSteps.d2Resolver(d2)
val renderingMarkdown = markdownParser(siteConfig, Some(d2Resolver))
// val markdown = markdownParser(siteConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ case class LibrarySite(
override val trackers: Seq[Tracker] = Seq.empty,
search: Boolean = true,
d2Config: D2.Config = D2.Config.default,
tailwindConfig: TailwindCSS.Config = TailwindCSS.Config.default
tailwindConfig: TailwindCSS.Config = TailwindCSS.Config.default,
override val cache: Cache = Cache.NoCaching
) extends subatomic.builders.Builder

object LibrarySite {
Expand Down Expand Up @@ -234,7 +235,8 @@ object LibrarySite {
extra: Site[LibrarySite.Doc] => Site[LibrarySite.Doc]
) = {
val tailwind = TailwindCSS.bootstrap(siteConfig.tailwindConfig)
val d2 = D2.bootstrap(siteConfig.d2Config)
val d2 =
D2.bootstrap(siteConfig.d2Config, Cache.labelled("d2", Cache.InMemory))

val d2Resolver = BuilderSteps.d2Resolver(d2)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
enablePlugins(SubatomicPlugin)

libraryDependencies += "org.typelevel" %% "cats-effect" % "2.2.0"
libraryDependencies += "org.typelevel" %% "cats-effect" % "3.5.0"

subatomicInheritClasspath := false

scalaVersion := "3.3.1"
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import scala.io.Source
import java.util.Properties

object Main {
def main(args: Array[String]) {
def main(args: Array[String]) = {
val props = new Properties

scala.util.Try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
enablePlugins(SubatomicPlugin)

libraryDependencies += "org.typelevel" %% "cats-effect" % "2.2.0"
libraryDependencies += "org.typelevel" %% "cats-effect" % "3.5.0"

scalaVersion := "3.3.1"
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import scala.io.Source
import java.util.Properties

object Main {
def main(args: Array[String]) {
def main(args: Array[String]) = {
val props = new Properties

props.load(Source.fromResource("subatomic.properties").reader())
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.8.3
sbt.version = 1.9.7

0 comments on commit 842cc41

Please sign in to comment.