PropertyRecordUsageCache.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "RuntimeLibraryPch.h"
  6. namespace Js
  7. {
  8. PropertyRecordUsageCache::PropertyRecordUsageCache() :
  9. propertyRecord(nullptr),
  10. ldElemInlineCache(nullptr),
  11. stElemInlineCache(nullptr),
  12. hitRate(0)
  13. {
  14. // Required due to DEFINE_VTABLE_CTOR on parent, but useless
  15. }
  16. PropertyRecordUsageCache::PropertyRecordUsageCache(StaticType* type, const PropertyRecord* propertyRecord) :
  17. propertyRecord(propertyRecord),
  18. ldElemInlineCache(nullptr),
  19. stElemInlineCache(nullptr),
  20. hitRate(0)
  21. {
  22. // TODO: in future, might be worth putting these inline to avoid extra allocations. PIC copy API needs to be updated to support this though
  23. this->ldElemInlineCache = ScriptContextPolymorphicInlineCache::New(MinPropertyStringInlineCacheSize, type->GetLibrary());
  24. this->stElemInlineCache = ScriptContextPolymorphicInlineCache::New(MinPropertyStringInlineCacheSize, type->GetLibrary());
  25. }
  26. PolymorphicInlineCache* PropertyRecordUsageCache::GetLdElemInlineCache() const
  27. {
  28. return this->ldElemInlineCache;
  29. }
  30. PolymorphicInlineCache* PropertyRecordUsageCache::GetStElemInlineCache() const
  31. {
  32. return this->stElemInlineCache;
  33. }
  34. bool PropertyRecordUsageCache::ShouldUseCache() const
  35. {
  36. return this->hitRate > (int)CONFIG_FLAG(PropertyCacheMissThreshold);
  37. }
  38. void PropertyRecordUsageCache::RegisterCacheMiss()
  39. {
  40. this->hitRate -= (int)CONFIG_FLAG(PropertyCacheMissPenalty);
  41. if (this->hitRate < (int)CONFIG_FLAG(PropertyCacheMissReset))
  42. {
  43. this->hitRate = 0;
  44. }
  45. }
  46. PolymorphicInlineCache * PropertyRecordUsageCache::CreateBiggerPolymorphicInlineCache(bool isLdElem)
  47. {
  48. PolymorphicInlineCache * polymorphicInlineCache = isLdElem ? GetLdElemInlineCache() : GetStElemInlineCache();
  49. ScriptContext * scriptContext = polymorphicInlineCache->GetScriptContext();
  50. Assert(polymorphicInlineCache && polymorphicInlineCache->CanAllocateBigger());
  51. uint16 polymorphicInlineCacheSize = polymorphicInlineCache->GetSize();
  52. uint16 newPolymorphicInlineCacheSize = PolymorphicInlineCache::GetNextSize(polymorphicInlineCacheSize);
  53. Assert(newPolymorphicInlineCacheSize > polymorphicInlineCacheSize);
  54. PolymorphicInlineCache * newPolymorphicInlineCache = ScriptContextPolymorphicInlineCache::New(newPolymorphicInlineCacheSize, scriptContext->GetLibrary());
  55. polymorphicInlineCache->CopyTo(this->propertyRecord->GetPropertyId(), scriptContext, newPolymorphicInlineCache);
  56. if (isLdElem)
  57. {
  58. this->ldElemInlineCache = newPolymorphicInlineCache;
  59. }
  60. else
  61. {
  62. this->stElemInlineCache = newPolymorphicInlineCache;
  63. }
  64. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  65. if (PHASE_VERBOSE_TRACE1(Js::PolymorphicInlineCachePhase) || PHASE_TRACE1(PropertyCachePhase))
  66. {
  67. Output::Print(_u("PropertyRecordUsageCache '%s' : Bigger PIC, oldSize = %d, newSize = %d\n"), GetString(), polymorphicInlineCacheSize, newPolymorphicInlineCacheSize);
  68. }
  69. #endif
  70. return newPolymorphicInlineCache;
  71. }
  72. }