Skip to content

Commit efaa6f5

Browse files
committed
[ADT] Avoid creating iterators in DenseMap::operator[] (NFC)
An attempt to resolve the slowdown from #155204
1 parent dff8788 commit efaa6f5

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

llvm/include/llvm/ADT/DenseMap.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,11 @@ class DenseMapBase : public DebugEpochBase {
348348
}
349349

350350
ValueT &operator[](const KeyT &Key) {
351-
return try_emplace_impl(Key).first->second;
351+
return lookupOrInsertIntoBucket(Key).first->second;
352352
}
353353

354354
ValueT &operator[](KeyT &&Key) {
355-
return try_emplace_impl(std::move(Key)).first->second;
355+
return lookupOrInsertIntoBucket(std::move(Key)).first->second;
356356
}
357357

358358
/// isPointerIntoBucketsArray - Return true if the specified pointer points
@@ -477,16 +477,24 @@ class DenseMapBase : public DebugEpochBase {
477477

478478
private:
479479
template <typename KeyArgT, typename... Ts>
480-
std::pair<iterator, bool> try_emplace_impl(KeyArgT &&Key, Ts &&...Args) {
480+
std::pair<BucketT *, bool> lookupOrInsertIntoBucket(KeyArgT &&Key,
481+
Ts &&...Args) {
481482
BucketT *TheBucket = nullptr;
482483
if (LookupBucketFor(Key, TheBucket))
483-
return {makeInsertIterator(TheBucket), false}; // Already in the map.
484+
return {TheBucket, false}; // Already in the map.
484485

485486
// Otherwise, insert the new element.
486487
TheBucket = findBucketForInsertion(Key, TheBucket);
487488
TheBucket->getFirst() = std::forward<KeyArgT>(Key);
488489
::new (&TheBucket->getSecond()) ValueT(std::forward<Ts>(Args)...);
489-
return {makeInsertIterator(TheBucket), true};
490+
return {TheBucket, true};
491+
}
492+
493+
template <typename KeyArgT, typename... Ts>
494+
std::pair<iterator, bool> try_emplace_impl(KeyArgT &&Key, Ts &&...Args) {
495+
auto [Bucket, Inserted] = lookupOrInsertIntoBucket(
496+
std::forward<KeyArgT>(Key), std::forward<Ts>(Args)...);
497+
return {makeInsertIterator(Bucket), Inserted};
490498
}
491499

492500
iterator makeIterator(BucketT *P, BucketT *E, DebugEpochBase &Epoch,

0 commit comments

Comments
 (0)