[Commonizer] Replace InputTargetsSelectorInterface with a simple function
^KT-47301
This commit is contained in:
committed by
Space
parent
5a8b39e08d
commit
770efb7ecb
@@ -22,8 +22,7 @@ internal fun CommonizerQueue(parameters: CommonizerParameters): CommonizerQueue
|
|||||||
CommonizerQueue.Deserializer { deserializeTarget(parameters, target) }
|
CommonizerQueue.Deserializer { deserializeTarget(parameters, target) }
|
||||||
},
|
},
|
||||||
commonizer = { inputs, output -> commonizeTarget(parameters, inputs, output) },
|
commonizer = { inputs, output -> commonizeTarget(parameters, inputs, output) },
|
||||||
serializer = { declarations, outputTarget -> serializeTarget(parameters, declarations, outputTarget) },
|
serializer = { declarations, outputTarget -> serializeTarget(parameters, declarations, outputTarget) }
|
||||||
inputTargetsSelector = DefaultInputTargetsSelector
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +32,6 @@ internal class CommonizerQueue(
|
|||||||
private val deserializers: TargetDependent<Deserializer>,
|
private val deserializers: TargetDependent<Deserializer>,
|
||||||
private val commonizer: Commonizer,
|
private val commonizer: Commonizer,
|
||||||
private val serializer: Serializer,
|
private val serializer: Serializer,
|
||||||
private val inputTargetsSelector: InputTargetsSelector
|
|
||||||
) {
|
) {
|
||||||
|
|
||||||
fun interface Deserializer {
|
fun interface Deserializer {
|
||||||
@@ -116,7 +114,7 @@ internal class CommonizerQueue(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun registerTargetDependencies(outputTarget: OutputCommonizerTarget) {
|
private fun registerTargetDependencies(outputTarget: OutputCommonizerTarget) {
|
||||||
targetDependencies[outputTarget] = inputTargetsSelector(outputTargets + deserializers.targets, outputTarget)
|
targetDependencies[outputTarget] = selectInputTargets(outputTargets + deserializers.targets, outputTarget)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun removeTargetDependencies(target: OutputCommonizerTarget) {
|
private fun removeTargetDependencies(target: OutputCommonizerTarget) {
|
||||||
@@ -140,4 +138,4 @@ internal class CommonizerQueue(
|
|||||||
init {
|
init {
|
||||||
outputTargets.forEach(this::enqueue)
|
outputTargets.forEach(this::enqueue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,34 +8,20 @@ package org.jetbrains.kotlin.commonizer
|
|||||||
import org.jetbrains.kotlin.commonizer.utils.isProperSubsetOf
|
import org.jetbrains.kotlin.commonizer.utils.isProperSubsetOf
|
||||||
import org.jetbrains.kotlin.commonizer.utils.isSubsetOf
|
import org.jetbrains.kotlin.commonizer.utils.isSubsetOf
|
||||||
|
|
||||||
internal fun interface InputTargetsSelector {
|
internal fun selectInputTargets(inputTargets: Set<CommonizerTarget>, outputTarget: SharedCommonizerTarget): Set<CommonizerTarget> {
|
||||||
operator fun invoke(inputTargets: Set<CommonizerTarget>, outputTarget: SharedCommonizerTarget): Set<CommonizerTarget>
|
val subsetInputTargets = inputTargets
|
||||||
}
|
.filter { inputTarget -> inputTarget != outputTarget && inputTarget.allLeaves() isSubsetOf outputTarget.allLeaves() }
|
||||||
|
.sortedBy { it.allLeaves().size }
|
||||||
|
|
||||||
internal operator fun InputTargetsSelector.invoke(
|
val disjointSubsetInputTargets = subsetInputTargets
|
||||||
parameters: CommonizerParameters,
|
.filter { inputTarget ->
|
||||||
outputTarget: SharedCommonizerTarget
|
subsetInputTargets.none { potentialSuperSet -> inputTarget.allLeaves() isProperSubsetOf potentialSuperSet.allLeaves() }
|
||||||
): Set<CommonizerTarget> {
|
|
||||||
return invoke(parameters.outputTargets + parameters.targetProviders.targets, outputTarget)
|
|
||||||
}
|
|
||||||
|
|
||||||
internal object DefaultInputTargetsSelector : InputTargetsSelector {
|
|
||||||
override fun invoke(inputTargets: Set<CommonizerTarget>, outputTarget: SharedCommonizerTarget): Set<CommonizerTarget> {
|
|
||||||
|
|
||||||
val subsetInputTargets = inputTargets
|
|
||||||
.filter { inputTarget -> inputTarget != outputTarget && inputTarget.allLeaves() isSubsetOf outputTarget.allLeaves() }
|
|
||||||
.sortedBy { it.allLeaves().size }
|
|
||||||
|
|
||||||
val disjointSubsetInputTargets = subsetInputTargets
|
|
||||||
.filter { inputTarget ->
|
|
||||||
subsetInputTargets.none { potentialSuperSet -> inputTarget.allLeaves() isProperSubsetOf potentialSuperSet.allLeaves() }
|
|
||||||
}
|
|
||||||
|
|
||||||
return outputTarget.allLeaves().fold(setOf()) { selectedInputTargets, outputLeafTarget ->
|
|
||||||
if (outputLeafTarget in selectedInputTargets.allLeaves()) return@fold selectedInputTargets
|
|
||||||
selectedInputTargets + (disjointSubsetInputTargets.firstOrNull { inputTarget -> outputLeafTarget in inputTarget.allLeaves() }
|
|
||||||
?: failedSelectingInputTargets(inputTargets, outputTarget))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return outputTarget.allLeaves().fold(setOf()) { selectedInputTargets, outputLeafTarget ->
|
||||||
|
if (outputLeafTarget in selectedInputTargets.allLeaves()) return@fold selectedInputTargets
|
||||||
|
selectedInputTargets + (disjointSubsetInputTargets.firstOrNull { inputTarget -> outputLeafTarget in inputTarget.allLeaves() }
|
||||||
|
?: failedSelectingInputTargets(inputTargets, outputTarget))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,4 +31,4 @@ private fun failedSelectingInputTargets(inputTargets: Set<CommonizerTarget>, out
|
|||||||
"inputTargets=$inputTargets\n" +
|
"inputTargets=$inputTargets\n" +
|
||||||
"missing leaf targets: ${outputTarget.allLeaves() - inputTargets.allLeaves()}"
|
"missing leaf targets: ${outputTarget.allLeaves() - inputTargets.allLeaves()}"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ class CommonizerQueueTest {
|
|||||||
) { CommonizerQueue.Deserializer { null } },
|
) { CommonizerQueue.Deserializer { null } },
|
||||||
commonizer = { _, _ -> null },
|
commonizer = { _, _ -> null },
|
||||||
serializer = { _, _ -> },
|
serializer = { _, _ -> },
|
||||||
inputTargetsSelector = DefaultInputTargetsSelector
|
|
||||||
)
|
)
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
@@ -104,7 +103,6 @@ class CommonizerQueueTest {
|
|||||||
CirRootNode(CommonizedGroup(0), storageManager.createNullableLazyValue { CirRoot.create(output) })
|
CirRootNode(CommonizedGroup(0), storageManager.createNullableLazyValue { CirRoot.create(output) })
|
||||||
},
|
},
|
||||||
serializer = { _, _ -> },
|
serializer = { _, _ -> },
|
||||||
inputTargetsSelector = DefaultInputTargetsSelector
|
|
||||||
)
|
)
|
||||||
|
|
||||||
queue.invokeAll()
|
queue.invokeAll()
|
||||||
@@ -121,14 +119,14 @@ class CommonizerQueueTest {
|
|||||||
|
|
||||||
val abInvocation = commonizerInvocations.single { it.output == abOutputTarget }
|
val abInvocation = commonizerInvocations.single { it.output == abOutputTarget }
|
||||||
assertEquals(
|
assertEquals(
|
||||||
DefaultInputTargetsSelector(providedTargets + outputTargets, abOutputTarget),
|
selectInputTargets(providedTargets + outputTargets, abOutputTarget),
|
||||||
abInvocation.inputs.targets.toSet(),
|
abInvocation.inputs.targets.toSet(),
|
||||||
"Expected commonizer being invoked with selected targets for abInvocation"
|
"Expected commonizer being invoked with selected targets for abInvocation"
|
||||||
)
|
)
|
||||||
|
|
||||||
val abcInvocation = commonizerInvocations.single { it.output == abcOutputTarget }
|
val abcInvocation = commonizerInvocations.single { it.output == abcOutputTarget }
|
||||||
assertEquals(
|
assertEquals(
|
||||||
DefaultInputTargetsSelector(providedTargets + outputTargets, abcOutputTarget),
|
selectInputTargets(providedTargets + outputTargets, abcOutputTarget),
|
||||||
abcInvocation.inputs.targets.toSet(),
|
abcInvocation.inputs.targets.toSet(),
|
||||||
"Expected commonizer being invoked with selected targets for abcInvocation"
|
"Expected commonizer being invoked with selected targets for abcInvocation"
|
||||||
)
|
)
|
||||||
|
|||||||
+12
-12
@@ -10,14 +10,14 @@ import kotlin.test.assertEquals
|
|||||||
import kotlin.test.assertFailsWith
|
import kotlin.test.assertFailsWith
|
||||||
import kotlin.test.assertTrue
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
class DefaultInputTargetsSelectorTest {
|
class SelectInputTargetsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `missing leaf targets`() {
|
fun `missing leaf targets`() {
|
||||||
val inputTargets = setOf(LeafCommonizerTarget("a"), LeafCommonizerTarget("b"))
|
val inputTargets = setOf(LeafCommonizerTarget("a"), LeafCommonizerTarget("b"))
|
||||||
|
|
||||||
val exception = assertFailsWith<IllegalArgumentException> {
|
val exception = assertFailsWith<IllegalArgumentException> {
|
||||||
DefaultInputTargetsSelector(inputTargets, parseCommonizerTarget("(a, b, c)") as SharedCommonizerTarget)
|
selectInputTargets(inputTargets, parseCommonizerTarget("(a, b, c)") as SharedCommonizerTarget)
|
||||||
}
|
}
|
||||||
|
|
||||||
assertTrue(
|
assertTrue(
|
||||||
@@ -43,17 +43,17 @@ class DefaultInputTargetsSelectorTest {
|
|||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
setOf(LeafCommonizerTarget("a"), LeafCommonizerTarget("b")),
|
setOf(LeafCommonizerTarget("a"), LeafCommonizerTarget("b")),
|
||||||
DefaultInputTargetsSelector(inputTargets, parseCommonizerTarget("(a, b)") as SharedCommonizerTarget)
|
selectInputTargets(inputTargets, parseCommonizerTarget("(a, b)") as SharedCommonizerTarget)
|
||||||
)
|
)
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
setOf(LeafCommonizerTarget("a"), LeafCommonizerTarget("b"), LeafCommonizerTarget("c")),
|
setOf(LeafCommonizerTarget("a"), LeafCommonizerTarget("b"), LeafCommonizerTarget("c")),
|
||||||
DefaultInputTargetsSelector(inputTargets, parseCommonizerTarget("(a, b, c)") as SharedCommonizerTarget)
|
selectInputTargets(inputTargets, parseCommonizerTarget("(a, b, c)") as SharedCommonizerTarget)
|
||||||
)
|
)
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
setOf(LeafCommonizerTarget("a"), LeafCommonizerTarget("b"), parseCommonizerTarget("(c, d)")),
|
setOf(LeafCommonizerTarget("a"), LeafCommonizerTarget("b"), parseCommonizerTarget("(c, d)")),
|
||||||
DefaultInputTargetsSelector(inputTargets, parseCommonizerTarget("(a, b, c, d)") as SharedCommonizerTarget)
|
selectInputTargets(inputTargets, parseCommonizerTarget("(a, b, c, d)") as SharedCommonizerTarget)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,17 +70,17 @@ class DefaultInputTargetsSelectorTest {
|
|||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
setOf("(a, b, c, d)", "f").map(::parseCommonizerTarget).toSet(),
|
setOf("(a, b, c, d)", "f").map(::parseCommonizerTarget).toSet(),
|
||||||
DefaultInputTargetsSelector(inputTargets, parseCommonizerTarget("(a, b, c, d, f)") as SharedCommonizerTarget)
|
selectInputTargets(inputTargets, parseCommonizerTarget("(a, b, c, d, f)") as SharedCommonizerTarget)
|
||||||
)
|
)
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
setOf("(a, b, c, d)", "(c, d, e)").map(::parseCommonizerTarget).toSet(),
|
setOf("(a, b, c, d)", "(c, d, e)").map(::parseCommonizerTarget).toSet(),
|
||||||
DefaultInputTargetsSelector(inputTargets, parseCommonizerTarget("(a, b, c, d, e)") as SharedCommonizerTarget)
|
selectInputTargets(inputTargets, parseCommonizerTarget("(a, b, c, d, e)") as SharedCommonizerTarget)
|
||||||
)
|
)
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
setOf("(a, b, c, d)", "(c, d, e)", "f").map(::parseCommonizerTarget).toSet(),
|
setOf("(a, b, c, d)", "(c, d, e)", "f").map(::parseCommonizerTarget).toSet(),
|
||||||
DefaultInputTargetsSelector(inputTargets, parseCommonizerTarget("(a, b, c, d, e, f)") as SharedCommonizerTarget)
|
selectInputTargets(inputTargets, parseCommonizerTarget("(a, b, c, d, e, f)") as SharedCommonizerTarget)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +88,7 @@ class DefaultInputTargetsSelectorTest {
|
|||||||
fun `empty outputTarget`() {
|
fun `empty outputTarget`() {
|
||||||
assertEquals(
|
assertEquals(
|
||||||
emptySet(),
|
emptySet(),
|
||||||
DefaultInputTargetsSelector(emptySet(), parseCommonizerTarget("()") as SharedCommonizerTarget)
|
selectInputTargets(emptySet(), parseCommonizerTarget("()") as SharedCommonizerTarget)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,7 +96,7 @@ class DefaultInputTargetsSelectorTest {
|
|||||||
fun `single leaf outputTarget`() {
|
fun `single leaf outputTarget`() {
|
||||||
assertEquals(
|
assertEquals(
|
||||||
setOf(LeafCommonizerTarget("a")),
|
setOf(LeafCommonizerTarget("a")),
|
||||||
DefaultInputTargetsSelector(setOf(LeafCommonizerTarget("a")), parseCommonizerTarget("(a)") as SharedCommonizerTarget)
|
selectInputTargets(setOf(LeafCommonizerTarget("a")), parseCommonizerTarget("(a)") as SharedCommonizerTarget)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,10 +104,10 @@ class DefaultInputTargetsSelectorTest {
|
|||||||
fun `exact output available`() {
|
fun `exact output available`() {
|
||||||
assertEquals(
|
assertEquals(
|
||||||
setOf(parseCommonizerTarget("(a, b)"), parseCommonizerTarget("(c, d)")),
|
setOf(parseCommonizerTarget("(a, b)"), parseCommonizerTarget("(c, d)")),
|
||||||
DefaultInputTargetsSelector(
|
selectInputTargets(
|
||||||
setOf("a", "b", "c", "d", "(a, b)", "(c, d)", "(a, b, c, d)").map(::parseCommonizerTarget).toSet(),
|
setOf("a", "b", "c", "d", "(a, b)", "(c, d)", "(a, b, c, d)").map(::parseCommonizerTarget).toSet(),
|
||||||
parseCommonizerTarget("(a, b, c, d)") as SharedCommonizerTarget
|
parseCommonizerTarget("(a, b, c, d)") as SharedCommonizerTarget
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user