[llvm] [ADT] Only call reserve on empty containers in append_values (PR #172109)

Jakub Kuderski via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 13 10:47:07 PST 2025


================
@@ -2152,7 +2152,17 @@ void append_range(Container &C, Range &&R) {
 /// Appends all `Values` to container `C`.
 template <typename Container, typename... Args>
 void append_values(Container &C, Args &&...Values) {
-  C.reserve(range_size(C) + sizeof...(Args));
+  if (size_t InitialSize = range_size(C); InitialSize == 0) {
----------------
kuhar wrote:

I got back to this line of work recently and think we need some fast size function that allows for both random access iterators and containers with `.size()`. We can probably also drop `range_size` since `std::size` does the same thing.

Here specifically, I'm not aware of any containers that have .insert but don't have (fast) `.size()` (almost every usage is with vector and small vector), so ACK but I'm going to address this separately.

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


More information about the llvm-commits mailing list