CacheOperators.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. #include "RuntimeLanguagePch.h"
  6. #if DBG
  7. #include "Types/PathTypeHandler.h"
  8. #endif
  9. namespace Js
  10. {
  11. void CacheOperators::CachePropertyRead(
  12. Var startingObject,
  13. RecyclableObject * objectWithProperty,
  14. const bool isRoot,
  15. PropertyId propertyId,
  16. const bool isMissing,
  17. PropertyValueInfo* info,
  18. ScriptContext * requestContext)
  19. {
  20. if (!CanCachePropertyRead(info, objectWithProperty, requestContext))
  21. {
  22. return;
  23. }
  24. AssertMsg(!(info->GetFlags() & InlineCacheGetterFlag), "We cache getters only in DictionaryTypeHandler::GetProperty() before they were executed");
  25. AssertMsg((info->GetFlags() & InlineCacheSetterFlag) == 0, "invalid setter flag in CachePropertyRead");
  26. if (info->GetInstance() != objectWithProperty) // We can't cache if slot owner is not the object
  27. {
  28. AssertMsg(info->IsNoCache() || objectWithProperty->GetPropertyIndex(propertyId) == Constants::NoSlot, "Missed updating PropertyValueInfo?");
  29. return;
  30. }
  31. PropertyIndex propertyIndex = info->GetPropertyIndex();
  32. Assert(propertyIndex == objectWithProperty->GetPropertyIndex(propertyId) ||
  33. (RootObjectBase::Is(objectWithProperty) && propertyIndex == RootObjectBase::FromVar(objectWithProperty)->GetRootPropertyIndex(propertyId)));
  34. Assert(DynamicType::Is(objectWithProperty->GetTypeId()));
  35. #if ENABLE_FIXED_FIELDS
  36. // We are populating a cache guarded by the instance's type (not the type of the object with property somewhere in the prototype chain),
  37. // so we only care if the instance's property (if any) is fixed.
  38. Assert(info->IsNoCache() || !info->IsStoreFieldCacheEnabled() || info->GetInstance() != objectWithProperty || !objectWithProperty->IsFixedProperty(propertyId));
  39. #endif
  40. DynamicObject * dynamicObjectWithProperty = DynamicObject::FromVar(objectWithProperty);
  41. PropertyIndex slotIndex;
  42. bool isInlineSlot;
  43. dynamicObjectWithProperty->GetDynamicType()->GetTypeHandler()->PropertyIndexToInlineOrAuxSlotIndex(propertyIndex, &slotIndex, &isInlineSlot);
  44. const bool isProto = objectWithProperty != startingObject;
  45. if(!isProto)
  46. {
  47. Assert(info->IsWritable() == (objectWithProperty->IsWritable(propertyId) ? true : false));
  48. // Because StFld and LdFld caches aren't shared, we can safely cache for LdFld operations even if the property isn't writable.
  49. }
  50. else if(
  51. PropertyValueInfo::PrototypeCacheDisabled((PropertyValueInfo*)info) ||
  52. !RecyclableObject::Is(startingObject) ||
  53. RecyclableObject::UnsafeFromVar(startingObject)->GetScriptContext() != requestContext)
  54. {
  55. // Don't need to cache if the beginning property is number etc.
  56. return;
  57. }
  58. #ifdef TELEMETRY_AddToCache
  59. // For performance reasons, only execute this code in interpreted mode, not JIT.
  60. // This method only returns true in interpreted mode and can be used to detect interpreted mode.
  61. if (info->AllowResizingPolymorphicInlineCache())
  62. {
  63. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  64. {
  65. requestContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(objectWithProperty, propertyId, nullptr);
  66. }
  67. }
  68. #endif
  69. Cache<false, true, true>(
  70. isProto,
  71. dynamicObjectWithProperty,
  72. isRoot,
  73. RecyclableObject::FromVar(startingObject)->GetType(),
  74. nullptr,
  75. propertyId,
  76. slotIndex,
  77. isInlineSlot,
  78. isMissing,
  79. 0,
  80. info,
  81. requestContext);
  82. }
  83. void CacheOperators::CachePropertyReadForGetter(
  84. PropertyValueInfo *info,
  85. Var originalInstance,
  86. JsUtil::CharacterBuffer<WCHAR> const& propertyName,
  87. ScriptContext* requestContext)
  88. {
  89. PropertyRecord const* propertyRecord;
  90. requestContext->GetOrAddPropertyRecord(propertyName, &propertyRecord);
  91. CachePropertyReadForGetter(info, originalInstance, propertyRecord->GetPropertyId(), requestContext);
  92. }
  93. void CacheOperators::CachePropertyReadForGetter(
  94. PropertyValueInfo *info,
  95. Var originalInstance,
  96. PropertyId propertyId,
  97. ScriptContext* requestContext)
  98. {
  99. if (!info || !CacheOperators::CanCachePropertyRead(info, info->GetInstance(), requestContext))
  100. {
  101. return;
  102. }
  103. Assert(RecyclableObject::Is(originalInstance));
  104. Assert(DynamicType::Is(info->GetInstance()->GetTypeId()));
  105. DynamicObject * dynamicInstance = DynamicObject::FromVar(info->GetInstance());
  106. PropertyIndex slotIndex;
  107. bool isInlineSlot;
  108. dynamicInstance->GetDynamicType()->GetTypeHandler()->PropertyIndexToInlineOrAuxSlotIndex(info->GetPropertyIndex(), &slotIndex, &isInlineSlot);
  109. const bool isProto = info->GetInstance() != originalInstance;
  110. if(isProto &&
  111. (
  112. !RecyclableObject::Is(originalInstance) ||
  113. RecyclableObject::FromVar(originalInstance)->GetScriptContext() != requestContext
  114. ))
  115. {
  116. // Don't need to cache if the beginning property is number etc.
  117. return;
  118. }
  119. #ifdef TELEMETRY_AddToCache
  120. if (info->AllowResizingPolymorphicInlineCache()) // If in interpreted mode, not JIT.
  121. {
  122. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  123. {
  124. requestContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(info->GetInstance(), propertyId, nullptr);
  125. }
  126. }
  127. #endif
  128. Cache<true, true, false>(
  129. isProto,
  130. dynamicInstance,
  131. false,
  132. RecyclableObject::FromVar(originalInstance)->GetType(),
  133. nullptr,
  134. propertyId,
  135. slotIndex,
  136. isInlineSlot,
  137. false,
  138. 0,
  139. info,
  140. requestContext);
  141. }
  142. void CacheOperators::CachePropertyWrite(
  143. RecyclableObject * object,
  144. const bool isRoot,
  145. Type* typeWithoutProperty,
  146. PropertyId propertyId,
  147. PropertyValueInfo* info,
  148. ScriptContext * requestContext)
  149. {
  150. Assert(typeWithoutProperty != nullptr);
  151. if (!CacheOperators::CanCachePropertyWrite(info, object, requestContext))
  152. {
  153. return;
  154. }
  155. BOOL isSetter = (info->GetFlags() == InlineCacheSetterFlag);
  156. if (info->GetInstance() != object) // We can't cache if slot owner is not the object
  157. {
  158. if (!isSetter)
  159. {
  160. AssertMsg(info->IsNoCache() || object->GetPropertyIndex(propertyId) == Constants::NoSlot, "Missed updating PropertyValueInfo?");
  161. return;
  162. }
  163. }
  164. PropertyIndex propertyIndex = info->GetPropertyIndex();
  165. if (isSetter)
  166. {
  167. if (propertyIndex & 0xf000)
  168. {
  169. return;
  170. }
  171. }
  172. else
  173. {
  174. if (propertyIndex == Constants::NoSlot)
  175. {
  176. return;
  177. }
  178. }
  179. Assert((!isRoot && propertyIndex == object->GetPropertyIndex(propertyId)) || isSetter ||
  180. (isRoot && propertyIndex == RootObjectBase::FromVar(object)->GetRootPropertyIndex(propertyId)));
  181. Assert(DynamicType::Is(object->GetTypeId()));
  182. AssertMsg((info->GetFlags() & InlineCacheGetterFlag) == 0, "invalid getter for CachePropertyWrite");
  183. RecyclableObject* instance = info->GetInstance();
  184. DynamicObject * dynamicInstance = DynamicObject::FromVar(instance);
  185. PropertyIndex slotIndex;
  186. bool isInlineSlot;
  187. dynamicInstance->GetDynamicType()->GetTypeHandler()->PropertyIndexToInlineOrAuxSlotIndex(propertyIndex, &slotIndex, &isInlineSlot);
  188. if (!isSetter)
  189. {
  190. AssertMsg(instance == object, "invalid instance for non setter");
  191. Assert(DynamicType::Is(typeWithoutProperty));
  192. #if ENABLE_FIXED_FIELDS
  193. Assert(info->IsNoCache() || !info->IsStoreFieldCacheEnabled() || object->CanStorePropertyValueDirectly(propertyId, isRoot));
  194. #endif
  195. Assert(info->IsWritable());
  196. DynamicType* newType = (DynamicType*)object->GetType();
  197. DynamicType* oldType = (DynamicType*)typeWithoutProperty;
  198. Assert(newType);
  199. int requiredAuxSlotCapacity;
  200. // Don't cache property adds for types that aren't (yet) shared. We must go down the slow path to force type sharing
  201. // and invalidate any potential fixed fields this type may have.
  202. // Don't cache property adds to prototypes, so we don't have to check if the object is a prototype on the fast path.
  203. if (newType != oldType && newType->GetIsShared() && newType->GetTypeHandler()->IsPathTypeHandler() && (!oldType->GetTypeHandler()->GetIsPrototype()))
  204. {
  205. DynamicTypeHandler* oldTypeHandler = oldType->GetTypeHandler();
  206. DynamicTypeHandler* newTypeHandler = newType->GetTypeHandler();
  207. // This may be the transition from deferred type handler to path type handler. Don't try to cache now.
  208. if (!oldTypeHandler->IsPathTypeHandler())
  209. {
  210. return;
  211. }
  212. int oldCapacity = oldTypeHandler->GetSlotCapacity();
  213. int newCapacity = newTypeHandler->GetSlotCapacity();
  214. int newInlineCapacity = newTypeHandler->GetInlineSlotCapacity();
  215. // We are adding only one property here. If some other properties were added as a side effect on the slow path
  216. // we should never cache the type transition, as the other property slots will not be populated by the fast path.
  217. AssertMsg(((PathTypeHandlerBase *)oldTypeHandler)->GetPropertyCount() + 1 == ((PathTypeHandlerBase *)newTypeHandler)->GetPropertyCount() ||
  218. ((PathTypeHandlerBase *)oldTypeHandler)->GetPropertyCount() == ((PathTypeHandlerBase *)newTypeHandler)->GetPropertyCount(),
  219. "Don't cache type transitions that add multiple properties.");
  220. // InlineCache::TrySetProperty assumes the following invariants to decide if and how to adjust auxiliary slot capacity.
  221. AssertMsg(DynamicObject::IsTypeHandlerCompatibleForObjectHeaderInlining(oldTypeHandler, newTypeHandler),
  222. "TypeHandler should be compatible for transition.");
  223. // If the slot is inlined then we should never need to adjust auxiliary slot capacity.
  224. Assert(!isInlineSlot || oldCapacity == newCapacity || newCapacity <= newInlineCapacity);
  225. // If the slot is not inlined then the new type must have some auxiliary slots.
  226. Assert(isInlineSlot || newCapacity > newInlineCapacity);
  227. // If the slot is not inlined and the property being added exceeds the old type's slot capacity, then slotIndex corresponds
  228. // to the number of occupied auxiliary slots (i.e. the old type's auxiliary slot capacity).
  229. // If the object is optimized for <=2 properties, then slotIndex should be same as the oldCapacity(as there is no inlineSlots in the new typeHandler).
  230. Assert(
  231. isInlineSlot ||
  232. oldCapacity == newCapacity ||
  233. slotIndex == oldCapacity - oldTypeHandler->GetInlineSlotCapacity() ||
  234. (
  235. oldTypeHandler->IsObjectHeaderInlinedTypeHandler() &&
  236. newInlineCapacity ==
  237. oldTypeHandler->GetInlineSlotCapacity() -
  238. DynamicTypeHandler::GetObjectHeaderInlinableSlotCapacity() &&
  239. slotIndex == DynamicTypeHandler::GetObjectHeaderInlinableSlotCapacity()
  240. ));
  241. requiredAuxSlotCapacity = (!isInlineSlot && oldCapacity < newCapacity) ? newCapacity - newInlineCapacity : 0;
  242. // Required auxiliary slot capacity must fit in the available inline cache bits.
  243. Assert(requiredAuxSlotCapacity < (0x01 << InlineCache::RequiredAuxSlotCapacityBitCount));
  244. }
  245. else
  246. {
  247. typeWithoutProperty = nullptr;
  248. requiredAuxSlotCapacity = 0;
  249. }
  250. Cache<false, false, true>(
  251. false,
  252. DynamicObject::FromVar(object),
  253. isRoot,
  254. object->GetType(),
  255. typeWithoutProperty,
  256. propertyId,
  257. slotIndex,
  258. isInlineSlot,
  259. false,
  260. requiredAuxSlotCapacity,
  261. info,
  262. requestContext);
  263. return;
  264. }
  265. const bool isProto = instance != object;
  266. if(isProto && instance->GetScriptContext() != requestContext)
  267. {
  268. // Don't cache if object and prototype are from different context
  269. return;
  270. }
  271. Cache<true, false, false>(
  272. isProto,
  273. dynamicInstance,
  274. false,
  275. object->GetType(),
  276. nullptr,
  277. propertyId,
  278. slotIndex,
  279. isInlineSlot,
  280. false,
  281. 0,
  282. info,
  283. requestContext);
  284. }
  285. bool CacheOperators::CanCachePropertyRead(const PropertyValueInfo *info, RecyclableObject * object, ScriptContext * requestContext)
  286. {
  287. return
  288. info &&
  289. info->GetPropertyIndex() != Constants::NoSlot &&
  290. (info->GetInlineCache() || info->GetPolymorphicInlineCache() || info->GetFunctionBody()) &&
  291. CanCachePropertyRead(object, requestContext);
  292. }
  293. bool CacheOperators::CanCachePropertyRead(RecyclableObject * object, ScriptContext * requestContext)
  294. {
  295. return object->GetScriptContext() == requestContext && !PHASE_OFF1(InlineCachePhase);
  296. }
  297. bool CacheOperators::CanCachePropertyWrite(const PropertyValueInfo *info, RecyclableObject * object, ScriptContext * requestContext)
  298. {
  299. return
  300. info &&
  301. (info->GetInlineCache() || info->GetPolymorphicInlineCache() || info->GetFunctionBody()) &&
  302. CanCachePropertyWrite(object, requestContext);
  303. }
  304. bool CacheOperators::CanCachePropertyWrite(RecyclableObject * object, ScriptContext * requestContext)
  305. {
  306. return object->GetScriptContext() == requestContext && DynamicType::Is(object->GetTypeId()) && !PHASE_OFF1(InlineCachePhase);
  307. }
  308. #if DBG_DUMP
  309. void CacheOperators::TraceCache(InlineCache * inlineCache, const char16 * methodName, PropertyId propertyId, ScriptContext * requestContext, RecyclableObject * object)
  310. {
  311. TraceCacheCommon(methodName, propertyId, requestContext, object);
  312. if(inlineCache)
  313. {
  314. Output::Print(_u("Inline Cache: \n "));
  315. inlineCache->Dump();
  316. }
  317. Output::Print(_u("\n"));
  318. Output::Flush();
  319. }
  320. void CacheOperators::TraceCache(PolymorphicInlineCache * polymorphicInlineCache, const char16 * methodName, PropertyId propertyId, ScriptContext * requestContext, RecyclableObject * object)
  321. {
  322. TraceCacheCommon(methodName, propertyId, requestContext, object);
  323. Output::Print(_u("Polymorphic Inline Cache, size = %d :\n"), polymorphicInlineCache->GetSize());
  324. polymorphicInlineCache->Dump();
  325. Output::Flush();
  326. }
  327. void CacheOperators::TraceCacheCommon(const char16 * methodName, PropertyId propertyId, ScriptContext * requestContext, RecyclableObject * object)
  328. {
  329. if(object)
  330. {
  331. JavascriptFunction* caller;
  332. const WCHAR* callerName = NULL;
  333. uint lineNumber = 0;
  334. uint columnNumber = 0;
  335. if(JavascriptStackWalker::GetCaller(&caller, requestContext))
  336. {
  337. FunctionBody *functionBody = caller->GetFunctionBody();
  338. callerName = functionBody->GetExternalDisplayName();
  339. lineNumber = functionBody->GetLineNumber();
  340. columnNumber = functionBody->GetColumnNumber();
  341. }
  342. Output::Print(_u("%s, %s, %s(%d:%d), InType: 0x%X "),
  343. methodName,
  344. requestContext->GetPropertyName(propertyId)->GetBuffer(),
  345. callerName,
  346. lineNumber,
  347. columnNumber,
  348. object->GetType());
  349. }
  350. }
  351. #endif
  352. }