Hi, In deterministic single-byte and UTF-8 LIKE matching, MatchText() scans text character by character after a '%' wildcard. In single-byte encodings every byte is a character, and in UTF-8 a leading byte never appears inside another character, so byte-wise search is safe whenever the literal starts with a leading byte.
I noticed Atsushi Ogawa's thread ("Use Boyer-Moore-Horspool for simple
LIKE contains patterns" [1]) and the low-entropy regression concerns
raised there. The attached patch takes a simpler, no-preprocessing
approach: it covers pure suffix (%literal), general '%' wildcard
scanning, and %literal% substrings without skip-table setup or FmgrInfo
caching. For long literals the BMH approach should win, and this
substring path could defer to it.
To avoid regressions on low-entropy and short inputs, both the general
'%' loop and like_match_substring() check leading bytes inline before
calling memchr(), verify endpoint/interior bytes inline before calling
memcmp(), and bypass the suffix/substring pre-scan below 12 bytes.
Nondeterministic collations, C-locale ILIKE (MATCH_LOWER), and other
multibyte encodings keep the existing loops.
Tested against master (b59ec01) with make check (243/243 passed,
--enable-cassert --with-icu, LC_ALL=en_US.UTF-8), with new tests in
strings.sql, collate.utf8.sql, and collate.icu.utf8.sql (also passing
on unpatched master) plus differential fuzzing over UTF-8, LATIN1,
EUC_JP, bytea, ILIKE, ESCAPE, and nondeterministic ICU collations.
Benchmarks (Apple M4 Pro, macOS arm64, clang 21, -O2, UTF-8 / C locale,
jit=off, max_parallel_workers_per_gather=0, 6 interleaved AB/BA pairs,
median ms, host 1m load ~7.0, identical result counts):
500k rows: a = md5(g::text)||'xx'||md5((g*7)::text) (66B),
u = 'αβγ'||md5(g::text)||'日本語'||md5((g*3)::text) (79B)
a LIKE '%ab': 50.85 ms -> 13.13 ms (3.87x)
a LIKE '%abc%': 50.94 ms -> 37.36 ms (1.36x)
u LIKE '%日本語ab%': 40.42 ms -> 23.32 ms (1.73x)
u LIKE '%a%b%c%': 43.35 ms -> 36.93 ms (1.18x)
u LIKE '%a%b%': 36.21 ms -> 30.55 ms (1.20x)
Adversarial / short (300k rows, single-column tables:
a = repeat('a',60)||g, ab = repeat('ab',30)||g,
eu = repeat('é',60)||g, s = (g%1000)::text):
a LIKE '%a_z': 84.96 ms -> 81.05 ms (~1.00x, noise)
a LIKE '%aaaz%': 98.18 ms -> 17.87 ms (5.49x)
a LIKE '%aaza%': 85.54 ms -> 19.15 ms (4.47x)
ab LIKE '%a_z': 49.60 ms -> 48.96 ms (1.01x)
eu LIKE '%é_z': 103.59 ms -> 102.29 ms (1.01x)
s LIKE '%9%1': 7.79 ms -> 7.55 ms (~1.00x, noise)
s LIKE '%9%': 7.91 ms -> 7.90 ms (1.01x)
Found and written by an AI optimization pipeline I run.
[1]
https://www.mail-archive.com/[email protected]/msg240336.html
Regards,
Josh Hurst
v1-0001-Add-memchr-memcmp-fast-paths-for-LIKE.patch
Description: Binary data
