8000 Opt: Fewer (and more predictable) branches in Range.isEmpty. · scala-wasm/scala-wasm@a5337ed · GitHub
[go: up one dir, main page]

Skip to content

Commit a5337ed

Browse files
committed
Opt: Fewer (and more predictable) branches in Range.isEmpty.
Extract branches to be mostly dependent on `isInclusive` and `step >= 0`. These are often constant, and when they're not statically constant, they are probably predictable anyway.
1 parent 52668c3 commit a5337ed

File tree

2 files changed

+12
-10
lines changed
  • scalalib

2 files changed

+12
-10
lines changed

scalalib/overrides-2.12/scala/collection/immutable/Range.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import scala.collection.parallel.immutable.ParRange
3333
* `init`) are also permitted on overfull ranges.
3434
*
3535
* @param start the start of this range.
36-
* @param end the end of the range. For exclusive ranges, e.g.
36+
* @param end the end of the range. For exclusive ranges, e.g.
3737
* `Range(0,3)` or `(0 until 3)`, this is one
3838
* step past the last one in the range. For inclusive
3939
* ranges, e.g. `Range.inclusive(0,3)` or `(0 to 3)`,
@@ -78,9 +78,10 @@ extends scala.collection.AbstractSeq[Int]
7878
// which means it will not fail fast for those cases where failing was
7979
// correct.
8080
override final val isEmpty = (
81-
(start > end && step > 0)
82-
|| (start < end && step < 0)
83-
|| (start == end && !isInclusive)
81+
if (isInclusive)
82+
(if (step >= 0) start > end else start < end)
83+
else
84+
(if (step >= 0) start >= end else start <= end)
8485
)
8586

8687
private val numRangeElements: Int = {
@@ -194,7 +195,7 @@ extends scala.collection.AbstractSeq[Int]
194195
copy(locationAfterN(n), end, step)
195196
}
196197
)
197-
198+
198199
/** Creates a new range containing the elements starting at `from` up to but not including `until`.
199200
*
200201
* $doesNotUseBuilders
@@ -211,7 +212,7 @@ extends scala.collection.AbstractSeq[Int]
211212
if (from >= until) newEmptyRange(fromValue)
212213
else new Range.Inclusive(fromValue, locationAfterN(until-1), step)
213214
}
214-
215+
215216
/** Creates a new range containing all the elements of this range except the last one.
216217
*
217218
* $doesNotUseBuilders

scalalib/overrides-2.13/scala/collection/immutable/Range.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ sealed abstract class Range(
8989
def isInclusive: Boolean
9090

9191
final override val isEmpty: Boolean = (
92-
(start > end && step > 0)
93-
|| (start < end && step < 0)
94-
|| (start == end && !isInclusive)
95-
)
92+
if (isInclusive)
93+
(if (step >= 0) start > end else start < end)
94+
else
95+
(if (step >= 0) start >= end else start <= end)
96+
)
9697

9798
private[this] val numRangeElements: Int = {
9899
if (step == 0) throw new IllegalArgumentException("step cannot be 0.")

0 commit comments

Comments
 (0)
0