TrigramIndex.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include "llvm/Support/TrigramIndex.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include <set>
  19. using namespace llvm;
  20. static const char RegexAdvancedMetachars[] = "()^$|+?[]\\{}";
  21. static bool isAdvancedMetachar(unsigned Char) {
  22. return strchr(RegexAdvancedMetachars, Char) != nullptr;
  23. }
  24. void TrigramIndex::insert(const std::string &Regex) {
  25. if (Defeated) return;
  26. std::set<unsigned> Was;
  27. unsigned Cnt = 0;
  28. unsigned Tri = 0;
  29. unsigned Len = 0;
  30. bool Escaped = false;
  31. for (unsigned Char : Regex) {
  32. if (!Escaped) {
  33. // Regular expressions allow escaping symbols by preceding it with '\'.
  34. if (Char == '\\') {
  35. Escaped = true;
  36. continue;
  37. }
  38. if (isAdvancedMetachar(Char)) {
  39. // This is a more complicated regex than we can handle here.
  40. Defeated = true;
  41. return;
  42. }
  43. if (Char == '.' || Char == '*') {
  44. Tri = 0;
  45. Len = 0;
  46. continue;
  47. }
  48. }
  49. if (Escaped && Char >= '1' && Char <= '9') {
  50. Defeated = true;
  51. return;
  52. }
  53. // We have already handled escaping and can reset the flag.
  54. Escaped = false;
  55. Tri = ((Tri << 8) + Char) & 0xFFFFFF;
  56. Len++;
  57. if (Len < 3)
  58. continue;
  59. // We don't want the index to grow too much for the popular trigrams,
  60. // as they are weak signals. It's ok to still require them for the
  61. // rules we have already processed. It's just a small additional
  62. // computational cost.
  63. if (Index[Tri].size() >= 4)
  64. continue;
  65. Cnt++;
  66. if (!Was.count(Tri)) {
  67. // Adding the current rule to the index.
  68. Index[Tri].push_back(Counts.size());
  69. Was.insert(Tri);
  70. }
  71. }
  72. if (!Cnt) {
  73. // This rule does not have remarkable trigrams to rely on.
  74. // We have to always call the full regex chain.
  75. Defeated = true;
  76. return;
  77. }
  78. Counts.push_back(Cnt);
  79. }
  80. bool TrigramIndex::isDefinitelyOut(StringRef Query) const {
  81. if (Defeated)
  82. return false;
  83. std::vector<unsigned> CurCounts(Counts.size());
  84. unsigned Tri = 0;
  85. for (size_t I = 0; I < Query.size(); I++) {
  86. Tri = ((Tri << 8) + Query[I]) & 0xFFFFFF;
  87. if (I < 2)
  88. continue;
  89. const auto &II = Index.find(Tri);
  90. if (II == Index.end())
  91. continue;
  92. for (size_t J : II->second) {
  93. CurCounts[J]++;
  94. // If we have reached a desired limit, we have to look at the query
  95. // more closely by running a full regex.
  96. if (CurCounts[J] >= Counts[J])
  97. return false;
  98. }
  99. }
  100. return true;
  101. }