ConstructorCache.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. enum class CtorCacheGuardValues : intptr_t
  9. {
  10. TagFlag = 0x01,
  11. Invalid = 0x00,
  12. Special = TagFlag
  13. };
  14. ENUM_CLASS_HELPERS(CtorCacheGuardValues, intptr_t);
  15. #define MaxCachedSlotCount 65535
  16. struct ConstructorCache : public PropertyGuard
  17. {
  18. friend class JavascriptFunction;
  19. struct ContentStruct
  20. {
  21. Field(ScriptContext*) scriptContext;
  22. // In a pinch we could eliminate this and store type pending sharing in the type field as long
  23. // as the guard value flags fit below the object alignment boundary. However, this wouldn't
  24. // keep the type alive, so it would only work if we zeroed constructor caches before GC.
  25. Field(DynamicType*) pendingType;
  26. // We cache only types whose slotCount < 64K to ensure the slotCount field doesn't look like a pointer to the recycler.
  27. Field(int) slotCount;
  28. // This layout (i.e. one-byte bit fields first, then the one-byte updateAfterCtor, and then the two byte inlineSlotCount) is
  29. // chosen intentionally to make sure the whole four bytes never look like a pointer and create a false reference pinning something
  30. // in recycler heap. The isPopulated bit is always set when the cache holds any data - even if it got invalidated.
  31. Field(bool) isPopulated : 1;
  32. Field(bool) isPolymorphic : 1;
  33. Field(bool) typeUpdatePending : 1;
  34. Field(bool) ctorHasNoExplicitReturnValue : 1;
  35. Field(bool) skipDefaultNewObject : 1;
  36. // This field indicates that the type stored in this cache is the final type after constructor.
  37. Field(bool) typeIsFinal : 1;
  38. // This field indicates that the constructor cache has been invalidated due to a constructor's prototype property change.
  39. // We use this flag to determine if we should mark the cache as polymorphic and not attempt subsequent optimizations.
  40. // The cache may also be invalidated due to a guard invalidation resulting from some property change (e.g. in proto chain),
  41. // in which case we won't deem the cache polymorphic.
  42. Field(bool) hasPrototypeChanged : 1;
  43. Field(uint8) callCount;
  44. // Separate from the bit field below for convenient compare from the JIT-ed code. Doesn't currently increase the size.
  45. // If size becomes an issue, we could merge back into the bit field and use a TEST instead of CMP.
  46. Field(bool) updateAfterCtor;
  47. Field(int16) inlineSlotCount;
  48. };
  49. Field(ContentStruct) content;
  50. CompileAssert(static_cast<intptr_t>(CtorCacheGuardValues::Invalid) == static_cast<intptr_t>(NULL));
  51. static ConstructorCache DefaultInstance;
  52. public:
  53. ConstructorCache();
  54. ConstructorCache(ConstructorCache const * other);
  55. static size_t const GetOffsetOfGuardValue() { return PropertyGuard::GetOffsetOfValue(); }
  56. static size_t const GetSizeOfGuardValue() { return PropertyGuard::GetSizeOfValue(); }
  57. void Populate(DynamicType* type, ScriptContext* scriptContext, bool ctorHasNoExplicitReturnValue, bool updateAfterCtor);
  58. void PopulateForSkipDefaultNewObject(ScriptContext* scriptContext);
  59. bool TryUpdateAfterConstructor(DynamicType* type, ScriptContext* scriptContext);
  60. void UpdateInlineSlotCount();
  61. void EnableAfterTypeUpdate();
  62. intptr_t GetRawGuardValue() const { return __super::GetValue(); }
  63. DynamicType* GetGuardValueAsType() const
  64. {
  65. return reinterpret_cast<DynamicType*>((CtorCacheGuardValues)__super::GetValue() & ~CtorCacheGuardValues::TagFlag);
  66. }
  67. DynamicType* GetType() const
  68. {
  69. Assert(static_cast<intptr_t>((CtorCacheGuardValues)__super::GetValue() & CtorCacheGuardValues::TagFlag) == 0);
  70. return reinterpret_cast<DynamicType*>(__super::GetValue());
  71. }
  72. DynamicType* GetPendingType() const
  73. {
  74. return this->content.pendingType;
  75. }
  76. ScriptContext* GetScriptContext() const
  77. {
  78. return this->content.scriptContext;
  79. }
  80. int GetSlotCount() const
  81. {
  82. return this->content.slotCount;
  83. }
  84. int16 GetInlineSlotCount() const
  85. {
  86. return this->content.inlineSlotCount;
  87. }
  88. static bool IsDefault(const ConstructorCache* constructorCache)
  89. {
  90. return constructorCache == &ConstructorCache::DefaultInstance;
  91. }
  92. bool IsDefault() const
  93. {
  94. return IsDefault(this);
  95. }
  96. bool IsPopulated() const
  97. {
  98. Assert(IsConsistent());
  99. return this->content.isPopulated;
  100. }
  101. bool IsEmpty() const
  102. {
  103. Assert(IsConsistent());
  104. return !this->content.isPopulated;
  105. }
  106. bool IsPolymorphic() const
  107. {
  108. Assert(IsConsistent());
  109. return this->content.isPolymorphic;
  110. }
  111. bool GetSkipDefaultNewObject() const
  112. {
  113. return this->content.skipDefaultNewObject;
  114. }
  115. bool GetCtorHasNoExplicitReturnValue() const
  116. {
  117. return this->content.ctorHasNoExplicitReturnValue;
  118. }
  119. bool GetUpdateCacheAfterCtor() const
  120. {
  121. return this->content.updateAfterCtor;
  122. }
  123. bool GetTypeUpdatePending() const
  124. {
  125. return this->content.typeUpdatePending;
  126. }
  127. bool IsEnabled() const
  128. {
  129. return GetGuardValueAsType() != nullptr;
  130. }
  131. bool IsInvalidated() const
  132. {
  133. return (CtorCacheGuardValues)__super::GetValue() == CtorCacheGuardValues::Invalid && this->content.isPopulated;
  134. }
  135. bool NeedsTypeUpdate() const
  136. {
  137. return (CtorCacheGuardValues)__super::GetValue() == CtorCacheGuardValues::Special && this->content.typeUpdatePending;
  138. }
  139. uint8 CallCount() const
  140. {
  141. return content.callCount;
  142. }
  143. void IncCallCount()
  144. {
  145. ++content.callCount;
  146. Assert(content.callCount != 0);
  147. }
  148. bool NeedsUpdateAfterCtor() const
  149. {
  150. return this->content.updateAfterCtor;
  151. }
  152. bool IsNormal() const
  153. {
  154. return (CtorCacheGuardValues)__super::GetValue() != CtorCacheGuardValues::Invalid && static_cast<intptr_t>((CtorCacheGuardValues)__super::GetValue() & CtorCacheGuardValues::TagFlag) == 0;
  155. }
  156. bool SkipDefaultNewObject() const
  157. {
  158. return (CtorCacheGuardValues)__super::GetValue() == CtorCacheGuardValues::Special && this->content.skipDefaultNewObject;
  159. }
  160. bool IsSetUpForJit() const
  161. {
  162. return GetRawGuardValue() != NULL && !IsPolymorphic() && !NeedsUpdateAfterCtor() && (IsNormal() || SkipDefaultNewObject());
  163. }
  164. void ClearUpdateAfterCtor()
  165. {
  166. Assert(IsConsistent());
  167. Assert(this->content.isPopulated);
  168. Assert(this->content.updateAfterCtor);
  169. this->content.updateAfterCtor = false;
  170. Assert(IsConsistent());
  171. }
  172. static ConstructorCache* EnsureValidInstance(ConstructorCache* currentCache, ScriptContext* scriptContext);
  173. const void* GetAddressOfGuardValue()
  174. {
  175. return reinterpret_cast<const void*>(__super::GetAddressOfValue());
  176. }
  177. static uint32 GetOffsetOfUpdateAfterCtor()
  178. {
  179. return offsetof(ConstructorCache, content.updateAfterCtor);
  180. }
  181. void InvalidateAsGuard()
  182. {
  183. Assert(!IsDefault(this));
  184. Invalidate();
  185. // Make sure we don't leak the types.
  186. Assert(this->content.pendingType == nullptr);
  187. Assert(IsInvalidated());
  188. Assert(IsConsistent());
  189. }
  190. #if DBG
  191. bool IsConsistent() const
  192. {
  193. return (CtorCacheGuardValues)__super::GetValue() == CtorCacheGuardValues::Invalid ||
  194. (this->content.isPopulated && (
  195. ((CtorCacheGuardValues)__super::GetValue() == CtorCacheGuardValues::Special && !this->content.updateAfterCtor && this->content.skipDefaultNewObject && !this->content.typeUpdatePending && this->content.slotCount == 0 && this->content.inlineSlotCount == 0 && this->content.pendingType == nullptr) ||
  196. ((CtorCacheGuardValues)__super::GetValue() == CtorCacheGuardValues::Special && !this->content.updateAfterCtor && this->content.typeUpdatePending && !this->content.skipDefaultNewObject && this->content.pendingType != nullptr) ||
  197. (((CtorCacheGuardValues)__super::GetValue() & CtorCacheGuardValues::TagFlag) == CtorCacheGuardValues::Invalid && !this->content.skipDefaultNewObject && !this->content.typeUpdatePending && this->content.pendingType == nullptr)));
  198. }
  199. #endif
  200. #if DBG_DUMP
  201. void Dump() const;
  202. #endif
  203. private:
  204. void InvalidateOnPrototypeChange();
  205. };
  206. }