@@ -58,12 +58,11 @@ class ThreadLocalCache {
58
58
// / ValueT. We use a weak reference here so that the object can be destroyed
59
59
// / without needing to lock access to the cache itself.
60
60
struct CacheType
61
- : public llvm::SmallDenseMap<PerInstanceState *,
62
- std::pair<std::weak_ptr<ValueT>, ValueT *>> {
61
+ : public llvm::SmallDenseMap<PerInstanceState *, std::weak_ptr<ValueT>> {
63
62
~CacheType () {
64
63
// Remove the values of this cache that haven't already expired.
65
64
for (auto &it : *this )
66
- if (std::shared_ptr<ValueT> value = it.second .first . lock ())
65
+ if (std::shared_ptr<ValueT> value = it.second .lock ())
67
66
it.first ->remove (value.get ());
68
67
}
69
68
@@ -72,7 +71,7 @@ class ThreadLocalCache {
72
71
void clearExpiredEntries () {
73
72
for (auto it = this ->begin (), e = this ->end (); it != e;) {
74
73
auto curIt = it++;
75
- if (curIt->second .first . expired ())
74
+ if (curIt->second .expired ())
76
75
this ->erase (curIt);
77
76
}
78
77
}
@@ -89,27 +88,22 @@ class ThreadLocalCache {
89
88
ValueT &get () {
90
89
// Check for an already existing instance for this thread.
91
90
CacheType &staticCache = getStaticCache ();
92
- std::pair<std::weak_ptr<ValueT>, ValueT *> &threadInstance =
93
- staticCache[perInstanceState.get ()];
94
- if (ValueT *value = threadInstance.second )
91
+ std::weak_ptr<ValueT> &threadInstance = staticCache[perInstanceState.get ()];
92
+ if (std::shared_ptr<ValueT> value = threadInstance.lock ())
95
93
return *value;
96
94
97
95
// Otherwise, create a new instance for this thread.
98
- {
99
- llvm::sys::SmartScopedLock<true > threadInstanceLock (
100
- perInstanceState->instanceMutex );
101
- threadInstance.second =
102
- perInstanceState->instances .emplace_back (std::make_unique<ValueT>())
103
- .get ();
104
- }
105
- threadInstance.first =
106
- std::shared_ptr<ValueT>(perInstanceState, threadInstance.second );
96
+ llvm::sys::SmartScopedLock<true > threadInstanceLock (
97
+ perInstanceState->instanceMutex );
98
+ perInstanceState->instances .push_back (std::make_unique<ValueT>());
99
+ ValueT *instance = perInstanceState->instances .back ().get ();
100
+ threadInstance = std::shared_ptr<ValueT>(perInstanceState, instance);
107
101
108
102
// Before returning the new instance, take the chance to clear out any used
109
103
// entries in the static map. The cache is only cleared within the same
110
104
// thread to remove the need to lock the cache itself.
111
105
staticCache.clearExpiredEntries ();
112
- return *threadInstance. second ;
106
+ return *instance ;
113
107
}
114
108
ValueT &operator *() { return get (); }
115
109
ValueT *operator ->() { return &get (); }
0 commit comments