[clang] Fix quadratic slowdown in AST matcher parent map generation (PR #87824)

via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 9 08:49:08 PDT 2024


================
@@ -61,7 +61,28 @@ class ParentMapContext::ParentMap {
   template <typename, typename...> friend struct ::MatchParents;
 
   /// Contains parents of a node.
-  using ParentVector = llvm::SmallVector<DynTypedNode, 2>;
+  class ParentVector {
+  public:
+    ParentVector() = default;
+    explicit ParentVector(size_t n, const DynTypedNode &value) {
+      Items.reserve(n);
+      for (; n > 0; --n) {
+        push_back(value);
+      }
+    }
+    bool contains(const DynTypedNode &value) {
+      return Seen.contains(value);
+    }
+    void push_back(const DynTypedNode &value) {
+      if (!value.getMemoizationData() || Seen.insert(value).second) {
+        Items.push_back(value);
+      }
----------------
higher-performance wrote:

Thank you! Applied the suggestion. Regarding the `push_back`, I don't believe you can use `std::fill_n` to fill the items directly, due to the additional checks that first occur inside `push_back`. (I could've optimized that away by lifting out the checks, but that didn't seem worthwhile vs. the clarity.)

https://github.com/llvm/llvm-project/pull/87824


More information about the cfe-commits mailing list