.. _program_listing_file_src_translator_cache.h: Program Listing for File cache.h ================================ |exhale_lsh| :ref:`Return to documentation for file ` (``src/translator/cache.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #pragma once #include #include #include #include #include "definitions.h" #include "translator/history.h" namespace marian::bergamot { template , class Equals = std::equal_to> class AtomicCache { public: struct Stats { size_t hits{0}; size_t misses{0}; }; explicit AtomicCache(size_t size, size_t buckets) : records_(size), mutexBuckets_(buckets) {} std::pair find(const Key &key) const { Value value; bool found = atomicLoad(key, value); return std::make_pair(found, value); } void store(const Key &key, Value value) { atomicStore(key, value); } const Stats stats() const { #ifdef ENABLE_CACHE_STATS return Stats{hits_.load(), misses_.load()}; #else ABORT("Cache statistics requested without enabling in builds. Please use -DENABLE_CACHE_STATS with cmake."); return Stats{0, 0}; #endif } private: using Record = std::pair; bool atomicLoad(const Key &key, Value &value) const { // No probing, direct map onto records_ size_t index = hash_(key) % records_.size(); size_t mutexId = index % mutexBuckets_.size(); std::lock_guard lock(mutexBuckets_[mutexId]); const Record &candidate = records_[index]; if (equals_(key, candidate.first)) { value = candidate.second; #ifdef ENABLE_CACHE_STATS ++hits_; #endif return true; } else { #ifdef ENABLE_CACHE_STATS ++misses_; #endif } return false; } void atomicStore(const Key &key, Value value) { // No probing, direct map onto records_ size_t index = hash_(key) % records_.size(); size_t mutexId = index % mutexBuckets_.size(); std::lock_guard lock(mutexBuckets_[mutexId]); Record &candidate = records_[index]; candidate.first = key; candidate.second = value; } std::vector records_; mutable std::vector mutexBuckets_; #ifdef ENABLE_CACHE_STATS mutable std::atomic hits_{0}; mutable std::atomic misses_{0}; #endif Hash hash_; Equals equals_; }; typedef AtomicCache> TranslationCache; } // namespace marian::bergamot