mirror of
https://github.com/Zygo/bees.git
synced 2025-05-17 21:35:45 +02:00
crucible: resource: optimize map cleanup
We were holding weak refs until the next time the resource ID was used. This is a bad thing if resource IDs are sparse (e.g. pointers or hashes) because we'll never see an ID twice. To fix, determine whether we released the last instance of a resource, and if so, free its weak ref immediately. Signed-off-by: Zygo Blaxell <bees@furryterror.org>
This commit is contained in:
parent
35100c2b9e
commit
c477618924
@ -273,12 +273,19 @@ namespace crucible {
|
|||||||
}
|
}
|
||||||
// Save key so we can clean the map
|
// Save key so we can clean the map
|
||||||
auto key = s_traits.get_key(*m_ptr);
|
auto key = s_traits.get_key(*m_ptr);
|
||||||
// Drop pointer early
|
// Save a weak_ptr so we can tell if we need to clean the map
|
||||||
|
weak_ptr_type wp = m_ptr;
|
||||||
|
// Drop shared_ptr
|
||||||
m_ptr.reset();
|
m_ptr.reset();
|
||||||
|
// If there are still other references to the shared_ptr, we can stop now
|
||||||
|
if (!wp.expired()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
// Remove weak_ptr from map if it has expired
|
// Remove weak_ptr from map if it has expired
|
||||||
// (and not been replaced in the meantime)
|
// (and not been replaced in the meantime)
|
||||||
unique_lock<mutex> lock_map(s_map_mutex);
|
unique_lock<mutex> lock_map(s_map_mutex);
|
||||||
auto found = s_map.find(key);
|
auto found = s_map.find(key);
|
||||||
|
// Map entry may have been replaced, so check for expiry again
|
||||||
if (found != s_map.end() && found->second.expired()) {
|
if (found != s_map.end() && found->second.expired()) {
|
||||||
s_map.erase(key);
|
s_map.erase(key);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user