dawn-cmake/src/dawn/native/BlobCache.cpp
Loko Kung 7289bca018 Add dawn version hash to cache keys.
Motivation is to simplify cache evicting by reusing the LRU to evict stale entries from older Dawn versions since there isn't already a simple cache busting solution. The dawn version will just be pushed into the cache keys instead so old version entries will eventually be retired.

- Removes the fingerprint from the GetCachingInterface API on DawnNative.
- Adds the "fingerprint", which was just the hash, to the device;s cache key directly instead.

Bug: dawn:549
Change-Id: I573aa03a2bb96dfe044293b1176d3a7746725572
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/94140
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Loko Kung <lokokung@google.com>
2022-06-24 23:39:49 +00:00

77 lines
2.4 KiB
C++

// Copyright 2022 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "dawn/native/BlobCache.h"
#include <algorithm>
#include "dawn/common/Assert.h"
#include "dawn/common/Version_autogen.h"
#include "dawn/native/CacheKey.h"
#include "dawn/native/Instance.h"
#include "dawn/platform/DawnPlatform.h"
namespace dawn::native {
BlobCache::BlobCache(dawn::platform::CachingInterface* cachingInterface)
: mCache(cachingInterface) {}
Blob BlobCache::Load(const CacheKey& key) {
std::lock_guard<std::mutex> lock(mMutex);
return LoadInternal(key);
}
void BlobCache::Store(const CacheKey& key, size_t valueSize, const void* value) {
std::lock_guard<std::mutex> lock(mMutex);
StoreInternal(key, valueSize, value);
}
void BlobCache::Store(const CacheKey& key, const Blob& value) {
Store(key, value.Size(), value.Data());
}
Blob BlobCache::LoadInternal(const CacheKey& key) {
ASSERT(ValidateCacheKey(key));
if (mCache == nullptr) {
return Blob();
}
const size_t expectedSize = mCache->LoadData(key.data(), key.size(), nullptr, 0);
if (expectedSize > 0) {
// Need to put this inside to trigger copy elision.
Blob result = CreateBlob(expectedSize);
const size_t actualSize =
mCache->LoadData(key.data(), key.size(), result.Data(), expectedSize);
ASSERT(expectedSize == actualSize);
return result;
}
return Blob();
}
void BlobCache::StoreInternal(const CacheKey& key, size_t valueSize, const void* value) {
ASSERT(ValidateCacheKey(key));
ASSERT(value != nullptr);
ASSERT(valueSize > 0);
if (mCache == nullptr) {
return;
}
mCache->StoreData(key.data(), key.size(), value, valueSize);
}
bool BlobCache::ValidateCacheKey(const CacheKey& key) {
return std::search(key.begin(), key.end(), kDawnVersion.begin(), kDawnVersion.end()) !=
key.end();
}
} // namespace dawn::native