Line data Source code
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_INL_H_
6 : #define V8_OBJECTS_COMPILATION_CACHE_INL_H_
7 :
8 : #include "src/objects/compilation-cache.h"
9 :
10 : #include "src/objects/name-inl.h"
11 : #include "src/objects/script-inl.h"
12 : #include "src/objects/shared-function-info.h"
13 : #include "src/objects/smi.h"
14 : #include "src/objects/string.h"
15 :
16 : // Has to be the last include (doesn't have include guards):
17 : #include "src/objects/object-macros.h"
18 :
19 : namespace v8 {
20 : namespace internal {
21 :
22 6169345 : CompilationCacheTable::CompilationCacheTable(Address ptr)
23 : : HashTable<CompilationCacheTable, CompilationCacheShape>(ptr) {
24 : SLOW_DCHECK(IsCompilationCacheTable());
25 6169345 : }
26 :
27 : NEVER_READ_ONLY_SPACE_IMPL(CompilationCacheTable)
28 6169345 : CAST_ACCESSOR(CompilationCacheTable)
29 :
30 : uint32_t CompilationCacheShape::RegExpHash(String string, Smi flags) {
31 2684262 : return string->Hash() + flags->value();
32 : }
33 :
34 5265446 : uint32_t CompilationCacheShape::StringSharedHash(String source,
35 : SharedFunctionInfo shared,
36 : LanguageMode language_mode,
37 : int position) {
38 5265446 : uint32_t hash = source->Hash();
39 5265446 : if (shared->HasSourceCode()) {
40 : // Instead of using the SharedFunctionInfo pointer in the hash
41 : // code computation, we use a combination of the hash of the
42 : // script source code and the start position of the calling scope.
43 : // We do this to ensure that the cache entries can survive garbage
44 : // collection.
45 5265446 : Script script(Script::cast(shared->script()));
46 5265446 : hash ^= String::cast(script->source())->Hash();
47 : STATIC_ASSERT(LanguageModeSize == 2);
48 5265446 : if (is_strict(language_mode)) hash ^= 0x8000;
49 5265446 : hash += position;
50 : }
51 5265446 : return hash;
52 : }
53 :
54 1198743 : uint32_t CompilationCacheShape::HashForObject(Isolate* isolate, Object object) {
55 1198743 : if (object->IsNumber()) return static_cast<uint32_t>(object->Number());
56 :
57 508201 : FixedArray val = FixedArray::cast(object);
58 1016402 : if (val->map() == val->GetReadOnlyRoots().fixed_cow_array_map()) {
59 : DCHECK_EQ(4, val->length());
60 151010 : SharedFunctionInfo shared = SharedFunctionInfo::cast(val->get(0));
61 151010 : String source = String::cast(val->get(1));
62 151010 : int language_unchecked = Smi::ToInt(val->get(2));
63 : DCHECK(is_valid_language_mode(language_unchecked));
64 151010 : LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked);
65 151010 : int position = Smi::ToInt(val->get(3));
66 151010 : return StringSharedHash(source, shared, language_mode, position);
67 : }
68 : DCHECK_LT(2, val->length());
69 : return RegExpHash(String::cast(val->get(JSRegExp::kSourceIndex)),
70 357191 : Smi::cast(val->get(JSRegExp::kFlagsIndex)));
71 : }
72 :
73 2599921 : InfoCellPair::InfoCellPair(SharedFunctionInfo shared,
74 : FeedbackCell feedback_cell)
75 : : is_compiled_scope_(!shared.is_null() ? shared->is_compiled_scope()
76 : : IsCompiledScope()),
77 : shared_(shared),
78 5199842 : feedback_cell_(feedback_cell) {}
79 :
80 : } // namespace internal
81 : } // namespace v8
82 :
83 : #include "src/objects/object-macros-undef.h"
84 :
85 : #endif // V8_OBJECTS_COMPILATION_CACHE_INL_H_
|