[clang] [NFC][clang][AST] Add compile-time dispatch for random access iterators in append() (PR #162000)
Samarth Narang via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 23 11:55:51 PDT 2025
https://github.com/snarang181 updated https://github.com/llvm/llvm-project/pull/162000
>From 5a7b222f638fc2bd88d014eb90ec9d5de0678c7f Mon Sep 17 00:00:00 2001
From: Samarth Narang <snarang at umass.edu>
Date: Sun, 5 Oct 2025 12:20:36 +0530
Subject: [PATCH 1/2] Compile-time dispatch for ASTVector operations.
---
clang/include/clang/AST/ASTVector.h | 24 +++++++++++++++++++-----
1 file changed, 19 insertions(+), 5 deletions(-)
diff --git a/clang/include/clang/AST/ASTVector.h b/clang/include/clang/AST/ASTVector.h
index d5a04767ca133..ca30ec343492a 100644
--- a/clang/include/clang/AST/ASTVector.h
+++ b/clang/include/clang/AST/ASTVector.h
@@ -180,9 +180,21 @@ class ASTVector {
size_t capacity() const { return this->capacity_ptr() - Begin; }
/// append - Add the specified range to the end of the SmallVector.
- template<typename in_iter>
+ template <typename in_iter>
void append(const ASTContext &C, in_iter in_start, in_iter in_end) {
- size_type NumInputs = std::distance(in_start, in_end);
+ using size_type =
+ typename std::remove_reference_t<decltype(*this)>::size_type;
+ using iterator_category =
+ typename std::iterator_traits<in_iter>::iterator_category;
+
+ size_t NumInputs = 0;
+ constexpr bool is_random_access =
+ std::is_base_of_v<std::random_access_iterator_tag, iterator_category>;
+
+ if constexpr (is_random_access)
+ NumInputs = static_cast<size_type>(in_end - in_start);
+ else
+ NumInputs = static_cast<size_type>(std::distance(in_start, in_end));
if (NumInputs == 0)
return;
@@ -192,9 +204,11 @@ class ASTVector {
this->grow(C, this->size()+NumInputs);
// Copy the new elements over.
- // TODO: NEED To compile time dispatch on whether in_iter is a random access
- // iterator to use the fast uninitialized_copy.
- std::uninitialized_copy(in_start, in_end, this->end());
+ if constexpr (is_random_access)
+ std::uninitialized_copy_n(in_start, NumInputs, this->end());
+ else
+ std::uninitialized_copy(in_start, in_end, this->end());
+
this->setEnd(this->end() + NumInputs);
}
>From ac3615e4541c37be768c2237e46124d34c5f18d3 Mon Sep 17 00:00:00 2001
From: Samarth Narang <snarang at umass.edu>
Date: Thu, 23 Oct 2025 13:51:17 -0400
Subject: [PATCH 2/2] Do not need another size_type definition in ASTVector.h
---
clang/include/clang/AST/ASTVector.h | 2 --
1 file changed, 2 deletions(-)
diff --git a/clang/include/clang/AST/ASTVector.h b/clang/include/clang/AST/ASTVector.h
index ca30ec343492a..78e4432723481 100644
--- a/clang/include/clang/AST/ASTVector.h
+++ b/clang/include/clang/AST/ASTVector.h
@@ -182,8 +182,6 @@ class ASTVector {
/// append - Add the specified range to the end of the SmallVector.
template <typename in_iter>
void append(const ASTContext &C, in_iter in_start, in_iter in_end) {
- using size_type =
- typename std::remove_reference_t<decltype(*this)>::size_type;
using iterator_category =
typename std::iterator_traits<in_iter>::iterator_category;
More information about the cfe-commits
mailing list