Skip to content

Commit 8175d6a

Browse files
committed
Optimization: Specialize ==, != on value classes
Without the optimization a comparison of value classes always boxing, since the rhs operand is cast to Any. Most likely these are eliminated by escape analysis, but by specializing here we make the job of the JOT compiler easier.
1 parent 7034d91 commit 8175d6a

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ case class Mode(val bits: Int) extends AnyVal {
1212

1313
override def toString: String =
1414
(0 until 31).filter(i => (bits & (1 << i)) != 0).map(modeName).mkString("Mode(", ",", ")")
15+
16+
def ==(that: Mode): Boolean = this.bits == that.bits
17+
def !=(that: Mode): Boolean = this.bits != that.bits
1518
}
1619

1720
object Mode {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ object Periods {
123123
this.lastPhaseId max that.lastPhaseId)
124124

125125
override def toString: String = s"Period($firstPhaseId..$lastPhaseId, run = $runId)"
126+
127+
def ==(that: Period): Boolean = this.code == that.code
128+
def !=(that: Period): Boolean = this.code != that.code
126129
}
127130

128131
object Period {

compiler/src/dotty/tools/dotc/core/tasty/TastyBuffer.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ object TastyBuffer {
2020
def + (delta: Int): Addr = Addr(this.index + delta)
2121

2222
def relativeTo(base: Addr): Addr = this - base.index - AddrWidth
23+
24+
def ==(that: Addr): Boolean = this.index == that.index
25+
def !=(that: Addr): Boolean = this.index != that.index
2326
}
2427

2528
val NoAddr: Addr = Addr(-1)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ object Spans {
127127
else
128128
s"${left}no position${right}"
129129
}
130+
131+
def ==(that: Span): Boolean = this.coords == that.coords
132+
def !=(that: Span): Boolean = this.coords != that.coords
130133
}
131134

132135
private def fromOffsets(start: Int, end: Int, pointDelta: Int) = {

0 commit comments

Comments
 (0)