TrigramIndex.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- TrigramIndex.h - a heuristic for SpecialCaseList --------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //===----------------------------------------------------------------------===//
  12. //
  13. // TrigramIndex implements a heuristic for SpecialCaseList that allows to
  14. // filter out ~99% incoming queries when all regular expressions in the
  15. // SpecialCaseList are simple wildcards with '*' and '.'. If rules are more
  16. // complicated, the check is defeated and it will always pass the queries to a
  17. // full regex.
  18. //
  19. // The basic idea is that in order for a wildcard to match a query, the query
  20. // needs to have all trigrams which occur in the wildcard. We create a trigram
  21. // index (trigram -> list of rules with it) and then count trigrams in the query
  22. // for each rule. If the count for one of the rules reaches the expected value,
  23. // the check passes the query to a regex. If none of the rules got enough
  24. // trigrams, the check tells that the query is definitely not matched by any
  25. // of the rules, and no regex matching is needed.
  26. // A similar idea was used in Google Code Search as described in the blog post:
  27. // https://swtch.com/~rsc/regexp/regexp4.html
  28. //
  29. //===----------------------------------------------------------------------===//
  30. #ifndef LLVM_SUPPORT_TRIGRAMINDEX_H
  31. #define LLVM_SUPPORT_TRIGRAMINDEX_H
  32. #include "llvm/ADT/SmallVector.h"
  33. #include "llvm/ADT/StringRef.h"
  34. #include <string>
  35. #include <unordered_map>
  36. #include <vector>
  37. namespace llvm {
  38. class TrigramIndex {
  39. public:
  40. /// Inserts a new Regex into the index.
  41. void insert(const std::string &Regex);
  42. /// Returns true, if special case list definitely does not have a line
  43. /// that matches the query. Returns false, if it's not sure.
  44. bool isDefinitelyOut(StringRef Query) const;
  45. /// Returned true, iff the heuristic is defeated and not useful.
  46. /// In this case isDefinitelyOut always returns false.
  47. bool isDefeated() { return Defeated; }
  48. private:
  49. // If true, the rules are too complicated for the check to work, and full
  50. // regex matching is needed for every rule.
  51. bool Defeated = false;
  52. // The minimum number of trigrams which should match for a rule to have a
  53. // chance to match the query. The number of elements equals the number of
  54. // regex rules in the SpecialCaseList.
  55. std::vector<unsigned> Counts;
  56. // Index holds a list of rules indices for each trigram. The same indices
  57. // are used in Counts to store per-rule limits.
  58. // If a trigram is too common (>4 rules with it), we stop tracking it,
  59. // which increases the probability for a need to match using regex, but
  60. // decreases the costs in the regular case.
  61. std::unordered_map<unsigned, SmallVector<size_t, 4>> Index{256};
  62. };
  63. } // namespace llvm
  64. #endif // LLVM_SUPPORT_TRIGRAMINDEX_H
  65. #ifdef __GNUC__
  66. #pragma GCC diagnostic pop
  67. #endif