compilation-cache.h 5.16 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_OBJECTS_COMPILATION_CACHE_H_
#define V8_OBJECTS_COMPILATION_CACHE_H_

8
#include "src/objects/feedback-cell.h"
9
#include "src/objects/hash-table.h"
10
#include "src/objects/js-regexp.h"
11
#include "src/objects/shared-function-info.h"
12
#include "src/roots/roots.h"
13 14 15 16 17 18 19

// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace v8 {
namespace internal {

20
class CompilationCacheShape : public BaseShape<HashTableKey*> {
21
 public:
22
  static inline bool IsMatch(HashTableKey* key, Object value) {
23 24 25
    return key->IsMatch(value);
  }

26
  static inline uint32_t Hash(ReadOnlyRoots roots, HashTableKey* key) {
27 28
    return key->Hash();
  }
29

30
  static inline uint32_t RegExpHash(String string, Smi flags);
31

32
  static inline uint32_t StringSharedHash(String source,
33
                                          SharedFunctionInfo shared,
34 35 36
                                          LanguageMode language_mode,
                                          int position);

37
  static inline uint32_t HashForObject(ReadOnlyRoots roots, Object object);
38 39 40

  static const int kPrefixSize = 0;
  static const int kEntrySize = 3;
41
  static const bool kMatchNeedsHoleCheck = true;
42 43
};

44
class InfoCellPair {
45
 public:
46
  InfoCellPair() = default;
47 48
  inline InfoCellPair(Isolate* isolate, SharedFunctionInfo shared,
                      FeedbackCell feedback_cell);
49

50
  FeedbackCell feedback_cell() const {
51 52 53 54 55 56 57
    DCHECK(is_compiled_scope_.is_compiled());
    return feedback_cell_;
  }
  SharedFunctionInfo shared() const {
    DCHECK(is_compiled_scope_.is_compiled());
    return shared_;
  }
58

59
  bool has_feedback_cell() const {
60
    return !feedback_cell_.is_null() && is_compiled_scope_.is_compiled();
61 62 63 64 65 66 67
  }
  bool has_shared() const {
    // Only return true if SFI is compiled - the bytecode could have been
    // flushed while it's in the compilation cache, and not yet have been
    // removed form the compilation cache.
    return !shared_.is_null() && is_compiled_scope_.is_compiled();
  }
68 69

 private:
70
  IsCompiledScope is_compiled_scope_;
71
  SharedFunctionInfo shared_;
72
  FeedbackCell feedback_cell_;
73 74
};

75 76
EXTERN_DECLARE_HASH_TABLE(CompilationCacheTable, CompilationCacheShape)

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
// This cache is used in multiple different variants.
//
// For regexp caching, it simply maps identifying info of the regexp
// to the cached regexp object.
//
// Scripts and eval code only gets cached after a second probe for the
// code object. To do so, on first "put" only a hash identifying the
// source is entered into the cache, mapping it to a lifetime count of
// the hash. On each call to Age all such lifetimes get reduced, and
// removed once they reach zero. If a second put is called while such
// a hash is live in the cache, the hash gets replaced by an actual
// cache entry. Age also removes stale live entries from the cache.
// Such entries are identified by SharedFunctionInfos pointing to
// either the recompilation stub, or to "old" code. This avoids memory
// leaks due to premature caching of scripts and eval strings that are
// never needed later.
93
class CompilationCacheTable
94
    : public HashTable<CompilationCacheTable, CompilationCacheShape> {
95
 public:
96
  NEVER_READ_ONLY_SPACE
97 98 99 100 101 102 103 104
  static MaybeHandle<SharedFunctionInfo> LookupScript(
      Handle<CompilationCacheTable> table, Handle<String> src,
      Handle<Context> native_context, LanguageMode language_mode);
  static InfoCellPair LookupEval(Handle<CompilationCacheTable> table,
                                 Handle<String> src,
                                 Handle<SharedFunctionInfo> shared,
                                 Handle<Context> native_context,
                                 LanguageMode language_mode, int position);
105
  Handle<Object> LookupRegExp(Handle<String> source, JSRegExp::Flags flags);
106 107
  MaybeHandle<Code> LookupCode(Handle<SharedFunctionInfo> key);

108 109
  static Handle<CompilationCacheTable> PutScript(
      Handle<CompilationCacheTable> cache, Handle<String> src,
110
      Handle<Context> native_context, LanguageMode language_mode,
111
      Handle<SharedFunctionInfo> value);
112 113 114
  static Handle<CompilationCacheTable> PutEval(
      Handle<CompilationCacheTable> cache, Handle<String> src,
      Handle<SharedFunctionInfo> outer_info, Handle<SharedFunctionInfo> value,
115 116
      Handle<Context> native_context, Handle<FeedbackCell> feedback_cell,
      int position);
117
  static Handle<CompilationCacheTable> PutRegExp(
118
      Isolate* isolate, Handle<CompilationCacheTable> cache, Handle<String> src,
119
      JSRegExp::Flags flags, Handle<FixedArray> value);
120 121 122
  static Handle<CompilationCacheTable> PutCode(
      Isolate* isolate, Handle<CompilationCacheTable> cache,
      Handle<SharedFunctionInfo> key, Handle<Code> value);
123
  void Remove(Object value);
124 125 126
  void Age();
  static const int kHashGenerations = 10;

127
  DECL_CAST(CompilationCacheTable)
128 129

 private:
130 131
  OBJECT_CONSTRUCTORS(CompilationCacheTable,
                      HashTable<CompilationCacheTable, CompilationCacheShape>);
132 133 134 135 136 137 138 139
};

}  // namespace internal
}  // namespace v8

#include "src/objects/object-macros-undef.h"

#endif  // V8_OBJECTS_COMPILATION_CACHE_H_