mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-06 00:55:50 +00:00
3affe9913f
Use LRU cache for ESMReaders. When cache capacity is reached close least recently used ESMReader. Remember the file name if a reader was open. Once the reader requested again open the file if there is stored name for it. Put released ESMReader to the back of the free items list. Close ESMReader's from the front of the free items list. Cached item can be used only by one client at the same time. If the same item is requested twice exception is thrown. This should never happen in practice. If this happens need to fix the client logic. It's allowed to go over the capacity limit when requesting different readers. Ideally this should never happen but there will be system error anyway signalizing about too many open files. Need to fix client logic in this case. All places that were using a vector of ESMReaders now using the cache. Cache is local for each use case and there is no need for a thread safety.
88 lines
2.5 KiB
C++
88 lines
2.5 KiB
C++
#include "readerscache.hpp"
|
|
|
|
#include <stdexcept>
|
|
|
|
namespace ESM
|
|
{
|
|
ReadersCache::BusyItem::BusyItem(ReadersCache& owner, std::list<Item>::iterator item) noexcept
|
|
: mOwner(owner)
|
|
, mItem(item)
|
|
{}
|
|
|
|
ReadersCache::BusyItem::~BusyItem() noexcept
|
|
{
|
|
mOwner.releaseItem(mItem);
|
|
}
|
|
|
|
ReadersCache::ReadersCache(std::size_t capacity)
|
|
: mCapacity(capacity)
|
|
{}
|
|
|
|
ReadersCache::BusyItem ReadersCache::get(std::size_t index)
|
|
{
|
|
const auto indexIt = mIndex.find(index);
|
|
std::list<Item>::iterator it;
|
|
if (indexIt == mIndex.end())
|
|
{
|
|
closeExtraReaders();
|
|
it = mBusyItems.emplace(mBusyItems.end());
|
|
mIndex.emplace(index, it);
|
|
}
|
|
else
|
|
{
|
|
switch (indexIt->second->mState)
|
|
{
|
|
case State::Busy:
|
|
throw std::logic_error("ESMReader at index " + std::to_string(index) + " is busy");
|
|
case State::Free:
|
|
it = indexIt->second;
|
|
mBusyItems.splice(mBusyItems.end(), mFreeItems, it);
|
|
break;
|
|
case State::Closed:
|
|
closeExtraReaders();
|
|
it = indexIt->second;
|
|
if (it->mName.has_value())
|
|
{
|
|
it->mReader.open(*it->mName);
|
|
it->mName.reset();
|
|
}
|
|
mBusyItems.splice(mBusyItems.end(), mClosedItems, it);
|
|
break;
|
|
}
|
|
it->mState = State::Busy;
|
|
}
|
|
|
|
return BusyItem(*this, it);
|
|
}
|
|
|
|
void ReadersCache::closeExtraReaders()
|
|
{
|
|
while (!mFreeItems.empty() && mBusyItems.size() + mFreeItems.size() + 1 > mCapacity)
|
|
{
|
|
const auto it = mFreeItems.begin();
|
|
if (it->mReader.isOpen())
|
|
{
|
|
it->mName = it->mReader.getName();
|
|
it->mReader.close();
|
|
}
|
|
mClosedItems.splice(mClosedItems.end(), mFreeItems, it);
|
|
it->mState = State::Closed;
|
|
}
|
|
}
|
|
|
|
void ReadersCache::releaseItem(std::list<Item>::iterator it) noexcept
|
|
{
|
|
assert(it->mState == State::Busy);
|
|
if (it->mReader.isOpen())
|
|
{
|
|
mFreeItems.splice(mFreeItems.end(), mBusyItems, it);
|
|
it->mState = State::Free;
|
|
}
|
|
else
|
|
{
|
|
mClosedItems.splice(mClosedItems.end(), mBusyItems, it);
|
|
it->mState = State::Closed;
|
|
}
|
|
}
|
|
}
|