CodeCache.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (C) 2012, 2013 Apple Inc. All Rights Reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CodeCache_h
  26. #define CodeCache_h
  27. #include "CodeSpecializationKind.h"
  28. #include "ParserModes.h"
  29. #include "SourceCode.h"
  30. #include "Strong.h"
  31. #include "WeakRandom.h"
  32. #include <wtf/CurrentTime.h>
  33. #include <wtf/FixedArray.h>
  34. #include <wtf/Forward.h>
  35. #include <wtf/PassOwnPtr.h>
  36. #include <wtf/RandomNumber.h>
  37. #include <wtf/text/WTFString.h>
  38. namespace JSC {
  39. class EvalExecutable;
  40. class FunctionBodyNode;
  41. class Identifier;
  42. class JSScope;
  43. class ProgramExecutable;
  44. class UnlinkedCodeBlock;
  45. class UnlinkedEvalCodeBlock;
  46. class UnlinkedFunctionCodeBlock;
  47. class UnlinkedFunctionExecutable;
  48. class UnlinkedProgramCodeBlock;
  49. class VM;
  50. struct ParserError;
  51. class SourceCode;
  52. class SourceProvider;
  53. class SourceCodeKey {
  54. public:
  55. enum CodeType { EvalType, ProgramType, FunctionType };
  56. SourceCodeKey()
  57. {
  58. }
  59. SourceCodeKey(const SourceCode& sourceCode, const String& name, CodeType codeType, JSParserStrictness jsParserStrictness)
  60. : m_sourceCode(sourceCode)
  61. , m_name(name)
  62. , m_flags((codeType << 1) | jsParserStrictness)
  63. , m_hash(string().impl()->hash())
  64. {
  65. }
  66. SourceCodeKey(WTF::HashTableDeletedValueType)
  67. : m_sourceCode(WTF::HashTableDeletedValue)
  68. {
  69. }
  70. bool isHashTableDeletedValue() const { return m_sourceCode.isHashTableDeletedValue(); }
  71. unsigned hash() const { return m_hash; }
  72. size_t length() const { return m_sourceCode.length(); }
  73. bool isNull() const { return m_sourceCode.isNull(); }
  74. // To save memory, we compute our string on demand. It's expected that source
  75. // providers cache their strings to make this efficient.
  76. String string() const { return m_sourceCode.toString(); }
  77. bool operator==(const SourceCodeKey& other) const
  78. {
  79. return m_hash == other.m_hash
  80. && length() == other.length()
  81. && m_flags == other.m_flags
  82. && m_name == other.m_name
  83. && string() == other.string();
  84. }
  85. private:
  86. SourceCode m_sourceCode;
  87. String m_name;
  88. unsigned m_flags;
  89. unsigned m_hash;
  90. };
  91. struct SourceCodeKeyHash {
  92. static unsigned hash(const SourceCodeKey& key) { return key.hash(); }
  93. static bool equal(const SourceCodeKey& a, const SourceCodeKey& b) { return a == b; }
  94. static const bool safeToCompareToEmptyOrDeleted = false;
  95. };
  96. struct SourceCodeKeyHashTraits : SimpleClassHashTraits<SourceCodeKey> {
  97. static const bool hasIsEmptyValueFunction = true;
  98. static bool isEmptyValue(const SourceCodeKey& sourceCodeKey) { return sourceCodeKey.isNull(); }
  99. };
  100. struct SourceCodeValue {
  101. SourceCodeValue()
  102. {
  103. }
  104. SourceCodeValue(VM& vm, JSCell* cell, int64_t age)
  105. : cell(vm, cell)
  106. , age(age)
  107. {
  108. }
  109. Strong<JSCell> cell;
  110. int64_t age;
  111. };
  112. class CodeCacheMap {
  113. public:
  114. typedef HashMap<SourceCodeKey, SourceCodeValue, SourceCodeKeyHash, SourceCodeKeyHashTraits> MapType;
  115. typedef MapType::iterator iterator;
  116. typedef MapType::AddResult AddResult;
  117. CodeCacheMap(int64_t workingSetMaxBytes, size_t workingSetMaxEntries)
  118. : m_size(0)
  119. , m_sizeAtLastPrune(0)
  120. , m_timeAtLastPrune(monotonicallyIncreasingTime())
  121. , m_minCapacity(0)
  122. , m_capacity(0)
  123. , m_age(0)
  124. , m_workingSetMaxBytes(workingSetMaxBytes)
  125. , m_workingSetMaxEntries(workingSetMaxEntries)
  126. {
  127. }
  128. AddResult add(const SourceCodeKey& key, const SourceCodeValue& value)
  129. {
  130. prune();
  131. AddResult addResult = m_map.add(key, value);
  132. if (addResult.isNewEntry) {
  133. m_size += key.length();
  134. m_age += key.length();
  135. return addResult;
  136. }
  137. int64_t age = m_age - addResult.iterator->value.age;
  138. if (age > m_capacity) {
  139. // A requested object is older than the cache's capacity. We can
  140. // infer that requested objects are subject to high eviction probability,
  141. // so we grow the cache to improve our hit rate.
  142. m_capacity += recencyBias * oldObjectSamplingMultiplier * key.length();
  143. } else if (age < m_capacity / 2) {
  144. // A requested object is much younger than the cache's capacity. We can
  145. // infer that requested objects are subject to low eviction probability,
  146. // so we shrink the cache to save memory.
  147. m_capacity -= recencyBias * key.length();
  148. if (m_capacity < m_minCapacity)
  149. m_capacity = m_minCapacity;
  150. }
  151. addResult.iterator->value.age = m_age;
  152. m_age += key.length();
  153. return addResult;
  154. }
  155. void remove(iterator it)
  156. {
  157. m_size -= it->key.length();
  158. m_map.remove(it);
  159. }
  160. void clear()
  161. {
  162. m_size = 0;
  163. m_age = 0;
  164. m_map.clear();
  165. }
  166. int64_t age() { return m_age; }
  167. static const int64_t globalWorkingSetMaxBytes;
  168. static const size_t globalWorkingSetMaxEntries;
  169. // We have a smaller cap for the per-codeblock CodeCache that approximates the
  170. // linked EvalCodeCache limits, but still allows us to keep large string based
  171. // evals at least partially cached.
  172. static const unsigned nonGlobalWorkingSetScale;
  173. static const int64_t nonGlobalWorkingSetMaxBytes;
  174. static const size_t nonGlobalWorkingSetMaxEntries;
  175. private:
  176. // This constant factor biases cache capacity toward allowing a minimum
  177. // working set to enter the cache before it starts evicting.
  178. static const double workingSetTime;
  179. // This constant factor biases cache capacity toward recent activity. We
  180. // want to adapt to changing workloads.
  181. static const int64_t recencyBias = 4;
  182. // This constant factor treats a sampled event for one old object as if it
  183. // happened for many old objects. Most old objects are evicted before we can
  184. // sample them, so we need to extrapolate from the ones we do sample.
  185. static const int64_t oldObjectSamplingMultiplier = 32;
  186. size_t numberOfEntries() const { return static_cast<size_t>(m_map.size()); }
  187. bool canPruneQuickly() const { return numberOfEntries() < m_workingSetMaxEntries; }
  188. void pruneSlowCase();
  189. void prune()
  190. {
  191. if (m_size <= m_capacity && canPruneQuickly())
  192. return;
  193. if (monotonicallyIncreasingTime() - m_timeAtLastPrune < workingSetTime
  194. && m_size - m_sizeAtLastPrune < m_workingSetMaxBytes
  195. && canPruneQuickly())
  196. return;
  197. pruneSlowCase();
  198. }
  199. MapType m_map;
  200. int64_t m_size;
  201. int64_t m_sizeAtLastPrune;
  202. double m_timeAtLastPrune;
  203. int64_t m_minCapacity;
  204. int64_t m_capacity;
  205. int64_t m_age;
  206. const int64_t m_workingSetMaxBytes;
  207. const size_t m_workingSetMaxEntries;
  208. };
  209. // Caches top-level code such as <script>, eval(), new Function, and JSEvaluateScript().
  210. class CodeCache : public RefCounted<CodeCache> {
  211. public:
  212. enum CodeCacheKind { GlobalCodeCache, NonGlobalCodeCache };
  213. static PassRefPtr<CodeCache> create(CodeCacheKind kind) { return adoptRef(new CodeCache(kind)); }
  214. UnlinkedProgramCodeBlock* getProgramCodeBlock(VM&, ProgramExecutable*, const SourceCode&, JSParserStrictness, DebuggerMode, ProfilerMode, ParserError&);
  215. UnlinkedEvalCodeBlock* getEvalCodeBlock(VM&, JSScope*, EvalExecutable*, const SourceCode&, JSParserStrictness, DebuggerMode, ProfilerMode, ParserError&);
  216. UnlinkedFunctionExecutable* getFunctionExecutableFromGlobalCode(VM&, const Identifier&, const SourceCode&, ParserError&);
  217. ~CodeCache();
  218. void clear()
  219. {
  220. m_sourceCode.clear();
  221. }
  222. private:
  223. CodeCache(CodeCacheKind);
  224. template <class UnlinkedCodeBlockType, class ExecutableType>
  225. UnlinkedCodeBlockType* getCodeBlock(VM&, JSScope*, ExecutableType*, const SourceCode&, JSParserStrictness, DebuggerMode, ProfilerMode, ParserError&);
  226. template <class UnlinkedCodeBlockType, class ExecutableType>
  227. UnlinkedCodeBlockType* generateBytecode(VM&, JSScope*, ExecutableType*, const SourceCode&, JSParserStrictness, DebuggerMode, ProfilerMode, ParserError&);
  228. CodeCacheMap m_sourceCode;
  229. };
  230. }
  231. #endif // CodeCache_h