Skip to content

Commit fc22398

Browse files
committed
More simplification
1 parent 5df565a commit fc22398

5 files changed

Lines changed: 14 additions & 11 deletions

File tree

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7157,7 +7157,7 @@ object Types extends TypeUtils {
71577157

71587158
object VarianceMap:
71597159
/** An immutable map representing the variance of keys of type `K` */
7160-
opaque type VarianceMap[K <: AnyRef] <: AnyRef = SimpleIdentityMap[K, Integer]
7160+
opaque type VarianceMap[K <: AnyRef] = SimpleIdentityMap[K, Integer]
71617161
def empty[K <: AnyRef]: VarianceMap[K] = SimpleIdentityMap.empty[K]
71627162
extension [K <: AnyRef](vmap: VarianceMap[K])
71637163
/** The backing map used to implement this VarianceMap. */

compiler/src/dotty/tools/dotc/inlines/Inliner.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ class Inliner(val call: tpd.Tree)(using Context):
12201220
// inlining.println(i"drop unused $bindings%, % in $tree")
12211221
val (termBindings, typeBindings) = bindings.partition(_.symbol.isTerm)
12221222
if (typeBindings.nonEmpty) {
1223-
val typeBindingsSet = typeBindings.foldLeft[SimpleIdentitySet[Symbol]](SimpleIdentitySet.empty)(_ + _.symbol)
1223+
val typeBindingsSet = SimpleIdentitySet(typeBindings.iterator.map(_.symbol))
12241224
val inlineTypeBindings = new TreeTypeMap(
12251225
typeMap = new TypeMap() {
12261226
override def apply(tp: Type): Type = tp match {

compiler/src/dotty/tools/dotc/typer/Inferencing.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ object Inferencing {
590590
constraint.upper(param).foreach(p => traverse(constraint.typeVarOfParam(p)))
591591
case _ =>
592592
}
593-
if (vmap1 eq vmap) vmap else propagate(vmap1)
593+
if (vmap1 == vmap) vmap else propagate(vmap1)
594594
}
595595

596596
propagate(accu(accu(VarianceMap.empty, tp), pt.finalResultType))
@@ -669,7 +669,7 @@ trait Inferencing { this: Typer =>
669669
// `qualifying`.
670670

671671
val ownedVars = state.ownedVars
672-
if (ownedVars ne locked) && !ownedVars.isEmpty then
672+
if (ownedVars != locked) && !ownedVars.isEmpty then
673673
val qualifying = (ownedVars -- locked).toList
674674
if (!qualifying.isEmpty) {
675675
typr.println(i"interpolate $tree: ${tree.tpe.widen} in $state, pt = $pt, owned vars = ${state.ownedVars.toList}%, %, qualifying = ${qualifying.toList}%, %, previous = ${locked.toList}%, % / ${state.constraint}")
@@ -748,7 +748,7 @@ trait Inferencing { this: Typer =>
748748
end toInstantiate
749749

750750
def typeVarsIn(xs: ToInstantiate): TypeVars =
751-
xs.foldLeft(SimpleIdentitySet.empty: TypeVars)((tvs, tvi) => tvs + tvi._1)
751+
SimpleIdentitySet(xs.iterator.map(_._1))
752752

753753
/** Filter list of proposed instantiations so that they don't constrain further
754754
* the current constraint.

compiler/src/dotty/tools/dotc/util/SimpleIdentityMap.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import scala.collection.mutable.ListBuffer
55
/** A simple linked map with `eq` as the key comparison, optimized for small maps.
66
* It has linear complexity for `apply`, `updated`, and `remove`.
77
*/
8-
final class SimpleIdentityMap[K <: AnyRef, +V <: AnyRef](bindings: Array[AnyRef]) extends (K => V | Null) {
8+
final class SimpleIdentityMap[K <: AnyRef, +V <: AnyRef](bindings: Array[AnyRef]) extends AnyVal {
99
private def key(i: Int): K = bindings(i).asInstanceOf[K]
1010

1111
private def value(i: Int): V = bindings(i + 1).asInstanceOf[V]
@@ -15,7 +15,7 @@ final class SimpleIdentityMap[K <: AnyRef, +V <: AnyRef](bindings: Array[AnyRef]
1515

1616
def size: Int = bindings.length / 2
1717

18-
Stats.record(s"SimpleIdentityMap/$size")
18+
//Stats.record(s"SimpleIdentityMap/$size")
1919

2020
def apply(k: K): V | Null = {
2121
var i = 0
@@ -122,7 +122,7 @@ final class SimpleIdentityMap[K <: AnyRef, +V <: AnyRef](bindings: Array[AnyRef]
122122

123123
def toList: List[(K, V)] = map2((k, v) => (k, v))
124124

125-
override def toString(): String = {
125+
override def toString: String = {
126126
def assocToString(key: K, value: V) = s"$key -> $value"
127127

128128
map2(assocToString) mkString("(", ", ", ")")

compiler/src/dotty/tools/dotc/util/SimpleIdentitySet.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import collection.mutable
55
/** A simple linked set with `eq` as the comparison, optimized for small sets.
66
* It has linear complexity for `contains`, `+`, and `-`.
77
*/
8-
final class SimpleIdentitySet[+Elem <: AnyRef](val xs: Array[AnyRef]) {
8+
final class SimpleIdentitySet[+Elem <: AnyRef](private val xs: Array[AnyRef]) {
99
def size: Int = xs.length
1010

1111
def +[E >: Elem <: AnyRef](x: E): SimpleIdentitySet[E] =
@@ -144,7 +144,7 @@ final class SimpleIdentitySet[+Elem <: AnyRef](val xs: Array[AnyRef]) {
144144
else this.filter(that.contains)
145145

146146
def ==[E >: Elem <: AnyRef](that: SimpleIdentitySet[E]): Boolean =
147-
(this eq that) || this.size == that.size && forall(that.contains)
147+
(this.xs eq that.xs) || this.size == that.size && forall(that.contains)
148148

149149
def !=[E >: Elem <: AnyRef](that: SimpleIdentitySet[E]): Boolean =
150150
!(this == that)
@@ -156,7 +156,10 @@ object SimpleIdentitySet {
156156
private val emptySet = new SimpleIdentitySet(Array.empty[AnyRef])
157157

158158
def apply[Elem <: AnyRef](elems: Elem*): SimpleIdentitySet[Elem] =
159-
elems.foldLeft(empty: SimpleIdentitySet[Elem])(_ + _)
159+
new SimpleIdentitySet[Elem](elems.toArray)
160+
161+
def apply[Elem <: AnyRef](elems: Iterator[Elem]): SimpleIdentitySet[Elem] =
162+
new SimpleIdentitySet[Elem](elems.toArray)
160163

161164
extension [E <: AnyRef](xs: SimpleIdentitySet[E])
162165
def intersect(ys: SimpleIdentitySet[E]): SimpleIdentitySet[E] =

0 commit comments

Comments
 (0)