[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
Wed Mar 12 19:03:47 PDT 2025


higher-performance wrote:

So, this is going to sound very silly, but it seems I may have missed the obvious, and the solution may have been extremely simple: `llvm::SmallPtrSet`, on the return value of `clang::DynTypedNode::getMemoizationData()`.

The reason is that the original code only ever inserts into the vector whenever `getMemoizationData()` returned non-NULL:

https://github.com/llvm/llvm-project/blob/a3c248db87ebe88084386950846678c9a52dd7c0/clang/lib/AST/ParentMapContext.cpp#L388-L391

Specifically, `llvm::is_contained(*Vector, node)` (and thus `Vector->contains(node)`) is only ever called when `node.getMemoizationData()` returns non-NULL. There was no containment check in the case where memoization data was absent, so we can just use pointer identity on `getMemoizationData()`.

This drastically simplifies everything - please take another look and let me know if you see any issues!

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


More information about the cfe-commits mailing list