perl5 icon indicating copy to clipboard operation
perl5 copied to clipboard

Does a TRIE-EXACT only pattern do unnecessary work?

Open richardleach opened this issue 1 month ago • 2 comments

Description If you run: perl -e 'use re q|debug|; my $x = "x" x 10; 1 while $x =~ /xx|ab/aag;', the pattern has compiled to a TRIE-EXACT node:

Compiling REx "xx|ab"
Final program:
   1: TRIE-EXACT[ax] (9)
      <xx> 
      <ab> 
   9: END (0)

The output for each match is something like:

stclass AHOCORASICK-EXACT[ax] minlen 2 
Matching REx "xx|ab" against "xxxxxxxxxx"
Matching stclass AHOCORASICK-EXACT[ax] against "xxxxxxxxxx" (10 bytes)
   0 <> <xxxxxxxxxx>         |   0|  Charid:  1 CP:  78 State:    1, word=0 - legal
   1 <x> <xxxxxxxxx>         |   0|  Charid:  1 CP:  78 State:    2, word=0 - legal
   2 <xx> <xxxxxxxx>         |   0|  Charid:  1 CP:  78 State:    3, word=1 - accepting
Matches word #1 at position 0. Trying full pattern...
   0 <> <xxxxxxxxxx>         |   0| 1:TRIE-EXACT[ax](9)
   0 <> <xxxxxxxxxx>         |   0| TRIE: State:    1 Accepted: N TRIE: Charid:  1 CP:  78 After State:    2
   1 <x> <xxxxxxxxx>         |   0| TRIE: State:    2 Accepted: N TRIE: Charid:  1 CP:  78 After State:    3
   2 <xx> <xxxxxxxx>         |   0| TRIE: State:    3 Accepted: Y TRIE: Charid:  0 CP:   0 After State:    0
                             |   0| TRIE: got 1 possible matches
                             |   0| TRIE matched word #1, continuing
                             |   0| TRIE: only one match left, short-circuiting: #1 <xx>
   2 <xx> <xxxxxxxx>         |   0| 9:END(0)
Match successful!

Is it necessary to try the full TRIE-EXACT pattern when in effect, AHOCORASICK-EXACT[ax] has already found a whole pattern match?

Perl configuration blead

richardleach avatar Oct 19 '25 12:10 richardleach