Cache.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. namespace JsUtil
  7. {
  8. template <typename T, uint size>
  9. class CircularBuffer
  10. {
  11. public:
  12. CircularBuffer():
  13. writeIndex(0),
  14. filled(false)
  15. {
  16. }
  17. void Clear()
  18. {
  19. this->writeIndex = 0;
  20. this->filled = false;
  21. }
  22. void Add(const T& value)
  23. {
  24. if (!Contains(value))
  25. {
  26. entries[writeIndex] = value;
  27. uint nextIndex = (writeIndex + 1) % size;
  28. if (nextIndex < writeIndex && !filled)
  29. {
  30. filled = true;
  31. }
  32. writeIndex = nextIndex;
  33. }
  34. }
  35. bool Contains(const T& value)
  36. {
  37. for (uint i = 0; i < GetMaxIndex(); i++)
  38. {
  39. if (DefaultComparer<T>::Equals(entries[i], value))
  40. {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. uint GetMaxIndex()
  47. {
  48. return (filled ? size : writeIndex);
  49. }
  50. const T& Item(uint index)
  51. {
  52. Assert(index < GetMaxIndex());
  53. return entries[index];
  54. }
  55. #ifdef VERBOSE_EVAL_MAP
  56. void Dump()
  57. {
  58. Output::Print(_u("Length: %d, writeIndex: %d, filled: %d\n"), size, writeIndex, filled);
  59. for (uint i = 0; i < GetMaxIndex(); i++)
  60. {
  61. Output::Print(_u("Item %d: %s\n"), i, entries[i].str.GetBuffer());
  62. }
  63. Output::Flush();
  64. }
  65. #endif
  66. bool IsEmpty()
  67. {
  68. return (writeIndex == 0 && !filled);
  69. }
  70. int GetCount()
  71. {
  72. if (!filled) return writeIndex;
  73. return size;
  74. }
  75. private:
  76. uint writeIndex;
  77. bool filled;
  78. T entries[size];
  79. };
  80. template <class TKey, int MRUSize, class TAllocator = Recycler>
  81. class MRURetentionPolicy
  82. {
  83. public:
  84. typedef CircularBuffer<TKey, MRUSize> TMRUStoreType;
  85. MRURetentionPolicy(TAllocator* allocator)
  86. {
  87. store = AllocatorNew(TAllocator, allocator, TMRUStoreType);
  88. }
  89. void NotifyAdd(const TKey& key)
  90. {
  91. this->store->Add(key);
  92. }
  93. bool CanEvict(const TKey& key)
  94. {
  95. return !store->Contains(key);
  96. }
  97. void DumpKeepAlives()
  98. {
  99. store->Dump();
  100. }
  101. private:
  102. TMRUStoreType* store;
  103. };
  104. template <
  105. class TKey,
  106. class TValue,
  107. class TAllocator,
  108. class SizePolicy,
  109. class CacheRetentionPolicy,
  110. template <typename ValueOrKey> class Comparer = DefaultComparer,
  111. template <typename K, typename V> class Entry = SimpleDictionaryEntry
  112. >
  113. class Cache
  114. {
  115. private:
  116. typedef BaseDictionary<TKey, TValue, TAllocator, SizePolicy, Comparer, Entry> TCacheStoreType;
  117. typedef typename TCacheStoreType::AllocatorType AllocatorType;
  118. class CacheStore : public TCacheStoreType
  119. {
  120. public:
  121. CacheStore(AllocatorType* allocator, int capacity) : BaseDictionary(allocator, capacity), inAdd(false) {};
  122. bool IsInAdd()
  123. {
  124. return this->inAdd;
  125. }
  126. int Add(const TKey& key, const TValue& value)
  127. {
  128. AutoRestoreValue<bool> var(&this->inAdd, true);
  129. return __super::Add(key, value);
  130. }
  131. void SetIsInAdd(bool value) {inAdd = value; }
  132. private:
  133. bool inAdd;
  134. };
  135. public:
  136. typedef TKey KeyType;
  137. typedef TValue ValueType;
  138. typedef void (*OnItemEvictedCallback)(const TKey& key, TValue value);
  139. Cache(AllocatorType * allocator, int capacity = 0):
  140. cachePolicyType(allocator)
  141. {
  142. this->cacheStore = AllocatorNew(AllocatorType, allocator, CacheStore, allocator, capacity);
  143. }
  144. int Add(const TKey& key, const TValue& value)
  145. {
  146. int index = this->cacheStore->Add(key, value);
  147. this->cachePolicyType.NotifyAdd(key);
  148. return index;
  149. }
  150. void SetIsInAdd(bool value) {this->cacheStore->SetIsInAdd(value); }
  151. void NotifyAdd(const TKey& key)
  152. {
  153. this->cachePolicyType.NotifyAdd(key);
  154. }
  155. bool TryGetValue(const TKey& key, TValue* value)
  156. {
  157. return cacheStore->TryGetValue(key, value);
  158. }
  159. bool TryGetReference(const TKey& key, TValue** value, int* index)
  160. {
  161. return cacheStore->TryGetReference(key, value, index);
  162. }
  163. bool TryGetValueAndRemove(const TKey& key, TValue* value)
  164. {
  165. return cacheStore->TryGetValueAndRemove(key, value);
  166. }
  167. TKey const& GetKeyAt(const int& index)
  168. {
  169. return cacheStore->GetKeyAt(index);
  170. }
  171. template <class Fn>
  172. void Clean(Fn callback)
  173. {
  174. if (!this->cacheStore->IsInAdd())
  175. {
  176. // Queue up items to be removed
  177. // TODO: Don't use Contains since that's linear- store pointers to the eval map key instead, and set a bit indicating that its in the dictionary?
  178. cacheStore->MapAndRemoveIf([this, callback](const CacheStore::EntryType &entry) {
  179. if (this->cachePolicyType.CanEvict(entry.Key()) || CONFIG_FLAG(ForceCleanCacheOnCollect))
  180. {
  181. callback(entry.Key(), entry.Value());
  182. if (!CONFIG_FLAG(ForceCleanCacheOnCollect))
  183. {
  184. return true;
  185. }
  186. }
  187. return false;
  188. });
  189. if (CONFIG_FLAG(ForceCleanCacheOnCollect))
  190. {
  191. this->cacheStore->Clear();
  192. Assert(this->cacheStore->Count() == 0);
  193. }
  194. }
  195. }
  196. template <class Fn>
  197. void CleanAll(Fn callback)
  198. {
  199. Assert(!this->cacheStore->IsInAdd());
  200. cacheStore->MapAndRemoveIf([this, callback](const CacheStore::EntryType &entry) -> bool {
  201. callback(entry.Key(), entry.Value());
  202. return true;
  203. });
  204. }
  205. void DumpKeepAlives()
  206. {
  207. cachePolicyType.DumpKeepAlives();
  208. }
  209. private:
  210. CacheStore* cacheStore;
  211. CacheRetentionPolicy cachePolicyType;
  212. };
  213. }