From 8f1c528016341f686a97294fa2b59e9384f29c46 Mon Sep 17 00:00:00 2001 From: Mamat9q <167594514+Mamat9q@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:04:45 +0700 Subject: [PATCH] Prefilter: skip the quadratic dedup for very large string sets --- re2/prefilter.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/re2/prefilter.cc b/re2/prefilter.cc index decc412ba..b21900bbb 100644 --- a/re2/prefilter.cc +++ b/re2/prefilter.cc @@ -171,7 +171,14 @@ void Prefilter::SimplifyStringSet(SSet* ss) { Prefilter* Prefilter::OrStrings(SSet* ss) { Prefilter* or_prefilter = new Prefilter(NONE); - SimplifyStringSet(ss); + // SimplifyStringSet() compares every string against every longer + // string, so it is quadratic in the size of the set. Above this + // size, skip it: it only removes redundant atoms, which is purely + // an optimisation, and keeping the redundant atoms is always + // correct, because the prefilter only has to be a necessary + // condition for a match. + if (ss->size() <= 4096) + SimplifyStringSet(ss); for (SSIter i = ss->begin(); i != ss->end(); ++i) or_prefilter = Or(or_prefilter, FromString(*i)); return or_prefilter;