Skip to content

Commit a112e28

Browse files
authored
perf(abg): create JARs only when they are actually requested (#1615)
In some cases, only e.g. a POM is necessary to fetch for a given action. Thanks to this change, no Kotlin compilation or JAR creation will happen when requesting an artifact that doesn't require it.
1 parent f649d84 commit a112e28

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/Main.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ private fun Route.artifact(
109109
val file = call.parameters["file"]!!
110110
if (file in bindingArtifacts) {
111111
when (val artifact = bindingArtifacts[file]) {
112-
is TextArtifact -> call.respondText(artifact.data)
112+
is TextArtifact -> call.respondText(text = artifact.data())
113113
is JarArtifact ->
114114
call.respondBytes(
115-
bytes = artifact.data,
115+
bytes = artifact.data(),
116116
contentType = ContentType.parse("application/java-archive"),
117117
)
118118

maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/JarBuilding.kt

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import kotlin.io.path.div
2222
import kotlin.io.path.writeText
2323

2424
internal data class Jars(
25-
val mainJar: ByteArray,
26-
val sourcesJar: ByteArray,
25+
val mainJar: () -> ByteArray,
26+
val sourcesJar: () -> ByteArray,
2727
)
2828

2929
internal fun buildJars(
@@ -35,20 +35,28 @@ internal fun buildJars(
3535
generateBinding(owner = owner, name = name, version = version).also {
3636
if (it.isEmpty()) return null
3737
}
38-
val (sourceFilePaths, compilationInputDir) = binding.prepareDirectoryWithSources()
3938

40-
val pathWithJarContents = compileBinding(sourceFilePaths = sourceFilePaths)
41-
val mainJarByteArrayOutputStream = ByteArrayOutputStream()
42-
mainJarByteArrayOutputStream.createZipFile(pathWithJarContents)
43-
pathWithJarContents.toFile().deleteRecursively()
39+
val mainJar by lazy {
40+
val (sourceFilePaths, compilationInputDir) = binding.prepareDirectoryWithSources()
41+
val pathWithJarContents = compileBinding(sourceFilePaths = sourceFilePaths)
42+
val mainJarByteArrayOutputStream = ByteArrayOutputStream()
43+
mainJarByteArrayOutputStream.createZipFile(pathWithJarContents)
44+
pathWithJarContents.toFile().deleteRecursively()
45+
compilationInputDir.toFile().deleteRecursively()
46+
mainJarByteArrayOutputStream.toByteArray()
47+
}
4448

45-
val sourcesJarByteArrayOutputStream = ByteArrayOutputStream()
46-
sourcesJarByteArrayOutputStream.createZipFile(compilationInputDir)
47-
compilationInputDir.toFile().deleteRecursively()
49+
val sourcesJar by lazy {
50+
val (_, compilationInputDir) = binding.prepareDirectoryWithSources()
51+
val sourcesJarByteArrayOutputStream = ByteArrayOutputStream()
52+
sourcesJarByteArrayOutputStream.createZipFile(compilationInputDir)
53+
compilationInputDir.toFile().deleteRecursively()
54+
sourcesJarByteArrayOutputStream.toByteArray()
55+
}
4856

4957
return Jars(
50-
mainJar = mainJarByteArrayOutputStream.toByteArray(),
51-
sourcesJar = sourcesJarByteArrayOutputStream.toByteArray(),
58+
mainJar = { mainJar },
59+
sourcesJar = { sourcesJar },
5260
)
5361
}
5462

maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/VersionArtifactsBuilding.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCo
55
sealed interface Artifact
66

77
data class TextArtifact(
8-
val data: String,
8+
val data: () -> String,
99
) : Artifact
1010

1111
data class JarArtifact(
12-
val data: ByteArray,
12+
val data: () -> ByteArray,
1313
) : Artifact
1414

1515
fun ActionCoords.buildVersionArtifacts(): Map<String, Artifact>? {
@@ -18,12 +18,12 @@ fun ActionCoords.buildVersionArtifacts(): Map<String, Artifact>? {
1818
val module = buildModuleFile(owner = owner, name = name.replace("__", "/"), version = version)
1919
return mapOf(
2020
"$name-$version.jar" to JarArtifact(jars.mainJar),
21-
"$name-$version.jar.md5" to TextArtifact(jars.mainJar.md5Checksum()),
21+
"$name-$version.jar.md5" to TextArtifact { jars.mainJar().md5Checksum() },
2222
"$name-$version-sources.jar" to JarArtifact(jars.sourcesJar),
23-
"$name-$version-sources.jar.md5" to TextArtifact(jars.sourcesJar.md5Checksum()),
24-
"$name-$version.pom" to TextArtifact(pom),
25-
"$name-$version.pom.md5" to TextArtifact(pom.md5Checksum()),
26-
"$name-$version.module" to TextArtifact(module),
27-
"$name-$version.module.md5" to TextArtifact(module.md5Checksum()),
23+
"$name-$version-sources.jar.md5" to TextArtifact { jars.sourcesJar().md5Checksum() },
24+
"$name-$version.pom" to TextArtifact { pom },
25+
"$name-$version.pom.md5" to TextArtifact { pom.md5Checksum() },
26+
"$name-$version.module" to TextArtifact { module },
27+
"$name-$version.module.md5" to TextArtifact { module.md5Checksum() },
2828
)
2929
}

0 commit comments

Comments
 (0)