[clang] Fix quadratic slowdown in AST matcher parent map generation (PR #87824)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 9 07:54:01 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);
+ }
----------------
AaronBallman wrote:
```suggestion
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);
```
Changes to match our coding style, NFC.
Regarding:
```
for (; N > 0; --N)
push_back(Value);
```
should we use `std::fill_n` instead?
https://github.com/llvm/llvm-project/pull/87824
More information about the cfe-commits
mailing list