[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Profiling LPEG
- From: Roberto Ierusalimschy <roberto@...>
- Date: Wed, 26 Apr 2023 10:52:08 -0300
> Okay, one thing I did was recompile LPEG to activate the ptree() function,
> and dump both the 're' compiled version any my LPEG version. The output was
> very surprising.
>
> 're' version [1]: 3,212 lines long
> 'lpeg' version: 611,931 lines long
>
> It appers that 're' does a better job of generating LPEG than I did by
> hand. I'm surprised that the 'lpeg' version was only twice as slow, given
> the results.
Without seeing your code, one thing popped in my head: In re, the only
way to name a pattern is as a rule in a grammar. In LPeg, we can
name patterns as grammar rules or as Lua variables. When you
use a pattern inside another pattern, Lua expands its code, as if it
was a macro. So, if you have patterns that appear multiple times inside
others, and then those patterns are used inside other ones, etc., things
can get very large.
Quick comparison:
-----------------------------------------------------------
local P = lpeg.P"hello world" -- pattern in a Lua variable
(P * P * P):ptree()
-- tree has 66 lines
local P = lpeg.V"P" -- pattern in a grammar rule
lpeg.P{P * P * P, P = lpeg.P"hello world"}:ptree()
-- tree has 32 lines
-----------------------------------------------------------
-- Roberto