Skip to content

Commit 69482b2

Browse files
authored
Merge pull request #228 from retronym/topic/state-set
Correctness fix for StateSet and a minor performance improvement
2 parents a651d82 + 764f93a commit 69482b2

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/main/scala/scala/async/internal/LiveVariables.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ trait LiveVariables {
143143
* A state `i` is contained in the list that is the value to which
144144
* key `j` maps iff control can flow from state `j` to state `i`.
145145
*/
146-
val cfg: Map[Int, Array[Int]] = {
146+
val cfg: IntMap[Array[Int]] = {
147147
var res = IntMap.empty[Array[Int]]
148148

149149
for (as <- asyncStates) res = res.updated(as.state, as.nextStates)
@@ -158,17 +158,16 @@ trait LiveVariables {
158158
def isPred0(state1: Int, state2: Int): Boolean =
159159
if(state1 == state2) false
160160
else if (seen.contains(state1)) false // breaks cycles in the CFG
161-
else cfg get state1 match {
162-
case Some(nextStates) =>
161+
else cfg getOrElse(state1, null) match {
162+
case null => false
163+
case nextStates =>
163164
seen += state1
164165
var i = 0
165166
while (i < nextStates.length) {
166167
if (nextStates(i) == state2 || isPred0(nextStates(i), state2)) return true
167168
i += 1
168169
}
169170
false
170-
case None =>
171-
false
172171
}
173172

174173
isPred0(state1, state2)

src/main/scala/scala/async/internal/StateSet.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ import scala.collection.JavaConverters.{asScalaIteratorConverter, iterableAsScal
1919

2020
// Set for StateIds, which are either small positive integers or -symbolID.
2121
final class StateSet {
22-
private var bitSet = new java.util.BitSet()
23-
private var caseSet = new util.HashSet[Integer]()
24-
def +=(stateId: Int): Unit = if (stateId > 0) bitSet.set(stateId) else caseSet.add(stateId)
25-
def contains(stateId: Int): Boolean = if (stateId > 0 && stateId < 1024) bitSet.get(stateId) else caseSet.contains(stateId)
22+
private val bitSet = new java.util.BitSet()
23+
private val caseSet = new util.HashSet[Integer]()
24+
def +=(stateId: Int): Unit = if (storeInBitSet(stateId)) bitSet.set(stateId) else caseSet.add(stateId)
25+
def contains(stateId: Int): Boolean = if (storeInBitSet(stateId)) bitSet.get(stateId) else caseSet.contains(stateId)
26+
private def storeInBitSet(stateId: Int) = {
27+
stateId > 0 && stateId < 1024
28+
}
2629
def iterator: Iterator[Integer] = {
2730
bitSet.stream().iterator().asScala ++ caseSet.asScala.iterator
2831
}

0 commit comments

Comments
 (0)