EvalMapRecord.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 Js
  7. {
  8. // Use as the top level comparer for two level dictionary. Note that two
  9. // values are equal as long as their fastHash is the same (and moduleID/isStrict is the same).
  10. // This comparer is used for the top level dictionary in two level evalmap dictionary.
  11. template <class T>
  12. struct FastEvalMapStringComparer
  13. {
  14. static bool Equals(T left, T right)
  15. {
  16. return (left.hash == right.hash) &&
  17. (left.moduleID == right.moduleID) &&
  18. (left.IsStrict() == right.IsStrict());
  19. }
  20. static hash_t GetHashCode(T t)
  21. {
  22. return (hash_t)t;
  23. }
  24. };
  25. // The value in top level of two level dictionary. It might contain only the single value
  26. // (TValue), or a second level dictionary.
  27. template <class TKey, class TValue, class SecondaryDictionary, class NestedKey>
  28. class TwoLevelHashRecord
  29. {
  30. public:
  31. TwoLevelHashRecord(TValue newValue) :
  32. singleValue(true), value(newValue) {}
  33. TwoLevelHashRecord() :
  34. singleValue(true), value(nullptr) {}
  35. SecondaryDictionary* GetDictionary()
  36. {
  37. Assert(!singleValue);
  38. return nestedMap;
  39. }
  40. bool TryGetValue(TKey& key, TValue* value)
  41. {
  42. if (IsValue())
  43. {
  44. *value = GetValue();
  45. return true;
  46. }
  47. return GetDictionary()->TryGetValue(key, value);
  48. }
  49. void Add(const TKey& key, const TValue& newValue)
  50. {
  51. Assert(!singleValue);
  52. NestedKey nestedKey;
  53. ConvertKey(key, nestedKey);
  54. nestedMap->Item(nestedKey, newValue);
  55. #ifdef PROFILE_EVALMAP
  56. if (Configuration::Global.flags.ProfileEvalMap)
  57. {
  58. Output::Print(_u("EvalMap fastcache collision:\t key = %d count = %d\n"), (hash_t)key, nestedMap->Count());
  59. }
  60. #endif
  61. }
  62. void Remove(const TKey& key)
  63. {
  64. Assert(!singleValue);
  65. NestedKey nestedKey;
  66. ConvertKey(key, nestedKey);
  67. nestedMap->Remove(nestedKey);
  68. }
  69. void ConvertToDictionary(TKey& key, Recycler* recycler)
  70. {
  71. Assert(singleValue);
  72. SecondaryDictionary* dictionary = RecyclerNew(recycler, SecondaryDictionary, recycler);
  73. auto newValue = value;
  74. nestedMap = dictionary;
  75. singleValue = false;
  76. Add(key, newValue);
  77. }
  78. bool IsValue() const { return singleValue; }
  79. TValue GetValue() const { Assert(singleValue); return value; }
  80. bool IsDictionaryEntry() const { return !singleValue; }
  81. private:
  82. Field(bool) singleValue;
  83. union
  84. {
  85. Field(TValue) value;
  86. Field(SecondaryDictionary*) nestedMap;
  87. };
  88. };
  89. // The two level dictionary. top level needs to be either simple hash value, or
  90. // key needs to be equals for all nested values.
  91. template <class Key, class Value, class EntryRecord, class TopLevelDictionary, class NestedKey>
  92. class TwoLevelHashDictionary
  93. {
  94. template <class T, class Value>
  95. class AutoRestoreSetInAdd
  96. {
  97. public:
  98. AutoRestoreSetInAdd(T* instance, Value value) :
  99. instance(instance), value(value)
  100. {
  101. instance->SetIsInAdd(value);
  102. }
  103. ~AutoRestoreSetInAdd()
  104. {
  105. instance->SetIsInAdd(!value);
  106. }
  107. private:
  108. T* instance;
  109. Value value;
  110. };
  111. public:
  112. TwoLevelHashDictionary(TopLevelDictionary* cache, Recycler* recycler) :
  113. dictionary(cache),
  114. recycler(recycler)
  115. {
  116. }
  117. bool TryGetValue(const Key& key, Value* value)
  118. {
  119. EntryRecord* const * entryRecord;
  120. Key cachedKey;
  121. int index;
  122. bool success = dictionary->TryGetReference(key, &entryRecord, &index);
  123. if (success && ((*entryRecord) != nullptr))
  124. {
  125. cachedKey = dictionary->GetKeyAt(index);
  126. if ((*entryRecord)->IsValue())
  127. {
  128. success = (cachedKey == key);
  129. if (success)
  130. {
  131. *value = (*entryRecord)->GetValue();
  132. }
  133. }
  134. else
  135. {
  136. NestedKey nestedKey;
  137. ConvertKey(key, nestedKey);
  138. success = (*entryRecord)->GetDictionary()->TryGetValue(nestedKey, value);
  139. }
  140. }
  141. else
  142. {
  143. success = false;
  144. }
  145. return success;
  146. }
  147. TopLevelDictionary* GetDictionary() const { return dictionary; }
  148. void NotifyAdd(const Key& key)
  149. {
  150. dictionary->NotifyAdd(key);
  151. }
  152. void Add(const Key& key, Value value)
  153. {
  154. EntryRecord* const * entryRecord;
  155. int index;
  156. bool success = dictionary->TryGetReference(key, &entryRecord, &index);
  157. if (success && ((*entryRecord) != nullptr))
  158. {
  159. AutoRestoreSetInAdd<TopLevelDictionary, bool> autoRestoreSetInAdd(this->dictionary, true);
  160. if ((*entryRecord)->IsValue())
  161. {
  162. Key oldKey = dictionary->GetKeyAt(index);
  163. (*entryRecord)->ConvertToDictionary(oldKey, recycler);
  164. }
  165. (*entryRecord)->Add(key, value);
  166. }
  167. else
  168. {
  169. EntryRecord* newRecord = RecyclerNew(recycler, EntryRecord, value);
  170. dictionary->Add(key, newRecord);
  171. #ifdef PROFILE_EVALMAP
  172. if (Configuration::Global.flags.ProfileEvalMap)
  173. {
  174. Output::Print(_u("EvalMap fastcache set:\t key = %d \n"), (hash_t)key);
  175. }
  176. #endif
  177. }
  178. }
  179. private:
  180. Field(TopLevelDictionary*) dictionary;
  181. FieldNoBarrier(Recycler*) recycler;
  182. };
  183. }