mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-04-28 21:07:59 +03:00
Merge branch 'document_object_cache' into 'master'
Some checks failed
Build and test / Ubuntu (push) Has been cancelled
Build and test / MacOS (push) Has been cancelled
Build and test / Read .env file and expose it as output (push) Has been cancelled
Build and test / Windows (2019) (push) Has been cancelled
Build and test / Windows (2022) (push) Has been cancelled
Some checks failed
Build and test / Ubuntu (push) Has been cancelled
Build and test / MacOS (push) Has been cancelled
Build and test / Read .env file and expose it as output (push) Has been cancelled
Build and test / Windows (2019) (push) Has been cancelled
Build and test / Windows (2022) (push) Has been cancelled
Draft: Add documentation for update function of Generic Object Cache See merge request OpenMW/openmw!4164
This commit is contained in:
commit
fc449a3d5a
1 changed files with 26 additions and 5 deletions
|
@ -53,28 +53,49 @@ namespace Resource
|
|||
class GenericObjectCache : public osg::Referenced
|
||||
{
|
||||
public:
|
||||
// Update last usage timestamp using referenceTime for each cache time if they are not nullptr and referenced
|
||||
// from somewhere else. Remove items with last usage > expiryTime. Note: last usage might be updated from other
|
||||
// places so nullptr or not references elsewhere items are not always removed.
|
||||
/*
|
||||
* @brief Updates usage timestamps and removes expired items
|
||||
*
|
||||
* Updates the lastUsage timestamp of cached items that have external references.
|
||||
* Initializes lastUsage timestamp for new items.
|
||||
* Removes items that haven't been referenced for longer than expiryDelay.
|
||||
*
|
||||
* @param referenceTime the timestamp indicating when the item was most recently used
|
||||
* @param expiryDelay the delay after which the cache entry for an item expires
|
||||
*/
|
||||
void update(double referenceTime, double expiryDelay)
|
||||
{
|
||||
std::vector<osg::ref_ptr<osg::Object>> objectsToRemove;
|
||||
|
||||
{
|
||||
const double expiryTime = referenceTime - expiryDelay;
|
||||
|
||||
std::lock_guard<std::mutex> lock(mMutex);
|
||||
std::erase_if(mItems, [&](auto& v) {
|
||||
|
||||
std::erase_if(mItems, [&](auto& v)
|
||||
{
|
||||
Item& item = v.second;
|
||||
|
||||
// update last usage timestamp if item is being referenced externally
|
||||
// or initialize if not set
|
||||
if ((item.mValue != nullptr && item.mValue->referenceCount() > 1) || item.mLastUsage == 0)
|
||||
item.mLastUsage = referenceTime;
|
||||
|
||||
// skip items that have been accessed since expiryTime
|
||||
if (item.mLastUsage > expiryTime)
|
||||
return false;
|
||||
|
||||
++mExpired;
|
||||
|
||||
// just mark for removal here so objects can be removed in bulk outside the lock
|
||||
if (item.mValue != nullptr)
|
||||
objectsToRemove.push_back(std::move(item.mValue));
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
// note, actual unref happens outside of the lock
|
||||
|
||||
// remove expired items from cache
|
||||
objectsToRemove.clear();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue