Skip to content

Commit c583aa3

Browse files
committed
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. This commit upstreams scala-js/scala-js@a5337ed from Scala.js.
1 parent 4dbc9fb commit c583aa3

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

library/src/scala/collection/immutable/Range.scala

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

9393
final override val isEmpty: Boolean = (
94-
(start > end && step > 0)
95-
|| (start < end && step < 0)
96-
|| (start == end && !isInclusive)
97-
)
94+
if (isInclusive)
95+
(if (step >= 0) start > end else start < end)
96+
else
97+
(if (step >= 0) start >= end else start <= end)
98+
)
9899

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

0 commit comments

Comments
 (0)