Skip to content

Commit 3a082cb

Browse files
committed
Avoid allocating the empty constraint multiple times
1 parent 581f7e9 commit 3a082cb

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ object OrderingConstraint {
2424
type ParamOrdering = ArrayValuedMap[List[TypeParamRef]]
2525

2626
/** A new constraint with given maps */
27-
private def newConstraint(boundsMap: ParamBounds, lowerMap: ParamOrdering, upperMap: ParamOrdering)(using Context) : OrderingConstraint = {
28-
val result = new OrderingConstraint(boundsMap, lowerMap, upperMap)
29-
ctx.run.recordConstraintSize(result, result.boundsMap.size)
30-
result
31-
}
27+
private def newConstraint(boundsMap: ParamBounds, lowerMap: ParamOrdering, upperMap: ParamOrdering)(using Context) : OrderingConstraint =
28+
if boundsMap.isEmpty && lowerMap.isEmpty && upperMap.isEmpty then
29+
empty
30+
else
31+
val result = new OrderingConstraint(boundsMap, lowerMap, upperMap)
32+
ctx.run.recordConstraintSize(result, result.boundsMap.size)
33+
result
3234

3335
/** A lens for updating a single entry array in one of the three constraint maps */
3436
abstract class ConstraintLens[T <: AnyRef: ClassTag] {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import collection.mutable.ListBuffer
66
* It has linear complexity for `apply`, `updated`, and `remove`.
77
*/
88
abstract class SimpleIdentityMap[K <: AnyRef, +V >: Null <: AnyRef] extends (K => V) {
9+
final def isEmpty: Boolean = this eq SimpleIdentityMap.myEmpty
910
def size: Int
1011
def apply(k: K): V
1112
def remove(k: K): SimpleIdentityMap[K, V]

0 commit comments

Comments
 (0)