--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
how did you test it?
--
import ImplicitWrappersAnyValPerfSpec._
import org.specs2.mutable.Specification
import scala.xml.Node
class ImplicitWrappersAnyValPerfSpec
extends Specification {
"implicit extension method wrappers" should {
"run faster with 'extends AnyVal'" in {
val th = new ichi.bench.Thyme
val inputs = (1 to 100000).map( i => <a><b><c>{i}</c></b></a> ).toArray
val (a, bf) = th.benchOffPair()({
var acc = 0
inputs.foreach { xml =>
acc += xml.childWithPath("b", "c").text.toInt
}
acc
}, ftitle = "Without extends AnyVal")({
var acc = 0
inputs.foreach { xml =>
acc += xml.childWithPathWithAnyVal("b", "c").text.toInt
}
acc
}, htitle = "With extends AnyVal")
val result = bf.toString()
println(result)
result must contain("Significantly different")
val rc = bf.foldChangeRuntime
rc.rdiff.mean must beLessThan(1.0)
}
}
}
object ImplicitWrappersAnyValPerfSpec {
implicit class AddChildHelpers(private val xml: Node) {
def childWithPath(elementNames: String*): Node = child(xml, elementNames: _*)
}
implicit class AddChildHelpersWithAnyVal(private val xml: Node) extends AnyVal {
def childWithPathWithAnyVal(elementNames: String*): Node = child(xml, elementNames: _*)
}
/**
* Walks down a chain of expected XML element names from the given Node.
*
* If at any stage there is not a unique child element with the next expected
* element name, then an IllegalArgumentException is thrown
*/
def child(
xml: Node,
elementNames: String*
): Node = {
elementNames.foldLeft(xml) { (parent, childName) =>
val matches = parent \ childName
require (matches.length == 1,
s"Invalid XML. Expected one '$childName' element; found: $parent")
matches.head
}
}
}
Hi All,
I was curiously whether or not escape analysis eliminates the need to use AnyVal
for implicit classes.
Cheers,
-John