Commit 1c97e98f03 for perl
commit 1c97e98f030ed0cc2c2f54b717244b051dfc9131
Author: Richard Leach <rich+perl@hyphen-dash-hyphen.info>
Date: Fri Oct 23 22:16:23 2026 +0000
re_intuit_start: call memchr directly for single character searches
`Perl_re_intuit_start` uses `Perl_fbm_instr` to find a possible start
position for a regular expression match. The latter special-cases one
and two byte (and zero, but shouldn't be relevant here) searches
before falling back to Boyer-Moore.
The special case for single byte searches is to use `memchr`, which
is highly optimized on many platforms. Searching for a single byte
character is very common - delimiters, for example. It therefore
seems worthwhile for `Perl_re_intuit_start` to directly call
`memchr` itself, rather than indirectly through `Perl_fbm_instr`.
_gcov_ numbers from the test harness show good use of this
direct path:
```
26771878: 1335: check_at = (SvCUR(check) == 1 && !SvTAIL(check))
18738215: 1336: ? (char *)memchr(start_point, *(const U8 *)SvPVX_const(check),
18738215: 1337: end_point - start_point)
95683600: 1338: : fbm_instr( start_point, end_point,
-: 1339: check, multiline ? FBMrf_MULTILINE : 0);
81149655: 1501: s = (SvCUR(must) == 1 && !SvTAIL(must))
39823408: 1502: ? (char *)memchr( (unsigned char*)from,
39823408: 1503: *(const U8 *)SvPVX_const(must),
39823408: 1504: (unsigned char*)to - (unsigned char*)from)
41326247: 1505: : fbm_instr(
-: 1506: (unsigned char*)from,
-: 1507: (unsigned char*)to,
-: 1508: must,
-: 1509: multiline ? FBMrf_MULTILINE : 0
-: 1510: );
```
diff --git a/regexec.c b/regexec.c
index 083c044dcb..62ddaa9c43 100644
--- a/regexec.c
+++ b/regexec.c
@@ -1332,7 +1332,10 @@ Perl_re_intuit_start(pTHX_
}
}
- check_at = fbm_instr( start_point, end_point,
+ check_at = (SvCUR(check) == 1 && !SvTAIL(check))
+ ? (char *)memchr(start_point, *(const U8 *)SvPVX_const(check),
+ end_point - start_point)
+ : fbm_instr( start_point, end_point,
check, multiline ? FBMrf_MULTILINE : 0);
DEBUG_EXECUTE_r(re_printf(
@@ -1495,7 +1498,11 @@ Perl_re_intuit_start(pTHX_
));
}
else {
- s = fbm_instr(
+ s = (SvCUR(must) == 1 && !SvTAIL(must))
+ ? (char *)memchr( (unsigned char*)from,
+ *(const U8 *)SvPVX_const(must),
+ (unsigned char*)to - (unsigned char*)from)
+ : fbm_instr(
(unsigned char*)from,
(unsigned char*)to,
must,