v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
compilation-cache-table-inl.h
Go to the documentation of this file.
1// Copyright 2017 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_OBJECTS_COMPILATION_CACHE_TABLE_INL_H_
6#define V8_OBJECTS_COMPILATION_CACHE_TABLE_INL_H_
7
9// Include the non-inl header before the rest of the headers.
10
11#include <optional>
12
16#include "src/objects/smi.h"
17#include "src/objects/string.h"
18
19// Has to be the last include (doesn't have include guards):
21
22namespace v8::internal {
23
24NEVER_READ_ONLY_SPACE_IMPL(CompilationCacheTable)
25
27 return get(EntryToIndex(entry) + 1);
28}
29
31 Tagged<Object> value,
32 WriteBarrierMode mode) {
33 set(EntryToIndex(entry) + 1, value, mode);
34}
35
37 static_assert(CompilationCacheShape::kEntrySize == 3);
38 return get(EntryToIndex(entry) + 2);
39}
40
42 Tagged<Object> value,
43 WriteBarrierMode mode) {
44 set(EntryToIndex(entry) + 2, value, mode);
45}
46
47// The key in a script cache is a WeakFixedArray containing a weak pointer to
48// the Script. The corresponding value can be either the root SharedFunctionInfo
49// or undefined. The purpose of storing the root SharedFunctionInfo as the value
50// is to keep it alive, not to save a lookup on the Script. A newly added entry
51// always contains the root SharedFunctionInfo. After the root
52// SharedFunctionInfo has aged sufficiently, it is replaced with undefined. In
53// this way, all strong references to large objects are dropped, but there is
54// still a way to get the Script if it happens to still be alive.
56 public:
62
63 ScriptCacheKey(Handle<String> source, const ScriptDetails* script_details,
64 Isolate* isolate);
66 int line_offset, int column_offset,
67 v8::ScriptOriginOptions origin_options,
68 MaybeHandle<Object> host_defined_options,
69 MaybeHandle<FixedArray> maybe_wrapped_arguments,
70 Isolate* isolate);
71
72 bool IsMatch(Tagged<Object> other) override;
73 bool MatchesScript(Tagged<Script> script);
74
77
78 static std::optional<Tagged<String>> SourceFromObject(Tagged<Object> obj) {
80 DCHECK(IsWeakFixedArray(obj));
82 DCHECK_EQ(array->length(), kEnd);
83
84 Tagged<MaybeObject> maybe_script = array->get(kWeakScript);
85 if (Tagged<HeapObject> script; maybe_script.GetHeapObjectIfWeak(&script)) {
86 Tagged<PrimitiveHeapObject> source_or_undefined =
87 Cast<Script>(script)->source();
88 // Scripts stored in the script cache should always have a source string.
89 return Cast<String>(source_or_undefined);
90 }
91
92 DCHECK(maybe_script.IsCleared());
93 return {};
94 }
95
96 private:
105};
106
108 Tagged<Smi> flags) {
109 return string->EnsureHash() + flags.value();
110}
111
114 LanguageMode language_mode,
115 int position) {
116 uint32_t hash = source->EnsureHash();
117 if (shared->HasSourceCode()) {
118 // Instead of using the SharedFunctionInfo pointer in the hash
119 // code computation, we use a combination of the hash of the
120 // script source code and the start position of the calling scope.
121 // We do this to ensure that the cache entries can survive garbage
122 // collection.
123 Tagged<Script> script(Cast<Script>(shared->script()));
124 hash ^= Cast<String>(script->source())->EnsureHash();
125 }
126 static_assert(LanguageModeSize == 2);
127 if (is_strict(language_mode)) hash ^= 0x8000;
128 hash += position;
129 return hash;
130}
131
133 Tagged<Object> object) {
134 // Eval: The key field contains the hash as a Number.
135 if (IsNumber(object))
136 return static_cast<uint32_t>(Object::NumberValue(object));
137
138 // Code: The key field contains the SFI key.
139 if (IsSharedFunctionInfo(object)) {
140 return Cast<SharedFunctionInfo>(object)->Hash();
141 }
142
143 // Script.
144 if (IsWeakFixedArray(object)) {
145 return static_cast<uint32_t>(Smi::ToInt(
146 Cast<WeakFixedArray>(object)->get(ScriptCacheKey::kHash).ToSmi()));
147 }
148
149 // RegExpData: The key field (and the value field) contains the RegExpData
150 // object.
151 if (IsRegExpDataWrapper(object)) {
153 Isolate* isolate = GetIsolateFromWritableObject(re_wrapper);
154 Tagged<RegExpData> data = re_wrapper->data(isolate);
155 return RegExpHash(data->source(), Smi::FromInt(data->flags()));
156 }
157
158 // Eval: See EvalCacheKey::ToHandle for the encoding.
160 DCHECK_EQ(val->map(), roots.fixed_cow_array_map());
161 DCHECK_EQ(4, val->length());
162 Tagged<String> source = Cast<String>(val->get(1));
163 int language_unchecked = Smi::ToInt(val->get(2));
164 DCHECK(is_valid_language_mode(language_unchecked));
165 LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked);
166 int position = Smi::ToInt(val->get(3));
167 Tagged<Object> shared = val->get(0);
168 return EvalHash(source, Cast<SharedFunctionInfo>(shared), language_mode,
169 position);
170}
171
173 Tagged<FeedbackCell> feedback_cell)
174 : is_compiled_scope_(!shared.is_null() ? shared->is_compiled_scope(isolate)
175 : IsCompiledScope()),
176 shared_(shared),
177 feedback_cell_(feedback_cell) {}
178
179} // namespace v8::internal
180
182
183#endif // V8_OBJECTS_COMPILATION_CACHE_TABLE_INL_H_
static uint32_t EvalHash(Tagged< String > source, Tagged< SharedFunctionInfo > shared, LanguageMode language_mode, int position)
static uint32_t HashForObject(ReadOnlyRoots roots, Tagged< Object > object)
static uint32_t RegExpHash(Tagged< String > string, Tagged< Smi > flags)
void SetPrimaryValueAt(InternalIndex entry, Tagged< Object > value, WriteBarrierMode mode=UPDATE_WRITE_BARRIER)
Tagged< Object > EvalFeedbackValueAt(InternalIndex entry)
void SetEvalFeedbackValueAt(InternalIndex entry, Tagged< Object > value, WriteBarrierMode mode=UPDATE_WRITE_BARRIER)
static double NumberValue(Tagged< Number > obj)
MaybeHandle< FixedArray > wrapped_arguments_
static std::optional< Tagged< String > > SourceFromObject(Tagged< Object > obj)
ScriptCacheKey(Handle< String > source, const ScriptDetails *script_details, Isolate *isolate)
DirectHandle< Object > AsHandle(Isolate *isolate, DirectHandle< SharedFunctionInfo > shared)
bool MatchesScript(Tagged< Script > script)
bool IsMatch(Tagged< Object > other) override
static constexpr int ToInt(const Tagged< Object > object)
Definition smi.h:33
static constexpr Tagged< Smi > FromInt(int value)
Definition smi.h:38
constexpr bool IsCleared() const
bool GetHeapObjectIfWeak(Tagged< HeapObject > *result) const
Tagged< SharedFunctionInfo > shared_
Definition debug.cc:1741
int position
Definition liveedit.cc:290
bool is_valid_language_mode(int language_mode)
Definition globals.h:781
bool IsNumber(Tagged< Object > obj)
V8_INLINE Isolate * GetIsolateFromWritableObject(Tagged< HeapObject > object)
bool is_strict(LanguageMode language_mode)
Definition globals.h:777
static const size_t LanguageModeSize
Definition globals.h:753
kInterpreterTrampolineOffset script
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
#define NEVER_READ_ONLY_SPACE_IMPL(Type)
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485