Re: Teaching Rakudo the tricks of Perl 5's regex optimiser
[email protected] (Brad Gilbert) Tue, 13 Aug 2019 09:32:05 -0500
| Newsgroups | perl.perl6.compiler |
|---|---|
| Message-ID | <CAD2L-T0YqabkpW-4dnAzT07vGnA+-K5E7h7PUcy_6fUpvSJKOw@mail.gmail.com> |
--000000000000dc826305900081cf
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
I would like to point out that regexes in Perl6 are treated as code.
(They can even have parameters and lexical variables.)
It uses the same compiler intrinsics as the rest of Perl6.
It uses the same VM opcodes as regular Perl6 code.
Regexes, string literals, and signatures are just domain specific
sub-languages.
Perl6 regexes are also missing features that are common to other regexes,
instead you just embed regular Perl6 code to handle it.
For example instead of `(?=3D=E2=80=A6)` Perl6 uses a method call to `befor=
e` with a
regex argument.
<.before =E2=80=A6>
use v5;
'abcd' =3D~ / . (?=3Dc) . /x # "bc"
use v6;
'abcd' ~~ / . <.before( /c/ )> . / # "bc"
'abcd' ~~ / . <.before c > . / # "bc" # (exactly identical)
A person could change the code in the `before` method to have it do
something different
This means that the biggest wins in the Perl6 regex system are probably
going to look nothing like the ones in other regex engines.
At least in the short term.
Inlining method calls for instance.
Perhaps the biggest one may be the one about passing around =E2=80=9Cfates=
=E2=80=9D. (I
barely understand the basics of this.)
So while information on how other projects do regex optimizations can help,
they are generally going to be small wins in comparison.
(That doesn't mean they aren't worth doing.)
Doing some of those optimizations may also make other bigger optimizations
harder to do.
(I'm fairly sure the =E2=80=9Cfates=E2=80=9D optimization would be harder, =
but I'm not
sure.)
On Tue, Aug 13, 2019 at 4:53 AM Nicholas Clark <[email protected]> wrote:
> I'm cheating here - I'm using an e-mail message as a way to publish some
> notes
> for Jonathan (or *anyone else interested*):
>
> Jonathan did a talk in Riga, Perl 6 performance update,
> https://perlcon.eu/talk/80
> The (re-uploaded) live stream is at
> https://www.youtube.com/watch?v=3DS5iVBlk7pdg#t=3D4h39m
>
> One thing that this talk revealed is that (currently) Perl 5 beats
> Rakudo on various regular expressions. What *I* know is that this is
> because
> Perl 5 cheats - it has an "optimiser", which happens to automatically do
> what jnthn then showed manually implemented in some of his benchmarks.
>
> You can see what the Perl 5 regex optimiser is doing by using the re
> pragma.
> I'm no expert on this thing, but playing with it, I can see that it can d=
o
> various things
>
> 0) Know that it can't do anything useful (it is skipped)
> 1) Know that a fixed string must be present
> Look for it (which is fast), not find it, and immediately return "no
> match"
> 2) Know that a fixed string must be present
> Look for it find it, carry on into the real engine
> (which arguably is slower than no optimiser)
> 3) Know that a fixed string must be present, *and* is the entire match
> Look for it (which is fast), find it, and immediately return "match".
>
> Clearly cases (1) and (3) are the big wins here. Case (3) was the one I h=
ad
> forgotten about - the cheating is so good that often the engine is never
> entered.
>
>
> So, if you run this:
>
> $ perl -wle 'use re "debug"; print "Perl rules" =3D~ /^Perl/ ? "Y" : "n"'
> Compiling REx "^Perl"
> Final program:
> 1: SBOL /^/ (2)
> 2: EXACT <Perl> (4)
> 4: END (0)
> anchored "Perl" at 0 (checking anchored noscan) anchored(SBOL) minlen 4
> Matching REx "^Perl" against "Perl rules"
> Intuit: trying to determine minimum start position...
> Looking for check substr at fixed offset 0...
> Intuit: Successfully guessed: match at offset 0
> 0 <> <Perl rules> | 0| 1:SBOL /^/(2)
> 0 <> <Perl rules> | 0| 2:EXACT <Perl>(4)
> 4 <Perl> < rules> | 0| 4:END(0)
> Match successful!
> Y
> Freeing REx: "^Perl"
>
>
> "Final program" describes how the regex ended up being compiled. This isn=
't
> really of interest here.
>
> What is of interest is the parts between the two lines "Intuit". That's t=
he
> optimiser. For the case I specifically chose here, what we have is that
>
> 1) the optimiser knows that the fixed string "Perl" must be in target
> string
> 2) which it searches for and finds
> 3) at which point it calls the main engine (these lines):
>
> 0 <> <Perl rules> | 0| 1:SBOL /^/(2)
> 0 <> <Perl rules> | 0| 2:EXACT <Perl>(4)
> 4 <Perl> < rules> | 0| 4:END(0)
>
> (that part isn't actually of interest here. I'm just noting it as "this i=
s
> the main engine, and comparable to what Rakudo does).
>
>
> The interesting cases are things like:
>
> $ perl -wle 'use re "debug"; print "Perl rules" =3D~ /er./ ? "Y" : "n"'
> Compiling REx "er."
> Final program:
> 1: EXACT <er> (3)
> 3: REG_ANY (4)
> 4: END (0)
> anchored "er" at 0 (checking anchored) minlen 3
> Matching REx "er." against "Perl rules"
> Intuit: trying to determine minimum start position...
> doing 'check' fbm scan, [0..9] gave 1
> Found anchored substr "er" at offset 1 (rx_origin now 1)...
> (multiline anchor test skipped)
> try at offset...
> Intuit: Successfully guessed: match at offset 1
> 1 <P> <erl rules> | 0| 1:EXACT <er>(3)
> 3 <Per> <l rules> | 0| 3:REG_ANY(4)
> 4 <Perl> < rules> | 0| 4:END(0)
> Match successful!
> Y
> Freeing REx: "er."
>
>
> You can see that
> 1) the compiler records that the longest fixed string is 'er'
> 2) the optimiser looks for this before even hitting the real engine
> 3) the optimiser tells the real engine that it doesn't even need to
> consider
> trying to match at string offset 0
>
>
> and this:
>
> $ perl -wle 'use re "debug"; print "Perl rules" =3D~ /er/ ? "Y" : "n"'
> Compiling REx "er"
> Final program:
> 1: EXACT <er> (3)
> 3: END (0)
> anchored "er" at 0 (checking anchored isall) minlen 2
> Matching REx "er" against "Perl rules"
> Intuit: trying to determine minimum start position...
> doing 'check' fbm scan, [0..10] gave 1
> Found anchored substr "er" at offset 1 (rx_origin now 1)...
> (multiline anchor test skipped)
> try at offset...
> Intuit: Successfully guessed: match at offset 1
> Y
> Freeing REx: "er"
>
>
> The optimiser matches, and knows that if its crib[1] matches, the entire
> regex must match, so it doesn't even call into the engine.
>
>
> and this:
>
> $ perl -wle 'use re "debug"; print "Perl rules" =3D~ /Good *, #MoarVM/ ? =
"Y"
> : "n"'
> Compiling REx "Good *, #MoarVM"
> Final program:
> 1: EXACT <Good> (3)
> 3: STAR (6)
> 4: EXACT < > (0)
> 6: EXACT <, #MoarVM> (10)
> 10: END (0)
> anchored "Good" at 0 floating ", #MoarVM" at 4..9223372036854775807
> (checking floating) minlen 13
> n
> Freeing REx: "Good *, #MoarVM"
>
> The optimiser knows that the pattern can't match less than 13 characters,
> but
> the offered string is too short.
>
> The optimiser also knows at what offsets the within the target string the
> fixed string must lie, if there is also some variable length stuff, but
> this
> is getting beyond what I know the plan for:
>
> $ perl -wle 'use re "debug"; print "Perl rules" =3D~ /Perl.*s/ ? "Y" : "n=
"'
> Compiling REx "Perl.*s"
> Final program:
> 1: EXACT <Perl> (3)
> 3: STAR (5)
> 4: REG_ANY (0)
> 5: EXACT <s> (7)
> 7: END (0)
> anchored "Perl" at 0 floating "s" at 4..9223372036854775807 (checking
> anchored) minlen 5
> Matching REx "Perl.*s" against "Perl rules"
> Intuit: trying to determine minimum start position...
> doing 'check' fbm scan, [0..9] gave 0
> Found anchored substr "Perl" at offset 0 (rx_origin now 0)...
> doing 'other' fbm scan, [4..10] gave 9
> Found floating substr "s" at offset 9 (rx_origin now 0)...
> (multiline anchor test skipped)
> Intuit: Successfully guessed: match at offset 0
> 0 <> <Perl rules> | 0| 1:EXACT <Perl>(3)
> 4 <Perl> < rules> | 0| 3:STAR(5)
> | 0| REG_ANY can match 6 times out of
> 2147483647...
> 9 <Perl rule> <s> | 1| 5:EXACT <s>(7)
> 10 <Perl rules> <> | 1| 7:END(0)
> Match successful!
> Y
> Freeing REx: "Perl.*s"
>
>
> If you're wearing Peril Sensitive Sunglasses, then the code that does thi=
s
> is
> the self-recursive Perl_re_intuit_start() in
> https://perl5.git.perl.org/perl.git/blob/HEAD:/regexec.c
>
> As a guide to reading that, for things like
>
> DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
> " String too short...\n"));
>
>
> DEBUG_EXECUTE_r() only generates that debugging output if the command lin=
e
> flag -Dr is set, *and* the `use re "debug";` de facto sets -Dr, even on a
> perl binary compiled without all the other -D flags enabled.
>
>
> I know that PCRE also exposes some information it derives from the patter=
n
> which can be used to pre-match -- see PCRE_INFO_FIRSTCHARACTER and
> PCRE_INFO_REQUIREDCHAR in
> https://www.pcre.org/original/doc/html/pcre_fullinfo.html
>
> I *thought* that Google's re2 library (which came out of code search) cou=
ld
> return the longest fixed string (if any) as part of its API, but I can't
> find
> that now. In https://swtch.com/~rsc/regexp/regexp4.html it mentions
> building
> trigrams (directly) from parsing the regex (with ANDs and ORs) which is
> what
> some $ork code is doing (but then using PCRE, not re2).
>
> Anyway, I hope that this helps someone, and helps make Rakudo's parser
> faster
>
> I assume that the useful strategy is roughly
>
> 0) Do the most simple thing - no alternations, anchored starts. Reject
> match
> if not present
> 1) Then 1+ character fixed substrings - Reject match if not present
> 2) Then the big cheat of "string is actually fixed" - compile to a sub th=
at
> build a match result immediately, instead of the more general engine
> 3) Then the even more general "fixed string as part of larger regex", whe=
re
> failure is a fast-path, but otherwise the regular regex engine follows
> 4) Minimum match length
> 5) The stuff where one starts to tell the main engine how
>
>
> but of course no plan survives contact with the enemy.
>
> Nicholas Clark
>
> 1: https://en.wiktionary.org/wiki/crib -- definitions 14 and 23:
> (usually in the plural) A collection of quotes or references for
> use in speaking, for assembling a written document, or as an aid to
> a project of some sort; a crib sheet.
> [and]
> A cheat sheet or past test used by students; crib sheet.
>
--000000000000dc826305900081cf
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I would like to point out that regexes in Perl6 are treate=
d as code.<div>(They can even have parameters and lexical variables.)</div>=
<div><div><br></div><div>It uses the same compiler intrinsics as the rest o=
f Perl6.</div><div>It uses the same VM opcodes as regular Perl6 code.</div>=
</div><div><br></div><div>Regexes, string literals, and signatures are just=
domain specific sub-languages.</div><div><br></div><div>Perl6 regexes are =
also missing features that are common to other regexes, instead you just em=
bed regular Perl6 code to handle it.</div><div><br></div><div>For example i=
nstead of `(?=3D=E2=80=A6)` Perl6 uses a method call to `before` with a reg=
ex argument.</div><div><br></div><div>=C2=A0 =C2=A0 <.before =E2=80=A6&g=
t;</div><div><br></div><div>=C2=A0 =C2=A0 use v5;</div><div>=C2=A0 =C2=A0 &=
#39;abcd' =3D~ / . (?=3Dc) . /x # "bc"</div><div><br></div><d=
iv>=C2=A0 =C2=A0 use v6;</div><div>=C2=A0 =C2=A0 'abcd' ~~ / . <=
.before( /c/ )> .=C2=A0 / # "bc"</div>=C2=A0 =C2=A0 'abcd&=
#39; ~~ / . <.before=C2=A0 =C2=A0c=C2=A0 =C2=A0> .=C2=A0 / # "bc=
" # (exactly identical)<div><br></div><div>A person could change the c=
ode in the `before` method to have it do something different</div><div></di=
v><div><br></div><div>This means that the biggest wins in the Perl6 regex s=
ystem are probably going to look nothing like the ones in other regex engin=
es.</div><div>At least in the short term.</div><div>Inlining method calls f=
or instance.</div><div>Perhaps the biggest one may be the one about passing=
around =E2=80=9Cfates=E2=80=9D. (I barely understand the basics of this.)<=
/div><div><br></div><div>So while information on how other projects do rege=
x optimizations can help, they are generally going to be small wins in comp=
arison.<br></div><div>(That doesn't mean they aren't worth doing.)<=
/div><div><br></div><div>Doing some of those optimizations may also make ot=
her bigger optimizations harder to do.</div><div>(I'm fairly sure the =
=E2=80=9Cfates=E2=80=9D optimization would be harder, but I'm not sure.=
)</div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail=
_attr">On Tue, Aug 13, 2019 at 4:53 AM Nicholas Clark <<a href=3D"mailto=
:[email protected]">[email protected]</a>> wrote:<br></div><blockquote class=3D"=
gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(20=
4,204,204);padding-left:1ex">I'm cheating here - I'm using an e-mai=
l message as a way to publish some notes<br>
for Jonathan (or *anyone else interested*):<br>
<br>
Jonathan did a talk in Riga, Perl 6 performance update,<br>
<a href=3D"https://perlcon.eu/talk/80" rel=3D"noreferrer" target=3D"_blank"=
>https://perlcon.eu/talk/80</a><br>
The (re-uploaded) live stream is at<br>
<a href=3D"https://www.youtube.com/watch?v=3DS5iVBlk7pdg#t=3D4h39m" rel=3D"=
noreferrer" target=3D"_blank">https://www.youtube.com/watch?v=3DS5iVBlk7pdg=
#t=3D4h39m</a><br>
<br>
One thing that this talk revealed is that (currently) Perl 5 beats<br>
Rakudo on various regular expressions. What *I* know is that this is becaus=
e<br>
Perl 5 cheats - it has an "optimiser", which happens to automatic=
ally do<br>
what jnthn then showed manually implemented in some of his benchmarks.<br>
<br>
You can see what the Perl 5 regex optimiser is doing by using the re pragma=
.<br>
I'm no expert on this thing, but playing with it, I can see that it can=
do<br>
various things<br>
<br>
0) Know that it can't do anything useful (it is skipped)<br>
1) Know that a fixed string must be present<br>
=C2=A0 =C2=A0Look for it (which is fast), not find it, and immediately retu=
rn "no match"<br>
2) Know that a fixed string must be present<br>
=C2=A0 =C2=A0Look for it find it, carry on into the real engine<br>
=C2=A0 =C2=A0(which arguably is slower than no optimiser)<br>
3) Know that a fixed string must be present, *and* is the entire match<br>
=C2=A0 =C2=A0Look for it (which is fast), find it, and immediately return &=
quot;match".<br>
<br>
Clearly cases (1) and (3) are the big wins here. Case (3) was the one I had=
<br>
forgotten about - the cheating is so good that often the engine is never<br=
>
entered.<br>
<br>
<br>
So, if you run this:<br>
<br>
$ perl -wle 'use re "debug"; print "Perl rules" =3D=
~ /^Perl/ ? "Y" : "n"'<br>
Compiling REx "^Perl"<br>
Final program:<br>
=C2=A0 =C2=A01: SBOL /^/ (2)<br>
=C2=A0 =C2=A02: EXACT <Perl> (4)<br>
=C2=A0 =C2=A04: END (0)<br>
anchored "Perl" at 0 (checking anchored noscan) anchored(SBOL) mi=
nlen 4<br>
Matching REx "^Perl" against "Perl rules"<br>
Intuit: trying to determine minimum start position...<br>
=C2=A0 Looking for check substr at fixed offset 0...<br>
Intuit: Successfully guessed: match at offset 0<br>
=C2=A0 =C2=A00 <> <Perl rules>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0|=C2=A0 =C2=A00| 1:SBOL /^/(2)<br>
=C2=A0 =C2=A00 <> <Perl rules>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0|=C2=A0 =C2=A00| 2:EXACT <Perl>(4)<br>
=C2=A0 =C2=A04 <Perl> < rules>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0|=C2=A0 =C2=A00| 4:END(0)<br>
Match successful!<br>
Y<br>
Freeing REx: "^Perl"<br>
<br>
<br>
"Final program" describes how the regex ended up being compiled. =
This isn't<br>
really of interest here.<br>
<br>
What is of interest is the parts between the two lines "Intuit". =
That's the<br>
optimiser. For the case I specifically chose here, what we have is that<br>
<br>
1) the optimiser knows that the fixed string "Perl" must be in ta=
rget string<br>
2) which it searches for and finds<br>
3) at which point it calls the main engine (these lines):<br>
<br>
=C2=A0 =C2=A00 <> <Perl rules>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0|=C2=A0 =C2=A00| 1:SBOL /^/(2)<br>
=C2=A0 =C2=A00 <> <Perl rules>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0|=C2=A0 =C2=A00| 2:EXACT <Perl>(4)<br>
=C2=A0 =C2=A04 <Perl> < rules>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0|=C2=A0 =C2=A00| 4:END(0)<br>
<br>
(that part isn't actually of interest here. I'm just noting it as &=
quot;this is<br>
the main engine, and comparable to what Rakudo does).<br>
<br>
<br>
The interesting cases are things like:<br>
<br>
$ perl -wle 'use re "debug"; print "Perl rules" =3D=
~ /er./ ? "Y" : "n"'<br>
Compiling REx "er."<br>
Final program:<br>
=C2=A0 =C2=A01: EXACT <er> (3)<br>
=C2=A0 =C2=A03: REG_ANY (4)<br>
=C2=A0 =C2=A04: END (0)<br>
anchored "er" at 0 (checking anchored) minlen 3<br>
Matching REx "er." against "Perl rules"<br>
Intuit: trying to determine minimum start position...<br>
=C2=A0 doing 'check' fbm scan, [0..9] gave 1<br>
=C2=A0 Found anchored substr "er" at offset 1 (rx_origin now 1)..=
.<br>
=C2=A0 (multiline anchor test skipped)<br>
=C2=A0 try at offset...<br>
Intuit: Successfully guessed: match at offset 1<br>
=C2=A0 =C2=A01 <P> <erl rules>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0|=C2=A0 =C2=A00| 1:EXACT <er>(3)<br>
=C2=A0 =C2=A03 <Per> <l rules>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0|=C2=A0 =C2=A00| 3:REG_ANY(4)<br>
=C2=A0 =C2=A04 <Perl> < rules>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0|=C2=A0 =C2=A00| 4:END(0)<br>
Match successful!<br>
Y<br>
Freeing REx: "er."<br>
<br>
<br>
You can see that<br>
1) the compiler records that the longest fixed string is 'er'<br>
2) the optimiser looks for this before even hitting the real engine<br>
3) the optimiser tells the real engine that it doesn't even need to con=
sider<br>
=C2=A0 =C2=A0trying to match at string offset 0<br>
<br>
<br>
and this:<br>
<br>
$ perl -wle 'use re "debug"; print "Perl rules" =3D=
~ /er/ ? "Y" : "n"'<br>
Compiling REx "er"<br>
Final program:<br>
=C2=A0 =C2=A01: EXACT <er> (3)<br>
=C2=A0 =C2=A03: END (0)<br>
anchored "er" at 0 (checking anchored isall) minlen 2<br>
Matching REx "er" against "Perl rules"<br>
Intuit: trying to determine minimum start position...<br>
=C2=A0 doing 'check' fbm scan, [0..10] gave 1<br>
=C2=A0 Found anchored substr "er" at offset 1 (rx_origin now 1)..=
.<br>
=C2=A0 (multiline anchor test skipped)<br>
=C2=A0 try at offset...<br>
Intuit: Successfully guessed: match at offset 1<br>
Y<br>
Freeing REx: "er"<br>
<br>
<br>
The optimiser matches, and knows that if its crib[1] matches, the entire<br=
>
regex must match, so it doesn't even call into the engine.<br>
<br>
<br>
and this:<br>
<br>
$ perl -wle 'use re "debug"; print "Perl rules" =3D=
~ /Good *, #MoarVM/ ? "Y" : "n"'<br>
Compiling REx "Good *, #MoarVM"<br>
Final program:<br>
=C2=A0 =C2=A01: EXACT <Good> (3)<br>
=C2=A0 =C2=A03: STAR (6)<br>
=C2=A0 =C2=A04:=C2=A0 =C2=A0EXACT < > (0)<br>
=C2=A0 =C2=A06: EXACT <, #MoarVM> (10)<br>
=C2=A0 10: END (0)<br>
anchored "Good" at 0 floating ", #MoarVM" at 4..9223372=
036854775807 (checking floating) minlen 13<br>
n<br>
Freeing REx: "Good *, #MoarVM"<br>
<br>
The optimiser knows that the pattern can't match less than 13 character=
s, but<br>
the offered string is too short.<br>
<br>
The optimiser also knows at what offsets the within the target string the<b=
r>
fixed string must lie, if there is also some variable length stuff, but thi=
s<br>
is getting beyond what I know the plan for:<br>
<br>
$ perl -wle 'use re "debug"; print "Perl rules" =3D=
~ /Perl.*s/ ? "Y" : "n"'<br>
Compiling REx "Perl.*s"<br>
Final program:<br>
=C2=A0 =C2=A01: EXACT <Perl> (3)<br>
=C2=A0 =C2=A03: STAR (5)<br>
=C2=A0 =C2=A04:=C2=A0 =C2=A0REG_ANY (0)<br>
=C2=A0 =C2=A05: EXACT <s> (7)<br>
=C2=A0 =C2=A07: END (0)<br>
anchored "Perl" at 0 floating "s" at 4..922337203685477=
5807 (checking anchored) minlen 5<br>
Matching REx "Perl.*s" against "Perl rules"<br>
Intuit: trying to determine minimum start position...<br>
=C2=A0 doing 'check' fbm scan, [0..9] gave 0<br>
=C2=A0 Found anchored substr "Perl" at offset 0 (rx_origin now 0)=
...<br>
=C2=A0 doing 'other' fbm scan, [4..10] gave 9<br>
=C2=A0 Found floating substr "s" at offset 9 (rx_origin now 0)...=
<br>
=C2=A0 (multiline anchor test skipped)<br>
Intuit: Successfully guessed: match at offset 0<br>
=C2=A0 =C2=A00 <> <Perl rules>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0|=C2=A0 =C2=A00| 1:EXACT <Perl>(3)<br>
=C2=A0 =C2=A04 <Perl> < rules>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0|=C2=A0 =C2=A00| 3:STAR(5)<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|=C2=A0 =C2=A00| REG_ANY can match 6 times o=
ut of 2147483647...<br>
=C2=A0 =C2=A09 <Perl rule> <s>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0|=C2=A0 =C2=A01|=C2=A0 5:EXACT <s>(7)<br>
=C2=A0 10 <Perl rules> <>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|=C2=
=A0 =C2=A01|=C2=A0 7:END(0)<br>
Match successful!<br>
Y<br>
Freeing REx: "Perl.*s"<br>
<br>
<br>
If you're wearing Peril Sensitive Sunglasses, then the code that does t=
his is<br>
the self-recursive Perl_re_intuit_start() in<br>
<a href=3D"https://perl5.git.perl.org/perl.git/blob/HEAD:/regexec.c" rel=3D=
"noreferrer" target=3D"_blank">https://perl5.git.perl.org/perl.git/blob/HEA=
D:/regexec.c</a><br>
<br>
As a guide to reading that, for things like<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "=C2=A0 String too short...\n"));=
<br>
<br>
<br>
DEBUG_EXECUTE_r() only generates that debugging output if the command line<=
br>
flag -Dr is set, *and* the `use re "debug";` de facto sets -Dr, e=
ven on a<br>
perl binary compiled without all the other -D flags enabled.<br>
<br>
<br>
I know that PCRE also exposes some information it derives from the pattern<=
br>
which can be used to pre-match -- see PCRE_INFO_FIRSTCHARACTER and<br>
PCRE_INFO_REQUIREDCHAR in<br>
<a href=3D"https://www.pcre.org/original/doc/html/pcre_fullinfo.html" rel=
=3D"noreferrer" target=3D"_blank">https://www.pcre.org/original/doc/html/pc=
re_fullinfo.html</a><br>
<br>
I *thought* that Google's re2 library (which came out of code search) c=
ould<br>
return the longest fixed string (if any) as part of its API, but I can'=
t find<br>
that now. In <a href=3D"https://swtch.com/~rsc/regexp/regexp4.html" rel=3D"=
noreferrer" target=3D"_blank">https://swtch.com/~rsc/regexp/regexp4.html</a=
> it mentions building<br>
trigrams (directly) from parsing the regex (with ANDs and ORs) which is wha=
t<br>
some $ork code is doing (but then using PCRE, not re2).<br>
<br>
Anyway, I hope that this helps someone, and helps make Rakudo's parser =
faster<br>
<br>
I assume that the useful strategy is roughly<br>
<br>
0) Do the most simple thing - no alternations, anchored starts. Reject matc=
h<br>
=C2=A0 =C2=A0if not present<br>
1) Then 1+ character fixed substrings - Reject match if not present<br>
2) Then the big cheat of "string is actually fixed" - compile to =
a sub that<br>
=C2=A0 =C2=A0build a match result immediately, instead of the more general =
engine<br>
3) Then the even more general "fixed string as part of larger regex&qu=
ot;, where<br>
=C2=A0 =C2=A0failure is a fast-path, but otherwise the regular regex engine=
follows<br>
4) Minimum match length<br>
5) The stuff where one starts to tell the main engine how<br>
<br>
<br>
but of course no plan survives contact with the enemy.<br>
<br>
Nicholas Clark<br>
<br>
1: <a href=3D"https://en.wiktionary.org/wiki/crib" rel=3D"noreferrer" targe=
t=3D"_blank">https://en.wiktionary.org/wiki/crib</a> -- definitions 14 and =
23:<br>
=C2=A0 =C2=A0(usually in the plural) A collection of quotes or references f=
or<br>
=C2=A0 =C2=A0use in speaking, for assembling a written document, or as an a=
id to<br>
=C2=A0 =C2=A0a project of some sort; a crib sheet.<br>
=C2=A0 =C2=A0[and]<br>
=C2=A0 =C2=A0A cheat sheet or past test used by students; crib sheet.<br>
</blockquote></div>
--000000000000dc826305900081cf--