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>
diff --git a/src/core/SkLRUCache.h b/src/core/SkLRUCache.h
index d5be4f3..50429d5 100644
--- a/src/core/SkLRUCache.h
+++ b/src/core/SkLRUCache.h
@@ -56,6 +56,8 @@
}
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 @@
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();
}
diff --git a/src/core/SkVMBlitter.cpp b/src/core/SkVMBlitter.cpp
index 2d527c4..e6eed85 100644
--- a/src/core/SkVMBlitter.cpp
+++ b/src/core/SkVMBlitter.cpp
@@ -563,12 +563,7 @@
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);