Cache absent land object

This allows to save on lookup in store.
macos_ci_fix
elsid 1 year ago
parent eba553821b
commit 816d3772b9
No known key found for this signature in database
GPG Key ID: 4DE04C198CBA7625

@ -19,9 +19,8 @@ namespace MWRender
osg::ref_ptr<ESMTerrain::LandObject> LandManager::getLand(ESM::ExteriorCellLocation cellIndex)
{
const osg::ref_ptr<osg::Object> obj = mCache->getRefFromObjectCache(cellIndex);
if (obj != nullptr)
return static_cast<ESMTerrain::LandObject*>(obj.get());
if (const std::optional<osg::ref_ptr<osg::Object>> obj = mCache->getRefFromObjectCacheOrNone(cellIndex))
return static_cast<ESMTerrain::LandObject*>(obj->get());
const MWBase::World& world = *MWBase::Environment::get().getWorld();
osg::ref_ptr<ESMTerrain::LandObject> landObj = nullptr;
@ -29,16 +28,14 @@ namespace MWRender
if (ESM::isEsm4Ext(cellIndex.mWorldspace))
{
const ESM4::Land* land = world.getStore().get<ESM4::Land>().search(cellIndex);
if (land == nullptr)
return nullptr;
landObj = new ESMTerrain::LandObject(*land, mLoadFlags);
if (land != nullptr)
landObj = new ESMTerrain::LandObject(*land, mLoadFlags);
}
else
{
const ESM::Land* land = world.getStore().get<ESM::Land>().search(cellIndex.mX, cellIndex.mY);
if (land == nullptr)
return nullptr;
landObj = new ESMTerrain::LandObject(*land, mLoadFlags);
if (land != nullptr)
landObj = new ESMTerrain::LandObject(*land, mLoadFlags);
}
mCache->addEntryToObjectCache(cellIndex, landObj.get());

@ -26,6 +26,7 @@
#include <map>
#include <mutex>
#include <optional>
#include <string>
namespace osg
@ -82,7 +83,8 @@ namespace Resource
{
if (oitr->second.second <= expiryTime)
{
objectsToRemove.push_back(oitr->second.first);
if (oitr->second.first != nullptr)
objectsToRemove.push_back(oitr->second.first);
_objectCache.erase(oitr++);
}
else
@ -127,6 +129,15 @@ namespace Resource
return nullptr;
}
std::optional<osg::ref_ptr<osg::Object>> getRefFromObjectCacheOrNone(const KeyType& key)
{
const std::lock_guard<std::mutex> lock(_objectCacheMutex);
const auto it = _objectCache.find(key);
if (it == _objectCache.end())
return std::nullopt;
return it->second.first;
}
/** Check if an object is in the cache, and if it is, update its usage time stamp. */
bool checkInObjectCache(const KeyType& key, double timeStamp)
{

Loading…
Cancel
Save