parsica-php / parsica

Parsica - PHP Parser Combinators - The easiest way to build robust parsers.
https://parsica-php.github.io/
MIT License
405 stars 18 forks source link

Performance: ~250% faster JSON Parser #57

Open mhsdesign opened 2 years ago

mhsdesign commented 2 years ago

related: #17 inspired by this regex:

/^
  "[^"\\]*              # double quoted strings with possibly escaped double quotes
    (?:
      \\.               # escaped character (quote)
      [^"\\]*           # unrolled loop following Jeffrey E.F. Friedl
    )*
  "
/x

i implemented an unrolled loop in combination with takeWhile. (the last use of the deprecated zeroOrMore was also removed) this leads to a big performance boost from ca ~250%(without xdebug) - ~330%(with xdebug) - both without opcache.

before: bench_json_encode: 4.133μs bench_Parsica_JSON: 4.109ms bench_basemax_jpophp: 432.128μs

after: bench_json_encode: 3.744μs bench_Parsica_JSON: 1.662ms bench_basemax_jpophp: 401.154μs