Skip to content

Commit fa24227

Browse files
diesalblalrytz
authored andcommitted
Replace a list of booleans by a mutable BitSet.
The modified code saves, for each element of a list, a boolean state variable that is modified before an operation and restored afterwards. The code was using a `List.map`, which creates a linked list with the same length as the input list, with as many Boolean objects. We change the code to use a BitSet instead, which needs less memory.
1 parent 49c14ad commit fa24227

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/reflect/scala/reflect/internal/Types.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4848,11 +4848,20 @@ trait Types
48484848
// sides of a subtyping/equality judgement, which can lead to recursive types
48494849
// being constructed. See pos/t0851 for a situation where this happens.
48504850
@inline final def suspendingTypeVars[T](tvs: List[TypeVar])(op: => T): T = {
4851-
val saved = tvs map (_.suspended)
4851+
val saved = bitSetByPredicate(tvs)(_.suspended)
48524852
tvs foreach (_.suspended = true)
48534853

48544854
try op
4855-
finally foreach2(tvs, saved)(_.suspended = _)
4855+
finally {
4856+
var index = 0
4857+
var sss = tvs
4858+
while (sss != Nil) {
4859+
val tv = sss.head
4860+
tv.suspended = saved(index)
4861+
index += 1
4862+
sss = sss.tail
4863+
}
4864+
}
48564865
}
48574866

48584867
final def stripExistentialsAndTypeVars(ts: List[Type], expandLazyBaseType: Boolean = false): (List[Type], List[Symbol]) = {

src/reflect/scala/reflect/internal/util/Collections.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,19 @@ trait Collections {
301301
}
302302
}
303303

304+
final def bitSetByPredicate[A](xs: List[A])(pred: A => Boolean): mutable.BitSet = {
305+
val bs = new mutable.BitSet()
306+
var ys = xs
307+
var i: Int = 0
308+
while (! ys.isEmpty){
309+
if (pred(ys.head))
310+
bs.add(i)
311+
ys = ys.tail
312+
i += 1
313+
}
314+
bs
315+
}
316+
304317
final def sequence[A](as: List[Option[A]]): Option[List[A]] = {
305318
if (as.exists (_.isEmpty)) None
306319
else Some(as.flatten)

0 commit comments

Comments
 (0)