Skip to content

Commit

Permalink
Short circuit userdev up-to-date checking on the last step when the d…
Browse files Browse the repository at this point in the history
…ev bundle hasn't changed
  • Loading branch information
jpenilla committed Nov 17, 2023
1 parent c7d0820 commit f7ff940
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class SetupHandlerImpl(
)

StepExecutor.executeSteps(
bundle.changed,
context,
extractStep,
filterVanillaJarStep,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package io.papermc.paperweight.userdev.internal.setup.step

import io.papermc.paperweight.userdev.internal.setup.SetupHandler
import io.papermc.paperweight.userdev.internal.setup.UserdevSetup
import io.papermc.paperweight.userdev.internal.setup.util.HashFunction
import io.papermc.paperweight.userdev.internal.setup.util.HashFunctionBuilder
import io.papermc.paperweight.userdev.internal.setup.util.buildHashFunction
import java.nio.file.Path
Expand Down Expand Up @@ -61,20 +62,24 @@ object StepExecutor {
private val inputOutputDataCache: MutableMap<KClass<out SetupStep>, InputOutputData> =
ConcurrentHashMap<KClass<out SetupStep>, InputOutputData>()

fun executeSteps(context: SetupHandler.Context, vararg steps: SetupStep) {
fun executeSteps(expectingChange: Boolean, context: SetupHandler.Context, vararg steps: SetupStep) {
// if we aren't expecting change, assume the last step is the output that matters
// and only verify its inputs/outputs - if it fails then we need to go back through
// and check each step anyway
if (!expectingChange) {
val lastStep = steps.last()
if (makeHashFunction(lastStep).upToDate(lastStep.hashFile)) {
return
}
}

for (step in steps) {
executeStep(context, step)
}
}

fun executeStep(context: SetupHandler.Context, step: SetupStep) {
val data = step.inputOutputData
val inputs = data.inputs.mapNotNull { it.get(step) }
val outputs = data.outputs.mapNotNull { it.get(step) }

val hashFunction = buildHashFunction(inputs, outputs) {
step.touchHashFunctionBuilder(this)
}
val hashFunction = makeHashFunction(step)

if (hashFunction.upToDate(step.hashFile)) {
return
Expand All @@ -89,6 +94,16 @@ object StepExecutor {
hashFunction.writeHash(step.hashFile)
}

private fun makeHashFunction(step: SetupStep): HashFunction {
val data = step.inputOutputData
val inputs = data.inputs.mapNotNull { it.get(step) }
val outputs = data.outputs.mapNotNull { it.get(step) }

return buildHashFunction(inputs, outputs) {
step.touchHashFunctionBuilder(this)
}
}

private val SetupStep.inputOutputData: InputOutputData
get() = inputOutputDataCache.computeIfAbsent(this::class) {
InputOutputData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class SetupHandlerImplV2(
)

StepExecutor.executeSteps(
bundle.changed,
context,
downloadMcLibs,
filterVanillaJarStep,
Expand Down

0 comments on commit f7ff940

Please sign in to comment.