Skip to content

Commit 3588519

Browse files
committed
refactor: remove ProceduralPath.evaluations
1 parent d0e8c6c commit 3588519

File tree

10 files changed

+40
-42
lines changed

10 files changed

+40
-42
lines changed

insight/src/main/kotlin/spp/jetbrains/insight/InsightKeys.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import spp.protocol.insight.InsightValue
2525
*/
2626
object InsightKeys {
2727

28-
val RUNTIME_PATHS = SourceKey<Set<ProceduralPath>>("RUNTIME_PATHS")
28+
val PROCEDURAL_PATHS = SourceKey<List<ProceduralPath>>("PROCEDURAL_PATHS")
2929

3030
val FUNCTION_DURATION = SourceKey<InsightValue<Long>>(InsightType.FUNCTION_DURATION.name)
3131
val FUNCTION_DURATION_PREDICTION = SourceKey<InsightValue<Long>>(InsightType.FUNCTION_DURATION_PREDICTION.name)

insight/src/main/kotlin/spp/jetbrains/insight/InsightPassProvider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ class InsightPassProvider {
8888
pathPasses.forEach { it.analyze(path) }
8989
}
9090

91-
fun analyze(pathSet: Set<ProceduralPath>): Set<ProceduralPath> {
91+
fun analyze(pathSet: List<ProceduralPath>): List<ProceduralPath> {
9292
val preProcessedPathSet = pathSetPasses.fold(pathSet) { acc, pass ->
9393
pass.preProcess(acc)
9494
}
95-
preProcessedPathSet.map { analyze(it) }.toSet()
95+
preProcessedPathSet.map { analyze(it) }
9696
val analyzedPathSetSet = pathSetPasses.fold(preProcessedPathSet) { acc, pass ->
9797
pass.analyze(acc)
9898
}

insight/src/main/kotlin/spp/jetbrains/insight/ProceduralAnalyzer.kt

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import spp.jetbrains.artifact.model.*
2020
import spp.jetbrains.artifact.service.getParentFunction
2121
import spp.jetbrains.artifact.service.toArtifact
2222
import java.util.*
23-
import java.util.concurrent.atomic.AtomicInteger
2423
import java.util.stream.Collectors
2524
import java.util.stream.IntStream
2625

@@ -36,20 +35,20 @@ class ProceduralAnalyzer {
3635
*
3736
* @param element the [ArtifactElement] to analyze
3837
*/
39-
fun analyze(element: ArtifactElement): Set<ProceduralPath> {
38+
fun analyze(element: ArtifactElement): List<ProceduralPath> {
4039
val parent = mutableListOf<Any>()
4140
walkDown(element, parent)
4241

4342
val ifCount = getIfArtifactCount(parent, 0)
4443
val boolPermutations = makeBoolPermutations(ifCount)
45-
val paths = mutableSetOf<ProceduralPath>()
46-
for (boolSet in boolPermutations) {
47-
val path = ProceduralPath(boolSet.toList(), element, mutableListOf())
48-
processPath(path, path.artifacts, parent, AtomicInteger(0))
44+
val paths = mutableListOf<ProceduralPath>()
45+
for (boolArray in boolPermutations) {
46+
val path = ProceduralPath(element, mutableListOf())
47+
processPath(path, path.artifacts, parent, boolArray.iterator())
4948
paths.add(path)
5049
}
5150

52-
return passProvider.analyze(paths.toSet())
51+
return passProvider.analyze(paths)
5352
}
5453

5554
/**
@@ -58,12 +57,12 @@ class ProceduralAnalyzer {
5857
*
5958
* @param element the [ArtifactElement] to analyze
6059
*/
61-
fun analyzeUp(element: ArtifactElement): Set<ProceduralPath> {
60+
fun analyzeUp(element: ArtifactElement): List<ProceduralPath> {
6261
//todo: do this more efficiently
6362
val paths = analyze(element.getParentFunction().toArtifact()!!)
6463
return paths.filter {
6564
it.descendants.contains(element)
66-
}.toSet()
65+
}
6766
}
6867

6968
private fun walkDown(element: ArtifactElement, parent: MutableList<Any>) {
@@ -106,7 +105,7 @@ class ProceduralAnalyzer {
106105
path: ProceduralPath,
107106
nextArtifacts: MutableList<ArtifactElement>,
108107
processArtifacts: List<Any>,
109-
boolIndex: AtomicInteger
108+
boolIterator: BooleanIterator
110109
) {
111110
processArtifacts.forEachIndexed { index, it ->
112111
var artifactElement: ArtifactElement? = null
@@ -116,19 +115,19 @@ class ProceduralAnalyzer {
116115
}
117116

118117
if (artifactElement is IfArtifact) {
119-
val bool = path.evaluations[boolIndex.getAndIncrement()]
120-
artifactElement.data.put(InsightKeys.CONDITION_EVALUATION, bool)
118+
val bool = boolIterator.next()
119+
artifactElement.data[InsightKeys.CONDITION_EVALUATION] = bool
121120

122121
if (bool) {
123122
val childArtifacts = processArtifacts[index + 1] as List<Any>
124-
processPath(path, artifactElement.childArtifacts, childArtifacts, boolIndex)
123+
processPath(path, artifactElement.childArtifacts, childArtifacts, boolIterator)
125124
} else {
126125
val childArtifacts = processArtifacts[index + 2] as List<Any>
127-
processPath(path, artifactElement.childArtifacts, childArtifacts, boolIndex)
126+
processPath(path, artifactElement.childArtifacts, childArtifacts, boolIterator)
128127
}
129128
} else if (artifactElement is LoopArtifact) {
130129
val childArtifacts = processArtifacts[index + 1] as List<Any>
131-
processPath(path, artifactElement.childArtifacts, childArtifacts, boolIndex)
130+
processPath(path, artifactElement.childArtifacts, childArtifacts, boolIterator)
132131
}
133132
}
134133
}

insight/src/main/kotlin/spp/jetbrains/insight/ProceduralPath.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import spp.protocol.insight.InsightValue
2626
* Represents an inter/intra-procedural path through the given [rootArtifact].
2727
*/
2828
data class ProceduralPath(
29-
val evaluations: List<Boolean>,
3029
val rootArtifact: ArtifactElement,
3130
val artifacts: MutableList<ArtifactElement>,
3231
internal val insights: MutableList<InsightValue<*>> = mutableListOf()
@@ -46,7 +45,7 @@ data class ProceduralPath(
4645
val conditionals = descendants.filterIsInstance<IfArtifact>()
4746
val conditions = mutableListOf<Pair<Boolean, IfArtifact>>()
4847
for (i in conditionals.indices) {
49-
conditions.add(Pair(this.evaluations[i], conditionals[i]))
48+
conditions.add(Pair(conditionals[i].getData(InsightKeys.CONDITION_EVALUATION)!!, conditionals[i]))
5049
}
5150
return conditions
5251
}

insight/src/main/kotlin/spp/jetbrains/insight/pass/ProceduralPathSetPass.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import spp.jetbrains.insight.ProceduralPath
2222
* A pass that analyzes a set of [ProceduralPath]s and adds data to them.
2323
*/
2424
interface ProceduralPathSetPass : IPass {
25-
fun preProcess(pathSet: Set<ProceduralPath>): Set<ProceduralPath> = pathSet
26-
fun analyze(pathSet: Set<ProceduralPath>): Set<ProceduralPath> = pathSet
27-
fun postProcess(pathSet: Set<ProceduralPath>): Set<ProceduralPath> = pathSet
25+
fun preProcess(paths: List<ProceduralPath>): List<ProceduralPath> = paths
26+
fun analyze(paths: List<ProceduralPath>): List<ProceduralPath> = paths
27+
fun postProcess(paths: List<ProceduralPath>): List<ProceduralPath> = paths
2828
}

insight/src/main/kotlin/spp/jetbrains/insight/pass/artifact/CallDurationPass.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ class CallDurationPass : ArtifactPass {
3636

3737
val resolvedFunction = element.getResolvedFunction()
3838
if (resolvedFunction != null) {
39-
val proceduralPaths = element.getData(InsightKeys.RUNTIME_PATHS)
40-
if (proceduralPaths != null) {
39+
val paths = element.getData(InsightKeys.PROCEDURAL_PATHS)
40+
if (paths != null) {
4141
//artifact has already been analyzed, use pre-determined duration (if available)
42-
val duration = proceduralPaths.mapNotNull {
42+
val duration = paths.mapNotNull {
4343
it.getInsights().find { it.type == PATH_DURATION }?.value as Long?
4444
}.ifNotEmpty { sum() }
4545
if (duration != null) {

insight/src/main/kotlin/spp/jetbrains/insight/pass/artifact/LoadPsiPass.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class LoadPsiPass : ArtifactPass {
3333

3434
val resolvedFunction = element.getResolvedFunction()
3535
if (resolvedFunction != null) {
36-
val paths = resolvedFunction.getUserData(InsightKeys.RUNTIME_PATHS.asPsiKey())
36+
val paths = resolvedFunction.getUserData(InsightKeys.PROCEDURAL_PATHS.asPsiKey())
3737
if (paths != null) {
38-
element.data[InsightKeys.RUNTIME_PATHS] = paths
38+
element.data[InsightKeys.PROCEDURAL_PATHS] = paths
3939
}
4040
}
4141
}

insight/src/main/kotlin/spp/jetbrains/insight/pass/pathset/SavePsiPathSetPass.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import spp.jetbrains.insight.pass.ProceduralPathSetPass
2525
*/
2626
class SavePsiPathSetPass : ProceduralPathSetPass {
2727

28-
override fun postProcess(pathSet: Set<ProceduralPath>): Set<ProceduralPath> {
29-
pathSet.forEach { it.rootArtifact.putUserData(InsightKeys.RUNTIME_PATHS.asPsiKey(), pathSet) }
30-
return super.postProcess(pathSet)
28+
override fun postProcess(paths: List<ProceduralPath>): List<ProceduralPath> {
29+
paths.forEach { it.rootArtifact.putUserData(InsightKeys.PROCEDURAL_PATHS.asPsiKey(), paths) }
30+
return super.postProcess(paths)
3131
}
3232
}

insight/src/main/kotlin/spp/jetbrains/insight/pass/pathset/SimplifyPathSetPass.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import spp.jetbrains.insight.pass.ProceduralPathSetPass
2626
*/
2727
class SimplifyPathSetPass : ProceduralPathSetPass {
2828

29-
override fun postProcess(pathSet: Set<ProceduralPath>): Set<ProceduralPath> {
30-
val simplifiedPaths = mutableSetOf<ProceduralPath>()
31-
for (path in pathSet) {
29+
override fun postProcess(paths: List<ProceduralPath>): List<ProceduralPath> {
30+
val simplifiedPaths = mutableListOf<ProceduralPath>()
31+
for (path in paths) {
3232
val possiblePath = path.artifacts.all {
3333
if (it is IfArtifact) {
3434
val probability = it.getData(InsightKeys.CONTROL_STRUCTURE_PROBABILITY)

insight/src/main/kotlin/spp/jetbrains/insight/pass/pathset/StaticDfaPathSetPass.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,29 @@ class StaticDfaPathSetPass : ProceduralPathSetPass {
4242

4343
private val log = logger<StaticDfaPathSetPass>()
4444

45-
override fun preProcess(pathSet: Set<ProceduralPath>): Set<ProceduralPath> {
46-
val project = pathSet.first().rootArtifact.project
45+
override fun preProcess(paths: List<ProceduralPath>): List<ProceduralPath> {
46+
val project = paths.first().rootArtifact.project
4747
val factory = DfaValueFactory(project)
4848
val flow = DataFlowIRProvider.forElement(
49-
(pathSet.first().rootArtifact as FunctionArtifact).bodyBlock!!.psiElement,
49+
(paths.first().rootArtifact as FunctionArtifact).bodyBlock!!.psiElement,
5050
factory
51-
) ?: return pathSet
51+
) ?: return paths
5252

5353
val listener = ConstantConditionDfaListener()
5454
val interpreter = StandardDataFlowInterpreter(flow, listener)
5555
val states = listOf(DfaMemoryStateImpl(factory))
5656
if (interpreter.interpret(states.map { s ->
5757
DfaInstructionState(flow.getInstruction(0), s)
5858
}) != RunnerResult.OK) {
59-
log.warn("Failed to interpret function ${(pathSet.first().rootArtifact as FunctionArtifact).name}")
59+
log.warn("Failed to interpret function ${(paths.first().rootArtifact as FunctionArtifact).name}")
6060
}
6161

6262
listener.constantConditions.forEach {
6363
if (it.key is KotlinAnchor.KotlinExpressionAnchor && it.value != ConstantValue.UNKNOWN) {
6464
val anchor = it.key as KotlinAnchor.KotlinExpressionAnchor
6565
val value = it.value
6666
val expression = anchor.expression
67-
pathSet.forEach {
67+
paths.forEach {
6868
it.artifacts.forEach {
6969
if (it is IfArtifact) {
7070
if (it.condition?.psiElement == expression) {
@@ -85,7 +85,7 @@ class StaticDfaPathSetPass : ProceduralPathSetPass {
8585
val anchor = it.key as JavaExpressionAnchor
8686
val value = it.value
8787
val expression = anchor.expression
88-
pathSet.forEach {
88+
paths.forEach {
8989
it.artifacts.forEach {
9090
if (it is IfArtifact) {
9191
if (it.condition?.psiElement == expression) {
@@ -105,7 +105,7 @@ class StaticDfaPathSetPass : ProceduralPathSetPass {
105105
}
106106
}
107107

108-
return pathSet
108+
return paths
109109
}
110110

111111
enum class ConstantValue {

0 commit comments

Comments
 (0)