TrigramIndex.cpp 3.1 KB

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