ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MultiSearch.h File Reference

The engineered search of many patterns at once, beyond the book: MultiSearcher finds all the occurrences in a text \( T \) of \( n \) bytes of \( k \) patterns \( P_0 , \ldots , P_{k-1} \): one pattern by StringSearcher [see StringSearch.h], up to 64 by a filter of shuffles of bytes, more by a filter of shift-or on super characters, and by the automaton of Aho and Corasick [see AhoCorasick.h] where a filter lets too much through; the benchmark of the multiple search compares it with AhoCorasick and with Vectorscan (the portable Hyperscan). More...

#include "ADS/Contract.h"
#include "ADS/string/AhoCorasick.h"
#include <array>
#include <cstddef>
#include <cstdint>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "ADS/string/impl/MultiSearch.tpp"

Go to the source code of this file.

Classes

class  ADS::MultiSearcher
 the search of many patterns at once, by a SIMD filter or by the automaton of Aho and Corasick More...

Namespaces

namespace  ADS

Detailed description

The engineered search of many patterns at once, beyond the book: MultiSearcher finds all the occurrences in a text \( T \) of \( n \) bytes of \( k \) patterns \( P_0 , \ldots , P_{k-1} \): one pattern by StringSearcher [see StringSearch.h], up to 64 by a filter of shuffles of bytes, more by a filter of shift-or on super characters, and by the automaton of Aho and Corasick [see AhoCorasick.h] where a filter lets too much through; the benchmark of the multiple search compares it with AhoCorasick and with Vectorscan (the portable Hyperscan).

The filter of shuffles puts the patterns, sorted, into 8 buckets of consecutive ones, so that the patterns of a bucket tend to share their first bytes (the patterns go into buckets, and the buckets are told apart by the 8 bits of a byte, as in the FDR matcher of Hyperscan). The place \( i \) of \( T \) is a candidate for the bucket \( b \) if, for every \( r < q \), some pattern of \( b \) has the low half (4 bits) of \( T[ i + r ] \) as the low half of its byte \( r \) and some pattern of \( b \) (maybe another) the high half as the high half, i.e., if the bit \( b \) of

\[ C_i = \bigwedge_{ r < q } \mathit{lo}_r[ T[ i + r ] \bmod 16 ] \wedge \mathit{hi}_r[ \lfloor T[ i + r ] / 16 \rfloor ] \]

is set, the bit \( b \) of the tables \( \mathit{lo}_r \) and \( \mathit{hi}_r \) (16 bytes each) set for the halves of the bytes \( r \) of the patterns of \( b \). Here \( q \) is the least number of bytes, at most 8 and at most the shortest pattern, that brings the probability of a place to pass below \( 2^{-10} \), the bytes of the text drawn with the frequencies \( f( x ) \) of the bytes of the patterns,

\[ \sum_b \prod_{ r < q } \sum_x f( x ) \, [ \text{the bit } b \text{ of } C( x , r ) \text{ is set} ] < 2^{-10} , \]

so that a large alphabet needs one or two bytes and a small one more. A table of 16 bytes is one register, and one lookup of bytes in it (TableLookupBytes() of Highway, with ADS_WITH_HIGHWAY) takes 16 places of \( T \) at once, or 32 in a vector of 32 bytes, so that a block of 32 places costs \( q \) loads, \( 2 q \) lookups a vector and a few logical operations, with no branch but the one on a candidate; without Highway the places are looked up one at a time. A candidate is checked against the patterns of its buckets, by their first 8 bytes in one comparison of words and then by the rest. Every occurrence makes its place a candidate for its bucket, so that nothing is missed; when the bytes compared with the patterns exceed 16 a place on average (\( 16 s + 4096 \) after the block at \( s \), a word of 8 bytes for each pattern checked and the bytes past it that memcmp() compares: patterns everywhere, long patterns whose first bytes are everywhere, or a text whose bytes the filter cannot tell apart), the automaton goes on from the first place not tested, as StringSearcher does from its own count of the bytes compared, its occurrences that start before it left out (and count() takes the count of the automaton from a little before that place, less the occurrences before it in a window of the length of the longest pattern). Since the bytes compared before the switch are at most \( 16 n + 4096 \) plus those of one block, and the automaton is linear, findAll() takes \( O( n + \mathit{occ} \log \mathit{occ} ) \) time in the worst case, \( \mathit{occ} \) the occurrences (the logarithm for their sort), and count() \( O( n ) \); automatonFrom() tells where the switch happens.

Above 64 patterns the halves of the bytes of a bucket cover them all, and the filter of shift-or of FDR takes over, on the first \( L = \min( 8 , \ell ) \) bytes of the patterns, \( \ell \) the shortest. The super character \( x( p ) \) of the place \( p \) is \( T[ p ] \) with the low 3 bits of \( T[ p + 1 ] \), 11 bits, and its mask \( \mathit{sh}[ x ] \) of 64 bits has the bit \( b \) of its byte \( L - 1 - r \) off if a pattern of the bucket \( b \) has that super character at its place \( r < L \) (its byte \( r \) and the low bits of the byte \( r + 1 \), any of them past its end), the bytes from \( L \) on all off. A pattern that starts at \( s \) can then end its window at \( e = s + L - 1 \) only if the bit \( b \) of the byte \( e - p \) of the mask of \( p \) is off for every \( e - L < p \le e \), i.e., only if

\[ \mathit{St}_e = \bigvee_{ t < L } \mathit{sh}[ x( e - t ) ]_t \]

has a bit off. A block of 8 places adds each mask moved by \( d \) bytes into a register of 16 bytes, the states of its 8 ends and of the next 8, with one load, one shift of bytes and one OR a place, the 11 bits read from two words of the text with no other load; the table of the masks is 16 KB. A state with a bit off sends its start to a table of the prefixes of \( L \) bytes of the patterns (chained, by a multiplicative hash), which checks the patterns of that prefix. When the patterns have 8 bytes or more, the filter takes the masks of the even places alone (a stride of 2, as FDR does on long patterns), half the loads: a window of \( W = \min( 8 , \lfloor \ell / 2 \rfloor ) \) even places, and each pattern twice, at its even offsets for the occurrences that start at an even place (in 4 buckets, the bits 0 to 3) and at its odd ones for the others (the bits 4 to 7), kept only if a window passes with probability below \( 2^{-8} \) on a text of the super characters of the patterns, since its buckets hold twice the patterns.

findAll() gives the pairs \( ( \mathit{start} , \mathit{number} ) \) of the occurrences, by increasing start and number, and count() their number; the patterns are numbered in their order, and the empty pattern is not allowed.

The contracts [see Contract.h]: a pattern is not empty; findAll() gives, for each pattern, the occurrences of naiveMatching() [see StringMatching.h] (an audit). The functions are defined in MultiSearch.tpp.

See also
X. Wang, Y. Hong, H. Chang, K. Park, G. Langdale, J. Hu, H. Zhu, Hyperscan: a fast multi-pattern regex matcher for modern CPUs, Proceedings of the 16th USENIX Symposium on Networked Systems Design and Implementation (NSDI), 631-648, 2019, https://www.usenix.org/conference/nsdi19/presentation/wang-xiang
A. V. Aho, M. J. Corasick, Efficient string matching: an aid to bibliographic search, Communications of the ACM 18(6), 333-340, 1975, doi:10.1145/360825.360855
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa