Commit d53eecae by Nicolas Capens Committed by Nicolas Capens

Rename constant cache to snapshot cache

This helps clarify that the cache is able to take snapshots of its contents, and this snapshot can be queried without the overhead of acquiring a mutex. Bug: b/131246679 Change-Id: I4317628a22f70d37309908b24df62dc5ec5a946c Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/42568 Presubmit-Ready: Nicolas Capens <nicolascapens@google.com> Kokoro-Presubmit: kokoro <noreply+kokoro@google.com> Tested-by: 's avatarNicolas Capens <nicolascapens@google.com> Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com>
parent 28f4ec36
...@@ -47,30 +47,32 @@ protected: ...@@ -47,30 +47,32 @@ protected:
Data *data; Data *data;
}; };
// An LRU cache which can take a 'snapshot' of its current state. This is useful
// for allowing concurrent read access without requiring a mutex to be locked.
template<class Key, class Data, class Hasher = std::hash<Key>> template<class Key, class Data, class Hasher = std::hash<Key>>
class LRUConstCache : public LRUCache<Key, Data> class LRUSnapshotCache : public LRUCache<Key, Data>
{ {
using LRUBase = LRUCache<Key, Data>; using LRUBase = LRUCache<Key, Data>;
public: public:
LRUConstCache(int n) LRUSnapshotCache(int n)
: LRUBase(n) : LRUBase(n)
{} {}
~LRUConstCache() { clearConstCache(); } ~LRUSnapshotCache() { clearSnapshot(); }
Data add(const Key &key, const Data &data) override Data add(const Key &key, const Data &data) override
{ {
constCacheNeedsUpdate = true; snapshotNeedsUpdate = true;
return LRUBase::add(key, data); return LRUBase::add(key, data);
} }
void updateConstCache(); void updateSnapshot();
const Data &queryConstCache(const Key &key) const; const Data &querySnapshot(const Key &key) const;
private: private:
void clearConstCache(); void clearSnapshot();
bool constCacheNeedsUpdate = false; bool snapshotNeedsUpdate = false;
std::unordered_map<Key, Data, Hasher> constCache; std::unordered_map<Key, Data, Hasher> snapshot;
}; };
} // namespace sw } // namespace sw
...@@ -153,36 +155,36 @@ Data LRUCache<Key, Data>::add(const Key &key, const Data &data) ...@@ -153,36 +155,36 @@ Data LRUCache<Key, Data>::add(const Key &key, const Data &data)
} }
template<class Key, class Data, class Hasher> template<class Key, class Data, class Hasher>
void LRUConstCache<Key, Data, Hasher>::clearConstCache() void LRUSnapshotCache<Key, Data, Hasher>::clearSnapshot()
{ {
constCache.clear(); snapshot.clear();
} }
template<class Key, class Data, class Hasher> template<class Key, class Data, class Hasher>
void LRUConstCache<Key, Data, Hasher>::updateConstCache() void LRUSnapshotCache<Key, Data, Hasher>::updateSnapshot()
{ {
if(constCacheNeedsUpdate) if(snapshotNeedsUpdate)
{ {
clearConstCache(); clearSnapshot();
for(int i = 0; i < LRUBase::size; i++) for(int i = 0; i < LRUBase::size; i++)
{ {
if(LRUBase::data[i]) if(LRUBase::data[i])
{ {
constCache[*LRUBase::ref[i]] = LRUBase::data[i]; snapshot[*LRUBase::ref[i]] = LRUBase::data[i];
} }
} }
constCacheNeedsUpdate = false; snapshotNeedsUpdate = false;
} }
} }
template<class Key, class Data, class Hasher> template<class Key, class Data, class Hasher>
const Data &LRUConstCache<Key, Data, Hasher>::queryConstCache(const Key &key) const const Data &LRUSnapshotCache<Key, Data, Hasher>::querySnapshot(const Key &key) const
{ {
auto it = constCache.find(key); auto it = snapshot.find(key);
static Data null = {}; static Data null = {};
return (it != constCache.end()) ? it->second : null; return (it != snapshot.end()) ? it->second : null;
} }
} // namespace sw } // namespace sw
......
...@@ -555,7 +555,7 @@ void Renderer::synchronize() ...@@ -555,7 +555,7 @@ void Renderer::synchronize()
MARL_SCOPED_EVENT("synchronize"); MARL_SCOPED_EVENT("synchronize");
auto ticket = drawTickets.take(); auto ticket = drawTickets.take();
ticket.wait(); ticket.wait();
device->updateSamplingRoutineConstCache(); device->updateSamplingRoutineSnapshotCache();
ticket.done(); ticket.done();
} }
......
...@@ -40,7 +40,7 @@ SpirvShader::ImageSampler *SpirvShader::getImageSampler(uint32_t inst, vk::Sampl ...@@ -40,7 +40,7 @@ SpirvShader::ImageSampler *SpirvShader::getImageSampler(uint32_t inst, vk::Sampl
ASSERT(imageDescriptor->device); ASSERT(imageDescriptor->device);
if(auto routine = imageDescriptor->device->findInConstCache(key)) if(auto routine = imageDescriptor->device->querySnapshotCache(key))
{ {
return (ImageSampler *)(routine->getEntry()); return (ImageSampler *)(routine->getEntry());
} }
......
...@@ -49,14 +49,14 @@ void Device::SamplingRoutineCache::add(const vk::Device::SamplingRoutineCache::K ...@@ -49,14 +49,14 @@ void Device::SamplingRoutineCache::add(const vk::Device::SamplingRoutineCache::K
cache.add(key, routine); cache.add(key, routine);
} }
rr::Routine *Device::SamplingRoutineCache::queryConst(const vk::Device::SamplingRoutineCache::Key &key) const rr::Routine *Device::SamplingRoutineCache::querySnapshot(const vk::Device::SamplingRoutineCache::Key &key) const
{ {
return cache.queryConstCache(key).get(); return cache.querySnapshot(key).get();
} }
void Device::SamplingRoutineCache::updateConstCache() void Device::SamplingRoutineCache::updateSnapshot()
{ {
cache.updateConstCache(); cache.updateSnapshot();
} }
Device::SamplerIndexer::~SamplerIndexer() Device::SamplerIndexer::~SamplerIndexer()
...@@ -302,15 +302,15 @@ Device::SamplingRoutineCache *Device::getSamplingRoutineCache() const ...@@ -302,15 +302,15 @@ Device::SamplingRoutineCache *Device::getSamplingRoutineCache() const
return samplingRoutineCache.get(); return samplingRoutineCache.get();
} }
rr::Routine *Device::findInConstCache(const SamplingRoutineCache::Key &key) const rr::Routine *Device::querySnapshotCache(const SamplingRoutineCache::Key &key) const
{ {
return samplingRoutineCache->queryConst(key); return samplingRoutineCache->querySnapshot(key);
} }
void Device::updateSamplingRoutineConstCache() void Device::updateSamplingRoutineSnapshotCache()
{ {
std::unique_lock<std::mutex> lock(samplingRoutineCacheMutex); std::unique_lock<std::mutex> lock(samplingRoutineCacheMutex);
samplingRoutineCache->updateConstCache(); samplingRoutineCache->updateSnapshot();
} }
std::mutex &Device::getSamplingRoutineCacheMutex() std::mutex &Device::getSamplingRoutineCacheMutex()
......
...@@ -89,17 +89,17 @@ public: ...@@ -89,17 +89,17 @@ public:
std::shared_ptr<rr::Routine> query(const Key &key) const; std::shared_ptr<rr::Routine> query(const Key &key) const;
void add(const Key &key, const std::shared_ptr<rr::Routine> &routine); void add(const Key &key, const std::shared_ptr<rr::Routine> &routine);
rr::Routine *queryConst(const Key &key) const; rr::Routine *querySnapshot(const Key &key) const;
void updateConstCache(); void updateSnapshot();
private: private:
sw::LRUConstCache<Key, std::shared_ptr<rr::Routine>, Key::Hash> cache; sw::LRUSnapshotCache<Key, std::shared_ptr<rr::Routine>, Key::Hash> cache;
}; };
SamplingRoutineCache *getSamplingRoutineCache() const; SamplingRoutineCache *getSamplingRoutineCache() const;
std::mutex &getSamplingRoutineCacheMutex(); std::mutex &getSamplingRoutineCacheMutex();
rr::Routine *findInConstCache(const SamplingRoutineCache::Key &key) const; rr::Routine *querySnapshotCache(const SamplingRoutineCache::Key &key) const;
void updateSamplingRoutineConstCache(); void updateSamplingRoutineSnapshotCache();
class SamplerIndexer class SamplerIndexer
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment