factor out SkLRU::insert_or_update()

I keep forgetting that insert() breaks if there's
an existing item with that key, so assert there's
not and factor out a safer insert_or_update().

Change-Id: I5d7e3b163d651442d309b7486f8d21e64497a6a2
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/377537
Reviewed-by: Mike Klein <mtklein@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
This commit is contained in:
Mike Klein 2021-02-28 09:02:26 -06:00 committed by Skia Commit-Bot
parent 56a1f06db2
commit 40da13b777
2 changed files with 12 additions and 6 deletions

View File

@ -56,6 +56,8 @@ public:
}
V* insert(const K& key, V value) {
SkASSERT(!this->find(key));
Entry* entry = new Entry(key, std::move(value));
fMap.set(entry);
fLRU.addToHead(entry);
@ -65,6 +67,15 @@ public:
return &entry->fValue;
}
V* insert_or_update(const K& key, V value) {
if (V* found = this->find(key)) {
*found = std::move(value);
return found;
} else {
return this->insert(key, std::move(value));
}
}
int count() {
return fMap.count();
}

View File

@ -563,12 +563,7 @@ namespace {
if (SkLRUCache<Key, skvm::Program>* cache = try_acquire_program_cache()) {
auto cache_program = [&](skvm::Program&& program, Coverage coverage) {
if (!program.empty()) {
Key key = fKey.withCoverage(coverage);
if (skvm::Program* found = cache->find(key)) {
*found = std::move(program);
} else {
cache->insert(key, std::move(program));
}
cache->insert_or_update(fKey.withCoverage(coverage), std::move(program));
}
};
cache_program(std::move(fBlitH), Coverage::Full);