-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[ADT] Use range-based for loops in SmallPtrSet (NFC) #152882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ADT] Use range-based for loops in SmallPtrSet (NFC) #152882
Conversation
This patch introduces helper function buckets() to convert two loops to range-based for loops.
@llvm/pr-subscribers-llvm-support Author: Kazu Hirata (kazutakahirata) ChangesThis patch introduces helper function buckets() to convert two loops Full diff: https://github.com/llvm/llvm-project/pull/152882.diff 2 Files Affected:
diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index 88aedf26dcaac..f0f28ac37761e 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -156,6 +156,10 @@ class SmallPtrSetImplBase : public DebugEpochBase {
return {CurArray, CurArray + NumNonEmpty};
}
+ iterator_range<const void **> buckets() {
+ return make_range(CurArray, EndPointer());
+ }
+
/// insert_imp - This returns true if the pointer was new to the set, false if
/// it was already in the set. This is hidden from the client so that the
/// derived class can check that the right type of pointer is passed in.
@@ -441,13 +445,12 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
return Removed;
}
- for (const void **APtr = CurArray, **E = EndPointer(); APtr != E; ++APtr) {
- const void *Value = *APtr;
- if (Value == getTombstoneMarker() || Value == getEmptyMarker())
+ for (const void *&Bucket : buckets()) {
+ if (Bucket == getTombstoneMarker() || Bucket == getEmptyMarker())
continue;
- PtrType Ptr = PtrTraits::getFromVoidPointer(const_cast<void *>(Value));
+ PtrType Ptr = PtrTraits::getFromVoidPointer(const_cast<void *>(Bucket));
if (P(Ptr)) {
- *APtr = getTombstoneMarker();
+ Bucket = getTombstoneMarker();
++NumTombstones;
incrementEpoch();
Removed = true;
diff --git a/llvm/lib/Support/SmallPtrSet.cpp b/llvm/lib/Support/SmallPtrSet.cpp
index 83143a7fe80fa..0c226970906d9 100644
--- a/llvm/lib/Support/SmallPtrSet.cpp
+++ b/llvm/lib/Support/SmallPtrSet.cpp
@@ -110,8 +110,7 @@ const void *const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
/// Grow - Allocate a larger backing store for the buckets and move it over.
///
void SmallPtrSetImplBase::Grow(unsigned NewSize) {
- const void **OldBuckets = CurArray;
- const void **OldEnd = EndPointer();
+ auto OldBuckets = buckets();
bool WasSmall = isSmall();
// Install the new array. Clear all the buckets to empty.
@@ -123,15 +122,14 @@ void SmallPtrSetImplBase::Grow(unsigned NewSize) {
memset(CurArray, -1, NewSize*sizeof(void*));
// Copy over all valid entries.
- for (const void **BucketPtr = OldBuckets; BucketPtr != OldEnd; ++BucketPtr) {
+ for (const void *&Bucket : OldBuckets) {
// Copy over the element if it is valid.
- const void *Elt = *BucketPtr;
- if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
- *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
+ if (Bucket != getTombstoneMarker() && Bucket != getEmptyMarker())
+ *const_cast<void **>(FindBucketFor(Bucket)) = const_cast<void *>(Bucket);
}
if (!WasSmall)
- free(OldBuckets);
+ free(OldBuckets.begin());
NumNonEmpty -= NumTombstones;
NumTombstones = 0;
IsSmall = false;
|
@llvm/pr-subscribers-llvm-adt Author: Kazu Hirata (kazutakahirata) ChangesThis patch introduces helper function buckets() to convert two loops Full diff: https://github.com/llvm/llvm-project/pull/152882.diff 2 Files Affected:
diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h
index 88aedf26dcaac..f0f28ac37761e 100644
--- a/llvm/include/llvm/ADT/SmallPtrSet.h
+++ b/llvm/include/llvm/ADT/SmallPtrSet.h
@@ -156,6 +156,10 @@ class SmallPtrSetImplBase : public DebugEpochBase {
return {CurArray, CurArray + NumNonEmpty};
}
+ iterator_range<const void **> buckets() {
+ return make_range(CurArray, EndPointer());
+ }
+
/// insert_imp - This returns true if the pointer was new to the set, false if
/// it was already in the set. This is hidden from the client so that the
/// derived class can check that the right type of pointer is passed in.
@@ -441,13 +445,12 @@ class SmallPtrSetImpl : public SmallPtrSetImplBase {
return Removed;
}
- for (const void **APtr = CurArray, **E = EndPointer(); APtr != E; ++APtr) {
- const void *Value = *APtr;
- if (Value == getTombstoneMarker() || Value == getEmptyMarker())
+ for (const void *&Bucket : buckets()) {
+ if (Bucket == getTombstoneMarker() || Bucket == getEmptyMarker())
continue;
- PtrType Ptr = PtrTraits::getFromVoidPointer(const_cast<void *>(Value));
+ PtrType Ptr = PtrTraits::getFromVoidPointer(const_cast<void *>(Bucket));
if (P(Ptr)) {
- *APtr = getTombstoneMarker();
+ Bucket = getTombstoneMarker();
++NumTombstones;
incrementEpoch();
Removed = true;
diff --git a/llvm/lib/Support/SmallPtrSet.cpp b/llvm/lib/Support/SmallPtrSet.cpp
index 83143a7fe80fa..0c226970906d9 100644
--- a/llvm/lib/Support/SmallPtrSet.cpp
+++ b/llvm/lib/Support/SmallPtrSet.cpp
@@ -110,8 +110,7 @@ const void *const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const {
/// Grow - Allocate a larger backing store for the buckets and move it over.
///
void SmallPtrSetImplBase::Grow(unsigned NewSize) {
- const void **OldBuckets = CurArray;
- const void **OldEnd = EndPointer();
+ auto OldBuckets = buckets();
bool WasSmall = isSmall();
// Install the new array. Clear all the buckets to empty.
@@ -123,15 +122,14 @@ void SmallPtrSetImplBase::Grow(unsigned NewSize) {
memset(CurArray, -1, NewSize*sizeof(void*));
// Copy over all valid entries.
- for (const void **BucketPtr = OldBuckets; BucketPtr != OldEnd; ++BucketPtr) {
+ for (const void *&Bucket : OldBuckets) {
// Copy over the element if it is valid.
- const void *Elt = *BucketPtr;
- if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
- *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
+ if (Bucket != getTombstoneMarker() && Bucket != getEmptyMarker())
+ *const_cast<void **>(FindBucketFor(Bucket)) = const_cast<void *>(Bucket);
}
if (!WasSmall)
- free(OldBuckets);
+ free(OldBuckets.begin());
NumNonEmpty -= NumTombstones;
NumTombstones = 0;
IsSmall = false;
|
This patch introduces helper function buckets() to convert two loops
to range-based for loops.