[llvm] [llvm] Use llvm::append_range (NFC) (PR #136066)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 18 01:17:55 PDT 2025


nikic wrote:

Unfortunately, restoring the reserve() actually made things worse, not better: https://llvm-compile-time-tracker.com/compare.php?from=209d8c8fa4fe16ef41003da17387f7c271002668&to=69b9ddc76418c6f60ce7751efb5beb1f3b3be3ff&stat=instructions:u

I just looked at what SmallVector::insert() on iterators does, and I found this gem: https://github.com/llvm/llvm-project/blob/a99c978d1b35e30d9a0fe9db68b91e9f2815c8e9/llvm/include/llvm/ADT/SmallVector.h#L889

So we're calling `std::distance()`, but only requiring an input iterator. If it's not a random access iterator, insert() is going to walk the whole iterator twice, which is very expensive for iterators like PostOrderIterator.

This seems like a pretty big footgun for the use of insert() and append_range(). I think the current insert() implementation should only be used for random access iterators, and there should be a different one for input iterators that only iterates once.

(As a side note, does C++ really require the full random access kitchen sink when `operator-` is all that is actually needed?)

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


More information about the llvm-commits mailing list