Fork me on GitHub
#instaparse
<
2017-12-16
>
borkdude17:12:57

Hello. Why is my InstaParser so slow? 850ms vs 8ms hand-written Clojure: https://github.com/borkdude/aoc2017/blob/master/src/day16.clj#L119

borkdude17:12:09

Maybe it’s my grammar, but I don’t see it.

aengelberg17:12:12

How big is the input?

aengelberg17:12:34

I mean, you're basically generating one string per character in a 40kb file. I'm not surprised it's slow.

aengelberg17:12:51

And wrapping with vectors, etc

borkdude17:12:31

That’s fair, but even without wrapping the arguments I get a similar time

borkdude17:12:48

I had that before, but I wanted to transform the arguments to ints, that’s why I wrapped them later

borkdude17:12:21

I mean, it’s not really a problem, but just curious why or if I made a mistake in my grammar

aengelberg17:12:55

I don't think you made a mistake like an ambiguity issue or anything

aengelberg17:12:23

It's just really exercising the parser's dataflow overhead

aengelberg17:12:15

I saw similar issues when trying to perf-tune @dave's Alda parser a while ago

aengelberg17:12:09

Because his rules were like "if you see this single character, parse this other single character"

aengelberg17:12:41

Instaparse does a lot of bookkeeping during a parse to make sure it magically works with weirdly recursive grammars, so for super low level parsers like this it doesn't exactly shine

borkdude17:12:57

(def parse2
  (insta/parser
   "<INPUT>       = (INSTRUCTION <','>)+ INSTRUCTION 
    <INSTRUCTION> = SPIN | EXCHANGE | PARTNER
    SPIN          = <'s'> POSITION
    EXCHANGE      = <'x'> POSITION <'/'> POSITION
    PARTNER       = <'p'> PROGRAM  <'/'> PROGRAM
    <POSITION>    = #'\\d\\d?'
    <PROGRAM>     = #'[a-p]'"))
No nesting of position and program, 800ms

aengelberg17:12:53

Just curious, does anything improve if you change the first rule to INSTRUCTION (<','> INSTRUCTION)*?

borkdude18:12:45

I wondered about that rule as well. Quickbenching…

borkdude18:12:13

Yup, 582ms!

borkdude18:12:50

Does the order of rules matter for performance?

borkdude18:12:21

I mean, when it’s more likely to encounter EXCHANGE, does it help putting that first?