Access to location information in scala combinatorparser kills performance

I wrote a new combiner for my parser in scala.

This is a ^^ combinator variation that conveys location information. But access to information about the position of the input element is really costly.

In my case, parsing a large example takes about 3 seconds without location information, since it takes more than 30 seconds.

I wrote a runnable example where the execution time is about 50% longer when accessing a position.

Why? How can I get the best lead time?

Example:

import scala.util.parsing.combinator.RegexParsers
import scala.util.parsing.combinator.Parsers
import scala.util.matching.Regex
import scala.language.implicitConversions
object FooParser extends RegexParsers with Parsers {
  var withPosInfo = false
  def b: Parser[String] = regexB("""[a-z]+""".r)  ^^@ { case (b, x) => b + " ::" + x.toString }
  def regexB(p: Regex): BParser[String] = new BParser(regex(p))
  class BParser[T](p: Parser[T]) {
    def ^^@[U](f: ((Int, Int), T) => U): Parser[U] = Parser { in =>
      val source = in.source
      val offset = in.offset
      val start = handleWhiteSpace(source, offset)
      val inwo = in.drop(start - offset)
      p(inwo) match {
        case Success(t, in1) =>
          {
            var a = 3
            var b = 4
            if(withPosInfo)
            { // takes a lot of time
              a = inwo.pos.line
              b = inwo.pos.column
            }            
            Success(f((a, b), t), in1)
          }
        case ns: NoSuccess => ns
      }
    }
  }
  def main(args: Array[String]) = {
    val r = "foo"*50000000
    var now = System.nanoTime

    parseAll(b, r) 
    var us = (System.nanoTime - now) / 1000
    println("without: %d us".format(us))
    withPosInfo = true
    now = System.nanoTime
    parseAll(b, r)
    us = (System.nanoTime - now) / 1000
    println("with   : %d us".format(us))
  }
}

Conclusion:

without: 2952496 us

s: 4591070 us

+5
source share
1 answer

, , . , scala.util.parsing.input.OffsetPosition, , . , , pos ( ). . CharSequenceReader OffsetPosition .

, , :

val ip = inwo.pos
a = ip.line
b = ip.column

pos . . , OffsetPosition , , .

/ . .

+4

All Articles