diff options
author | Simon Ochsenreither <simon@ochsenreither.de> | 2013-05-23 21:04:00 +0200 |
---|---|---|
committer | Simon Ochsenreither <simon@ochsenreither.de> | 2013-06-23 18:18:51 +0200 |
commit | 4cbb6a93b9d46174cd86d6c9b6ecaeba3ba67e9d (patch) | |
tree | 80032e4f62d92439ce8a8b78470658c984198647 /src/library/scala/runtime/RichLong.scala | |
parent | b6bc53479984f9fc0528a8ce3f6f0b582cb4147f (diff) | |
download | scala-4cbb6a93b9d46174cd86d6c9b6ecaeba3ba67e9d.tar.gz scala-4cbb6a93b9d46174cd86d6c9b6ecaeba3ba67e9d.tar.bz2 scala-4cbb6a93b9d46174cd86d6c9b6ecaeba3ba67e9d.zip |
SI-7511 Remove indirection of numeric methods
Methods on `Rich<AnyVal>`s like `abs`, `doubleValue`, `floatValue`,
`longValue`, `intValue`, `byteValue`, `shortValue`, `min`, `max`,
`signum` are slow because they go through multiple indirections
including typeclasses and boxing.
For instance, take `1L.abs`:
Instead of just computing and returning the result (like it is done in
`RichInt`), this is what happens:
- `RichLong` inherits `abs` by extending `ScalaNumberProxy[T]`
- `ScalaNumberProxy[T]` has an abstract
`protected implicit def num: Numeric[T]`
- This method is implemented in `RichLong` and points to
`scala.math.Numeric.LongIsIntegral`
- The actual method `abs` now calls `num.abs(self)`
Diffstat (limited to 'src/library/scala/runtime/RichLong.scala')
-rw-r--r-- | src/library/scala/runtime/RichLong.scala | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/src/library/scala/runtime/RichLong.scala b/src/library/scala/runtime/RichLong.scala index e5b39b1c09..df0bbec047 100644 --- a/src/library/scala/runtime/RichLong.scala +++ b/src/library/scala/runtime/RichLong.scala @@ -9,20 +9,31 @@ package scala package runtime - final class RichLong(val self: Long) extends AnyVal with IntegralProxy[Long] { protected def num = scala.math.Numeric.LongIsIntegral protected def ord = scala.math.Ordering.Long - def toBinaryString: String = java.lang.Long.toBinaryString(self) - def toHexString: String = java.lang.Long.toHexString(self) - def toOctalString: String = java.lang.Long.toOctalString(self) + override def doubleValue() = self.toDouble + override def floatValue() = self.toFloat + override def longValue() = self + override def intValue() = self.toInt + override def byteValue() = self.toByte + override def shortValue() = self.toShort - override def isValidByte = self.toByte.toLong == self + override def isValidByte = self.toByte.toLong == self override def isValidShort = self.toShort.toLong == self - override def isValidChar = self.toChar.toLong == self - override def isValidInt = self.toInt.toLong == self - // override def isValidLong = true + override def isValidChar = self.toChar.toLong == self + override def isValidInt = self.toInt.toLong == self + def isValidLong = true // override def isValidFloat = self.toFloat.toLong == self && self != Long.MaxValue // override def isValidDouble = self.toDouble.toLong == self && self != Long.MaxValue + + override def abs: Long = math.abs(self) + override def max(that: Long): Long = math.max(self, that) + override def min(that: Long): Long = math.min(self, that) + override def signum: Int = math.signum(self).toInt + + def toBinaryString: String = java.lang.Long.toBinaryString(self) + def toHexString: String = java.lang.Long.toHexString(self) + def toOctalString: String = java.lang.Long.toOctalString(self) } |