diff options
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Support/CMakeLists.txt | 1 | ||||
-rw-r--r-- | llvm/lib/Support/SpecialCaseList.cpp | 12 | ||||
-rw-r--r-- | llvm/lib/Support/TrigramIndex.cpp | 98 |
3 files changed, 109 insertions, 2 deletions
diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt index 33ab04df5b5..d6cbfbda2c4 100644 --- a/llvm/lib/Support/CMakeLists.txt +++ b/llvm/lib/Support/CMakeLists.txt @@ -94,6 +94,7 @@ add_llvm_library(LLVMSupport ThreadPool.cpp Timer.cpp ToolOutputFile.cpp + TrigramIndex.cpp Triple.cpp Twine.cpp Unicode.cpp diff --git a/llvm/lib/Support/SpecialCaseList.cpp b/llvm/lib/Support/SpecialCaseList.cpp index 62dcafefd88..df524b35235 100644 --- a/llvm/lib/Support/SpecialCaseList.cpp +++ b/llvm/lib/Support/SpecialCaseList.cpp @@ -15,6 +15,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/SpecialCaseList.h" +#include "llvm/Support/TrigramIndex.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringSet.h" @@ -33,10 +34,15 @@ namespace llvm { /// literal strings than Regex. struct SpecialCaseList::Entry { StringSet<> Strings; + TrigramIndex Trigrams; std::unique_ptr<Regex> RegEx; bool match(StringRef Query) const { - return Strings.count(Query) || (RegEx && RegEx->match(Query)); + if (Strings.count(Query)) + return true; + if (Trigrams.isDefinitelyOut(Query)) + return false; + return RegEx && RegEx->match(Query); } }; @@ -104,10 +110,12 @@ bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) { StringRef Category = SplitRegexp.second; // See if we can store Regexp in Strings. + auto &Entry = Entries[Prefix][Category]; if (Regex::isLiteralERE(Regexp)) { - Entries[Prefix][Category].Strings.insert(Regexp); + Entry.Strings.insert(Regexp); continue; } + Entry.Trigrams.insert(Regexp); // Replace * with .* for (size_t pos = 0; (pos = Regexp.find('*', pos)) != std::string::npos; diff --git a/llvm/lib/Support/TrigramIndex.cpp b/llvm/lib/Support/TrigramIndex.cpp new file mode 100644 index 00000000000..bba996e58ec --- /dev/null +++ b/llvm/lib/Support/TrigramIndex.cpp @@ -0,0 +1,98 @@ +//===-- TrigramIndex.cpp - a heuristic for SpecialCaseList ----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// TrigramIndex implements a heuristic for SpecialCaseList that allows to +// filter out ~99% incoming queries when all regular expressions in the +// SpecialCaseList are simple wildcards with '*' and '.'. If rules are more +// complicated, the check is defeated and it will always pass the queries to a +// full regex. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/TrigramIndex.h" +#include "llvm/ADT/SmallVector.h" + +#include <unordered_map> +#include <set> +#include <string> + +using namespace llvm; + +static const char RegexAdvancedMetachars[] = "()^$|+?[]\\{}"; + +static bool isSimpleWildcard(StringRef Str) { + // Check for regex metacharacters other than '*' and '.'. + return Str.find_first_of(RegexAdvancedMetachars) == StringRef::npos; +} + +void TrigramIndex::insert(std::string Regex) { + if (Defeated) return; + if (!isSimpleWildcard(Regex)) { + Defeated = true; + return; + } + + std::set<unsigned> Was; + unsigned Cnt = 0; + unsigned Tri = 0; + unsigned Len = 0; + for (unsigned Char : Regex) { + if (Char == '.' || Char == '*') { + Tri = 0; + Len = 0; + continue; + } + Tri = ((Tri << 8) + Char) & 0xFFFFFF; + Len++; + if (Len < 3) + continue; + // We don't want the index to grow too much for the popular trigrams, + // as they are weak signals. It's ok to still require them for the + // rules we have already processed. It's just a small additional + // computational cost. + if (Index[Tri].size() >= 4) + continue; + Cnt++; + if (!Was.count(Tri)) { + // Adding the current rule to the index. + Index[Tri].push_back(Counts.size()); + Was.insert(Tri); + } + } + if (!Cnt) { + // This rule does not have remarkable trigrams to rely on. + // We have to always call the full regex chain. + Defeated = true; + return; + } + Counts.push_back(Cnt); +} + +bool TrigramIndex::isDefinitelyOut(StringRef Query) const { + if (Defeated) + return false; + std::vector<unsigned> CurCounts(Counts.size()); + unsigned Tri = 0; + for (size_t I = 0; I < Query.size(); I++) { + Tri = ((Tri << 8) + Query[I]) & 0xFFFFFF; + if (I < 2) + continue; + const auto &II = Index.find(Tri); + if (II == Index.end()) + continue; + for (size_t J : II->second) { + CurCounts[J]++; + // If we have reached a desired limit, we have to look at the query + // more closely by running a full regex. + if (CurCounts[J] >= Counts[J]) + return false; + } + } + return true; +} |