LLVM  11.0.0git
TrigramIndex.cpp
Go to the documentation of this file.
1 //===-- TrigramIndex.cpp - a heuristic for SpecialCaseList ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // TrigramIndex implements a heuristic for SpecialCaseList that allows to
10 // filter out ~99% incoming queries when all regular expressions in the
11 // SpecialCaseList are simple wildcards with '*' and '.'. If rules are more
12 // complicated, the check is defeated and it will always pass the queries to a
13 // full regex.
14 //
15 //===----------------------------------------------------------------------===//
16 
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 
21 #include <set>
22 #include <string>
23 #include <unordered_map>
24 
25 using namespace llvm;
26 
27 static const char RegexAdvancedMetachars[] = "()^$|+?[]\\{}";
28 
29 static bool isAdvancedMetachar(unsigned Char) {
30  return strchr(RegexAdvancedMetachars, Char) != nullptr;
31 }
32 
33 void TrigramIndex::insert(std::string Regex) {
34  if (Defeated) return;
35  std::set<unsigned> Was;
36  unsigned Cnt = 0;
37  unsigned Tri = 0;
38  unsigned Len = 0;
39  bool Escaped = false;
40  for (unsigned Char : Regex) {
41  if (!Escaped) {
42  // Regular expressions allow escaping symbols by preceding it with '\'.
43  if (Char == '\\') {
44  Escaped = true;
45  continue;
46  }
47  if (isAdvancedMetachar(Char)) {
48  // This is a more complicated regex than we can handle here.
49  Defeated = true;
50  return;
51  }
52  if (Char == '.' || Char == '*') {
53  Tri = 0;
54  Len = 0;
55  continue;
56  }
57  }
58  if (Escaped && Char >= '1' && Char <= '9') {
59  Defeated = true;
60  return;
61  }
62  // We have already handled escaping and can reset the flag.
63  Escaped = false;
64  Tri = ((Tri << 8) + Char) & 0xFFFFFF;
65  Len++;
66  if (Len < 3)
67  continue;
68  // We don't want the index to grow too much for the popular trigrams,
69  // as they are weak signals. It's ok to still require them for the
70  // rules we have already processed. It's just a small additional
71  // computational cost.
72  if (Index[Tri].size() >= 4)
73  continue;
74  Cnt++;
75  if (!Was.count(Tri)) {
76  // Adding the current rule to the index.
77  Index[Tri].push_back(Counts.size());
78  Was.insert(Tri);
79  }
80  }
81  if (!Cnt) {
82  // This rule does not have remarkable trigrams to rely on.
83  // We have to always call the full regex chain.
84  Defeated = true;
85  return;
86  }
87  Counts.push_back(Cnt);
88 }
89 
91  if (Defeated)
92  return false;
93  std::vector<unsigned> CurCounts(Counts.size());
94  unsigned Tri = 0;
95  for (size_t I = 0; I < Query.size(); I++) {
96  Tri = ((Tri << 8) + Query[I]) & 0xFFFFFF;
97  if (I < 2)
98  continue;
99  const auto &II = Index.find(Tri);
100  if (II == Index.end())
101  continue;
102  for (size_t J : II->second) {
103  CurCounts[J]++;
104  // If we have reached a desired limit, we have to look at the query
105  // more closely by running a full regex.
106  if (CurCounts[J] >= Counts[J])
107  return false;
108  }
109  }
110  return true;
111 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool isDefinitelyOut(StringRef Query) const
Returns true, if special case list definitely does not have a line that matches the query...
static const char RegexAdvancedMetachars[]
static bool isAdvancedMetachar(unsigned Char)
void insert(std::string Regex)
Inserts a new Regex into the index.
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:160
auto size(R &&Range, std::enable_if_t< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1473
#define I(x, y, z)
Definition: MD5.cpp:59
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57