]> git.saurik.com Git - apple/javascriptcore.git/blame - bytecode/ObjectAllocationProfile.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / bytecode / ObjectAllocationProfile.h
CommitLineData
93a37866
A
1/*
2 * Copyright (C) 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
26#ifndef ObjectAllocationProfile_h
27#define ObjectAllocationProfile_h
28
29#include "VM.h"
30#include "JSGlobalObject.h"
31#include "ObjectPrototype.h"
32#include "SlotVisitor.h"
33#include "WriteBarrier.h"
34
35namespace JSC {
36
37class ObjectAllocationProfile {
38 friend class LLIntOffsetsExtractor;
39public:
40 static ptrdiff_t offsetOfAllocator() { return OBJECT_OFFSETOF(ObjectAllocationProfile, m_allocator); }
41 static ptrdiff_t offsetOfStructure() { return OBJECT_OFFSETOF(ObjectAllocationProfile, m_structure); }
42
43 ObjectAllocationProfile()
44 : m_allocator(0)
45 {
46 }
47
48 bool isNull() { return !m_allocator; }
49
50 void initialize(VM& vm, JSCell* owner, JSObject* prototype, unsigned inferredInlineCapacity)
51 {
52 ASSERT(!m_allocator);
53 ASSERT(!m_structure);
54
55 unsigned inlineCapacity = 0;
56 if (inferredInlineCapacity < JSFinalObject::defaultInlineCapacity()) {
57 // Try to shrink the object based on static analysis.
58 inferredInlineCapacity += possibleDefaultPropertyCount(vm, prototype);
59
60 if (!inferredInlineCapacity) {
61 // Empty objects are rare, so most likely the static analyzer just didn't
62 // see the real initializer function. This can happen with helper functions.
63 inferredInlineCapacity = JSFinalObject::defaultInlineCapacity();
64 } else if (inferredInlineCapacity > JSFinalObject::defaultInlineCapacity()) {
65 // Default properties are weak guesses, so don't allow them to turn a small
66 // object into a large object.
67 inferredInlineCapacity = JSFinalObject::defaultInlineCapacity();
68 }
69
70 inlineCapacity = inferredInlineCapacity;
71 ASSERT(inlineCapacity < JSFinalObject::maxInlineCapacity());
72 } else {
73 // Normal or large object.
74 inlineCapacity = inferredInlineCapacity;
75 if (inlineCapacity > JSFinalObject::maxInlineCapacity())
76 inlineCapacity = JSFinalObject::maxInlineCapacity();
77 }
78
79 ASSERT(inlineCapacity > 0);
80 ASSERT(inlineCapacity <= JSFinalObject::maxInlineCapacity());
81
82 size_t allocationSize = JSFinalObject::allocationSize(inlineCapacity);
83 MarkedAllocator* allocator = &vm.heap.allocatorForObjectWithoutDestructor(allocationSize);
84 ASSERT(allocator->cellSize());
85
86 // Take advantage of extra inline capacity available in the size class.
87 size_t slop = (allocator->cellSize() - allocationSize) / sizeof(WriteBarrier<Unknown>);
88 inlineCapacity += slop;
89 if (inlineCapacity > JSFinalObject::maxInlineCapacity())
90 inlineCapacity = JSFinalObject::maxInlineCapacity();
91
ed1e77d3
A
92 Structure* structure = vm.prototypeMap.emptyObjectStructureForPrototype(prototype, inlineCapacity);
93
94 // Ensure that if another thread sees the structure, it will see it properly created
95 WTF::storeStoreFence();
96
93a37866 97 m_allocator = allocator;
ed1e77d3 98 m_structure.set(vm, owner, structure);
93a37866
A
99 }
100
ed1e77d3
A
101 Structure* structure()
102 {
103 Structure* structure = m_structure.get();
104 // Ensure that if we see the structure, it has been properly created
105 WTF::loadLoadFence();
106 return structure;
107 }
108 unsigned inlineCapacity() { return structure()->inlineCapacity(); }
93a37866
A
109
110 void clear()
111 {
112 m_allocator = 0;
113 m_structure.clear();
114 ASSERT(isNull());
115 }
116
117 void visitAggregate(SlotVisitor& visitor)
118 {
119 visitor.append(&m_structure);
120 }
121
122private:
123
124 unsigned possibleDefaultPropertyCount(VM& vm, JSObject* prototype)
125 {
126 if (prototype == prototype->globalObject()->objectPrototype())
127 return 0;
128
129 size_t count = 0;
130 PropertyNameArray propertyNameArray(&vm);
ed1e77d3 131 prototype->structure()->getPropertyNamesFromStructure(vm, propertyNameArray, EnumerationMode());
93a37866
A
132 PropertyNameArrayData::PropertyNameVector& propertyNameVector = propertyNameArray.data()->propertyNameVector();
133 for (size_t i = 0; i < propertyNameVector.size(); ++i) {
134 JSValue value = prototype->getDirect(vm, propertyNameVector[i]);
135
136 // Functions are common, and are usually class-level objects that are not overridden.
137 if (jsDynamicCast<JSFunction*>(value))
138 continue;
139
140 ++count;
141
142 }
143 return count;
144 }
145
146 MarkedAllocator* m_allocator; // Precomputed to make things easier for generated code.
147 WriteBarrier<Structure> m_structure;
148};
149
150} // namespace JSC
151
152#endif // ObjectAllocationProfile_h