[clang] [analyzer][NFC] Drop redundant reserve before append_range (PR #246889)

Balázs Benics via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 28 12:48:48 PST 2026


https://github.com/steakhal created https://github.com/llvm/llvm-project/pull/246889

`llvm::append_range(C, R)` expands to `C.insert(C.end(), begin(R), end(R))`. For `std::vector`, insert with forward-or-better iterators computes the distance up front and performs a single allocation of the exact size, so a preceding `reserve(std::distance(...))` of the same range is redundant (and only adds an extra `O(n)` traversal for non-random-access ranges).

The two sites in `RegionStore.cpp` append `map_range()` over an `ImmutableMap`. `ImmutableMap` iterators are bidirectional and `mapped_iterator` preserves the underlying iterator category, so the insert already reserves. No other `append_range` call in the analyzer has a reserve at the same site.

I didn't measure the perf impact of this, but I'm 100% it doesn't change anything.

Assisted-by: Claude Code

>From 5f039631cf92da69026dad68a512207208ffb962 Mon Sep 17 00:00:00 2001
From: Balazs Benics <b_benics at apple.com>
Date: Sat, 28 Nov 2026 19:45:49 +0000
Subject: [PATCH] [analyzer][NFC] Drop redundant reserve before append_range

llvm::append_range(C, R) expands to C.insert(C.end(), begin(R), end(R)).
For std::vector, insert with forward-or-better iterators computes the
distance up front and performs a single allocation of the exact size, so
a preceding reserve(std::distance(...)) of the same range is redundant (and
only adds an extra O(n) traversal for non-random-access ranges).

The two sites in RegionStore.cpp append map_range() over an
ImmutableMap. ImmutableMap iterators are bidirectional and mapped_iterator
preserves the underlying iterator category, so the insert already reserves.
No other append_range call in the analyzer has a reserve at the same site.

Assisted-by: Claude Code
---
 clang/lib/StaticAnalyzer/Core/RegionStore.cpp | 2 --
 1 file changed, 2 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Core/RegionStore.cpp b/clang/lib/StaticAnalyzer/Core/RegionStore.cpp
index a93f5af26244b1..ea8e429551dd95 100644
--- a/clang/lib/StaticAnalyzer/Core/RegionStore.cpp
+++ b/clang/lib/StaticAnalyzer/Core/RegionStore.cpp
@@ -293,7 +293,6 @@ class RegionBindingsRef : public llvm::ImmutableMapRef<const MemRegion *,
     const auto AddrOf = [](const auto &Item) { return &Item; };
 
     std::vector<const Cluster *> SortedClusters;
-    SortedClusters.reserve(std::distance(begin(), end()));
     append_range(SortedClusters, map_range(*this, AddrOf));
     llvm::sort(SortedClusters, MemSpaceBeforeRegionName);
 
@@ -304,7 +303,6 @@ class RegionBindingsRef : public llvm::ImmutableMapRef<const MemRegion *,
           << (const void *)BaseRegion << "\", \"items\": [" << NL;
 
       std::vector<const Binding *> SortedBindings;
-      SortedBindings.reserve(std::distance(Bindings.begin(), Bindings.end()));
       append_range(SortedBindings, map_range(Bindings, AddrOf));
       llvm::sort(SortedBindings, DefaultBindingBeforeDirectBindings);
 



More information about the cfe-commits mailing list