[clang] Reduce memory usage in AST parent map generation by lazily checking if nodes have been seen (PR #129934)

via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 6 10:24:00 PST 2025


================
@@ -70,16 +94,38 @@ class ParentMapContext::ParentMap {
         push_back(Value);
     }
     bool contains(const DynTypedNode &Value) {
-      return Seen.contains(Value);
+      assert(Value.getMemoizationData());
+      bool found = FragileLazySeenCache.contains(&Value);
+      while (!found && ItemsProcessed < Items.size()) {
+        const auto it = Items.begin() + ItemsProcessed;
+        found |= MapInfo::isEqual(&*it, &Value);
+        FragileLazySeenCache.insert(&*it);
+        ++ItemsProcessed;
+      }
+      return found;
     }
     void push_back(const DynTypedNode &Value) {
-      if (!Value.getMemoizationData() || Seen.insert(Value).second)
+      if (!Value.getMemoizationData() || !contains(Value)) {
+        const size_t OldCapacity = Items.capacity();
         Items.push_back(Value);
+        if (OldCapacity != Items.capacity()) {
+          // Pointers are invalidated; remove them.
+          ItemsProcessed = 0;
+          // Free memory to avoid doubling peak memory usage during rehashing
----------------
higher-performance wrote:

Fixed!

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


More information about the cfe-commits mailing list