Bug 20821: Cache property transitions to speed up object initialization
[WebKit-https.git] / JavaScriptCore / kjs / StructureID.cpp
1 /*
2  * Copyright (C) 2008 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 COMPUTER, 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 COMPUTER, 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 #include "config.h"
27 #include "StructureID.h"
28
29 #include "identifier.h"
30 #include "JSObject.h"
31 #include <wtf/RefPtr.h>
32
33 using namespace std;
34
35 namespace JSC {
36
37     StructureID::StructureID(JSValue* prototype, JSType type)
38     : m_isDictionary(false)
39     , m_type(type)
40     , m_prototype(prototype)
41     , m_cachedPrototypeChain(0)
42     , m_previous(0)
43     , m_nameInPrevious(0)
44     , m_transitionCount(0)
45 {
46     ASSERT(m_prototype);
47     ASSERT(m_prototype->isObject() || m_prototype->isNull());
48 }
49
50 void StructureID::transitionTo(StructureID* oldStructureID, StructureID* newStructureID, JSObject* slotBase)
51 {
52     if (!slotBase->usingInlineStorage() && oldStructureID->m_propertyMap.size() != newStructureID->m_propertyMap.size())
53         slotBase->allocatePropertyStorage(oldStructureID->m_propertyMap.size(), newStructureID->m_propertyMap.size());
54 }
55
56 PassRefPtr<StructureID> StructureID::addPropertyTransition(StructureID* structureID, const Identifier& propertyName, JSValue* value, unsigned attributes, JSObject* slotBase, PutPropertySlot& slot, PropertyStorage& propertyStorage)
57 {
58     ASSERT(!structureID->m_isDictionary);
59     ASSERT(structureID->m_type == ObjectType);
60
61     if (StructureID* existingTransition = structureID->m_transitionTable.get(make_pair(propertyName.ustring().rep(), attributes))) {
62         if (!slotBase->usingInlineStorage() && structureID->m_propertyMap.size() != existingTransition->m_propertyMap.size())
63             slotBase->allocatePropertyStorage(structureID->m_propertyMap.size(), existingTransition->m_propertyMap.size());
64
65         size_t offset = existingTransition->propertyMap().getOffset(propertyName);
66         ASSERT(offset != WTF::notFound);
67         propertyStorage[offset] = value;
68         slot.setNewProperty(slotBase, offset);
69
70         return existingTransition;
71     }
72
73     if (structureID->m_transitionCount > s_maxTransitionLength) {
74         RefPtr<StructureID> transition = toDictionaryTransition(structureID);
75         transition->m_propertyMap.put(propertyName, value, attributes, false, slotBase, slot, propertyStorage);
76         return transition.release();
77     }
78
79     RefPtr<StructureID> transition = create(structureID->m_prototype);
80     transition->m_cachedPrototypeChain = structureID->m_cachedPrototypeChain;
81     transition->m_previous = structureID;
82     transition->m_nameInPrevious = propertyName.ustring().rep();
83     transition->m_attributesInPrevious = attributes;
84     transition->m_transitionCount = structureID->m_transitionCount + 1;
85     transition->m_propertyMap = structureID->m_propertyMap;
86
87     transition->m_propertyMap.put(propertyName, value, attributes, false, slotBase, slot, propertyStorage);
88
89     structureID->m_transitionTable.add(make_pair(propertyName.ustring().rep(), attributes), transition.get());
90     return transition.release();
91 }
92
93 PassRefPtr<StructureID> StructureID::toDictionaryTransition(StructureID* structureID)
94 {
95     ASSERT(!structureID->m_isDictionary);
96
97     RefPtr<StructureID> transition = create(structureID->m_prototype);
98     transition->m_isDictionary = true;
99     transition->m_propertyMap = structureID->m_propertyMap;
100     return transition.release();
101 }
102
103 PassRefPtr<StructureID> StructureID::fromDictionaryTransition(StructureID* structureID)
104 {
105     ASSERT(structureID->m_isDictionary);
106
107     // Since dictionary StructureIDs are not shared, and no opcodes specialize
108     // for them, we don't need to allocate a new StructureID when transitioning
109     // to non-dictionary status.
110     structureID->m_isDictionary = false;
111     return structureID;
112 }
113
114 PassRefPtr<StructureID> StructureID::changePrototypeTransition(StructureID* structureID, JSValue* prototype)
115 {
116     RefPtr<StructureID> transition = create(prototype);
117     transition->m_transitionCount = structureID->m_transitionCount + 1;
118     transition->m_propertyMap = structureID->m_propertyMap;
119     return transition.release();
120 }
121
122 PassRefPtr<StructureID> StructureID::getterSetterTransition(StructureID* structureID)
123 {
124     RefPtr<StructureID> transition = create(structureID->storedPrototype());
125     transition->m_transitionCount = structureID->m_transitionCount + 1;
126     transition->m_propertyMap = structureID->m_propertyMap;
127     return transition.release();
128 }
129
130 StructureID::~StructureID()
131 {
132     if (m_previous) {
133         ASSERT(m_previous->m_transitionTable.contains(make_pair(m_nameInPrevious, m_attributesInPrevious)));
134         m_previous->m_transitionTable.remove(make_pair(m_nameInPrevious, m_attributesInPrevious));
135     }
136 }
137
138 StructureIDChain::StructureIDChain(StructureID* structureID)
139 {
140     size_t size = 1;
141
142     StructureID* tmp = structureID;
143     while (!tmp->storedPrototype()->isNull()) {
144         ++size;
145         tmp = static_cast<JSCell*>(tmp->storedPrototype())->structureID();
146     }
147     
148     m_vector.set(new RefPtr<StructureID>[size + 1]);
149
150     size_t i;
151     for (i = 0; i < size - 1; ++i) {
152         m_vector[i] = structureID;
153         structureID = static_cast<JSObject*>(structureID->storedPrototype())->structureID();
154     }
155     m_vector[i] = structureID;
156     m_vector[i + 1] = 0;
157 }
158
159 } // namespace JSC