1 | /* |
2 | * Copyright (C) 2016 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 | |
26 | #pragma once |
27 | |
28 | #include "JSObject.h" |
29 | #include "PropertySlot.h" |
30 | #include "Structure.h" |
31 | |
32 | namespace JSC { |
33 | |
34 | class HasOwnPropertyCache { |
35 | static const uint32_t size = 2 * 1024; |
36 | static_assert(hasOneBitSet(size), "size should be a power of two." ); |
37 | public: |
38 | static const uint32_t mask = size - 1; |
39 | |
40 | struct Entry { |
41 | static ptrdiff_t offsetOfStructureID() { return OBJECT_OFFSETOF(Entry, structureID); } |
42 | static ptrdiff_t offsetOfImpl() { return OBJECT_OFFSETOF(Entry, impl); } |
43 | static ptrdiff_t offsetOfResult() { return OBJECT_OFFSETOF(Entry, result); } |
44 | |
45 | RefPtr<UniquedStringImpl> impl; |
46 | StructureID structureID { 0 }; |
47 | bool result { false }; |
48 | }; |
49 | |
50 | HasOwnPropertyCache() = delete; |
51 | |
52 | void operator delete(void* cache) |
53 | { |
54 | static_cast<HasOwnPropertyCache*>(cache)->clear(); |
55 | fastFree(cache); |
56 | } |
57 | |
58 | static HasOwnPropertyCache* create() |
59 | { |
60 | size_t allocationSize = sizeof(Entry) * size; |
61 | HasOwnPropertyCache* result = static_cast<HasOwnPropertyCache*>(fastMalloc(allocationSize)); |
62 | result->clearBuffer(); |
63 | return result; |
64 | } |
65 | |
66 | ALWAYS_INLINE static uint32_t hash(StructureID structureID, UniquedStringImpl* impl) |
67 | { |
68 | return bitwise_cast<uint32_t>(structureID) + impl->hash(); |
69 | } |
70 | |
71 | ALWAYS_INLINE Optional<bool> get(Structure* structure, PropertyName propName) |
72 | { |
73 | UniquedStringImpl* impl = propName.uid(); |
74 | StructureID id = structure->id(); |
75 | uint32_t index = HasOwnPropertyCache::hash(id, impl) & mask; |
76 | Entry& entry = bitwise_cast<Entry*>(this)[index]; |
77 | if (entry.structureID == id && entry.impl.get() == impl) |
78 | return entry.result; |
79 | return WTF::nullopt; |
80 | } |
81 | |
82 | ALWAYS_INLINE void tryAdd(VM& vm, PropertySlot& slot, JSObject* object, PropertyName propName, bool result) |
83 | { |
84 | if (parseIndex(propName)) |
85 | return; |
86 | |
87 | if (!slot.isCacheable() && !slot.isUnset()) |
88 | return; |
89 | |
90 | if (object->type() == PureForwardingProxyType || object->type() == ImpureProxyType) |
91 | return; |
92 | |
93 | Structure* structure = object->structure(vm); |
94 | if (!structure->typeInfo().prohibitsPropertyCaching() |
95 | && structure->propertyAccessesAreCacheable() |
96 | && (!slot.isUnset() || structure->propertyAccessesAreCacheableForAbsence())) { |
97 | if (structure->isDictionary()) { |
98 | // FIXME: We should be able to flatten a dictionary object again. |
99 | // https://bugs.webkit.org/show_bug.cgi?id=163092 |
100 | return; |
101 | } |
102 | |
103 | ASSERT(!result == slot.isUnset()); |
104 | |
105 | UniquedStringImpl* impl = propName.uid(); |
106 | StructureID id = structure->id(); |
107 | uint32_t index = HasOwnPropertyCache::hash(id, impl) & mask; |
108 | bitwise_cast<Entry*>(this)[index] = Entry { RefPtr<UniquedStringImpl>(impl), id, result }; |
109 | } |
110 | } |
111 | |
112 | void clear() |
113 | { |
114 | Entry* buffer = bitwise_cast<Entry*>(this); |
115 | for (uint32_t i = 0; i < size; ++i) |
116 | buffer[i].Entry::~Entry(); |
117 | |
118 | clearBuffer(); |
119 | } |
120 | |
121 | private: |
122 | void clearBuffer() |
123 | { |
124 | Entry* buffer = bitwise_cast<Entry*>(this); |
125 | for (uint32_t i = 0; i < size; ++i) |
126 | new (&buffer[i]) Entry(); |
127 | } |
128 | }; |
129 | |
130 | ALWAYS_INLINE HasOwnPropertyCache* VM::ensureHasOwnPropertyCache() |
131 | { |
132 | if (UNLIKELY(!m_hasOwnPropertyCache)) |
133 | m_hasOwnPropertyCache = std::unique_ptr<HasOwnPropertyCache>(HasOwnPropertyCache::create()); |
134 | return m_hasOwnPropertyCache.get(); |
135 | } |
136 | |
137 | } // namespace JSC |
138 | |