Commit df8061008f for perl

commit df8061008f94b9795f265fef13f93fc2d9ad9b51
Author: Richard Leach <rich+perl@hyphen-dash-hyphen.info>
Date:   Tue Nov 24 12:14:09 2026 +0000

    ANYOFM stclass for 1-octet length TRIE words

    In the event that a compiled TRIE consists of only single-octet words,
    AHO-CORASICK has the potential to be more overhead than benefit when
    compared against a synthetic single-character start class.

    This commit checks if an ANYOFM start class can be built and, if so,
    will use it. For example:

        Compiling REx "s\w+|q"
        Final program:
           1: TRIE-EXACT (11)
              <s> (5)
           5:   PLUS (11)
           6:     POSIXD[\w] (0)
              <q> (11)
          11: END (0)
        stclass ANYOFM[qs] minlen 1

    If only the generic ANYOF is viable, AHO-CORASICK is still selected:

        Compiling REx "z\w+|q"
        Final program:
           1: TRIE-EXACT (11)
              <z> (5)
           5:   PLUS (11)
           6:     POSIXD[\w] (0)
              <q> (11)
          11: END (0)
        stclass AHOCORASICK-EXACT minlen 1

    This change seems worthwhile, since:

    * ANYOFM and AHO-CORASICK have almost identical performance when the
     latter's repeat-block scanner can kick in.

    ANYOFM: ./perl -e 'my $x = "A" x 800_000_000; $x =~ /s\w+|q/;'

           453,366,925      cycles                           #    3.878 GHz
            13,407,249      stalled-cycles-frontend          #    2.96% frontend cycles idle
           703,149,992      instructions                     #    1.55  insn per cycle
            90,547,826      branches                         #  774.565 M/sec
               989,599      branch-misses                    #    1.09% of all branches

    AHO-CORASICK: ./perl -e 'my $x = "A" x 800_000_000; $x =~ /z\w+|q/;'

           479,231,999      cycles                           #    4.715 GHz
            13,398,039      stalled-cycles-frontend          #    2.80% frontend cycles idle
           978,187,168      instructions                     #    2.04  insn per cycle
           315,554,254      branches                         #    3.105 G/sec
               990,790      branch-misses                    #    0.31% of all branches

    * ANYOFM is much faster against random/non-block text:

    ANYOFM: ./perl -e 'my $x = "abcdefghijklmnop_r_tuvwxy_1234567890!ox--xo!0987654321_yxvut_r_ponmlkjihgfedcba"
                    x 10_000_000; $x =~ /s\w+|q/;'

           523,612,590      cycles                           #    4.745 GHz
            13,334,168      stalled-cycles-frontend          #    2.55% frontend cycles idle
           730,541,836      instructions                     #    1.40  insn per cycle
            86,003,336      branches                         #  779.339 M/sec
             1,039,367      branch-misses                    #    1.21% of all branches

    AHO-CORASICK: ./perl -e 'my $x = "abcdefghijklmnop_r_tuvwxy_1234567890!ox--xo!0987654321_yxvut_r_ponmlkjihgfedcba"
                    x 10_000_000; $x =~ /z\w+|q/;'

         3,100,395,270      cycles                           #    4.723 GHz
            43,049,589      stalled-cycles-frontend          #    1.39% frontend cycles idle
        12,452,977,695      instructions                     #    4.02  insn per cycle
         3,082,140,879      branches                         #    4.695 G/sec
             9,618,036      branch-misses                    #    0.31% of all branches

    Note: For completeness, ANYOF performs much worse than AHO-CORASICK on repeating text:

         1,932,888,002      cycles                           #    4.694 GHz
            14,439,293      stalled-cycles-frontend          #    0.75% frontend cycles idle
        10,478,511,084      instructions                     #    5.42  insn per cycle
         2,415,629,259      branches                         #    5.866 G/sec
             1,005,288      branch-misses                    #    0.04% of all branches

    but ~50% better on random/non-block text:

         2,011,250,260      cycles                           #    4.702 GHz
            16,542,140      stalled-cycles-frontend          #    0.82% frontend cycles idle
        10,383,850,165      instructions                     #    5.16  insn per cycle
         2,382,054,102      branches                         #    5.569 G/sec
             1,066,920      branch-misses                    #    0.04% of all branches

diff --git a/regcomp_trie.c b/regcomp_trie.c
index 7670902c93..3cb84cf1db 100644
--- a/regcomp_trie.c
+++ b/regcomp_trie.c
@@ -1908,6 +1908,68 @@ S_dump_ac_prefilters(pTHX_ const reg_ac_data *aho, const U32 depth)
 }
 #endif

+/* For a Trie containing only single-octet words, an optimized stclass like
+ * ANYOFM should be more efficient and streamlined than AHO-CORASICK.
+ * (Performance of the generic ANYOF against AHO-CORASICK will likely depend
+ * upon the nature of the target string being searched.) */
+static regnode *
+S_one_octet_trie_stclass(pTHX_ RExC_state_t *pRExC_state, const regnode *source) {
+    const U32 trie_offset = TRIE_DATA_SLOT(source);
+    const reg_trie_data * const trie = (reg_trie_data *)RExC_rxi->data->data[trie_offset];
+
+    /* Caller should have checked these */
+    assert(trie->maxlen == 1);
+    assert(!(trie->prop_flags & TRIE_FOLD_MASK));
+
+    if (TRIE_CONTAINS_WIDE(trie) )      /* Looking for single U8s only */
+        return NULL;
+
+    /* Shouldn't be any empty words */
+    assert(trie->minlen == 1);
+    assert(trie->startstate == 1);
+    assert(!trie->states[1].wordnum);
+    assert(trie->states[1].trans.base == 1);
+
+    SV * cp_list = NULL;
+
+    for (U32 ch = trie->states[1].min_octet; ch <= trie->states[1].max_octet; ch++) {
+        const reg_trie_trans * const trans = &trie->trans[1 + ch];
+        if (trans->next) {
+            assert(trans->check == 1);
+            assert(UVCHR_IS_INVARIANT(ch));
+            assert(trie->states[trans->next].wordnum);
+            cp_list = add_cp_to_invlist(cp_list, ch);
+        } else {
+            assert(trans->check == 0);
+        }
+    }
+    assert(cp_list);
+
+    regnode_ssc *ssc;
+    Newxz(ssc, 1, regnode_ssc);
+    regnode *stclass = (regnode *) ssc;
+
+    if (optimize_invariant_class(cp_list, stclass) == END) {
+#if 1
+        /* No optimization was found. A simple ANYOF may not suffice */
+        SvREFCNT_dec(cp_list);
+        Safefree(ssc);
+        return NULL;
+#else
+        /* Alternative code in case ANYOF turns out to be desirable */
+        OP(stclass) = ANYOF;
+        ARG1u_SET(stclass, ANYOF_MATCHES_NONE_OUTSIDE_BITMAP_VALUE);
+
+        populate_anyof_bitmap_from_invlist(stclass, &cp_list);
+        assert(! cp_list);
+#endif
+    }
+    SvREFCNT_dec(cp_list);
+    const U32 n = reg_add_data(pRExC_state, STR_WITH_LEN("f"));
+    RExC_rxi->data->data[n] = (void *) ssc;
+    return stclass;
+}
+
 regnode *
 Perl_construct_ahocorasick_from_trie(pTHX_ RExC_state_t *pRExC_state, regnode *source, U32 depth)
 {
@@ -1935,9 +1997,20 @@ Perl_construct_ahocorasick_from_trie(pTHX_ RExC_state_t *pRExC_state, regnode *s
    the second word where we would try 'g' and succeed, proceeding to match
    'cdgu'.
  */
- /* add a fail transition */
+
     const U32 trie_offset = TRIE_DATA_SLOT(source);
     reg_trie_data *trie = (reg_trie_data *)RExC_rxi->data->data[trie_offset];
+
+    /* But first, if all words contain a single character, check if an
+     * (optimized) single-U8 start class can be built */
+    if (UNLIKELY(trie->maxlen == 1) && !(trie->prop_flags & TRIE_FOLD_MASK)
+        ) {
+        regnode * const anyof = S_one_octet_trie_stclass(aTHX_ pRExC_state, source);
+        if (anyof)
+            return anyof;
+    }
+
+ /* add a fail transition */
     U32 *q;
     const U32 numstates = trie->statecount;
     U32 q_read = 0;