source: webkit/trunk/JavaScriptCore/kjs/StructureID.cpp@ 36847

Last change on this file since 36847 was 36789, checked in by [email protected], 17 years ago

2008-09-22 Sam Weinig <[email protected]>

Reviewed by Maciej Stachowiak.

Patch for https://bugs.webkit.org/show_bug.cgi?id=21014
Speed up for..in by using StructureID to avoid call hasProperty

Speeds up fasta by 8%.

  • VM/JSPropertyNameIterator.cpp: (JSC::JSPropertyNameIterator::invalidate):
  • VM/JSPropertyNameIterator.h: (JSC::JSPropertyNameIterator::next):
  • kjs/PropertyNameArray.h: (JSC::PropertyNameArrayData::begin): (JSC::PropertyNameArrayData::end): (JSC::PropertyNameArrayData::setCachedStructureID): (JSC::PropertyNameArrayData::cachedStructureID):
  • kjs/StructureID.cpp: (JSC::StructureID::getEnumerablePropertyNames): (JSC::structureIDChainsAreEqual):
  • kjs/StructureID.h:
File size: 8.9 KB
Line 
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 "PropertyNameArray.h"
32#include "lookup.h"
33#include <wtf/RefPtr.h>
34
35using namespace std;
36
37namespace JSC {
38
39StructureID::StructureID(JSValue* prototype, const TypeInfo& typeInfo)
40 : m_typeInfo(typeInfo)
41 , m_isDictionary(false)
42 , m_prototype(prototype)
43 , m_cachedPrototypeChain(0)
44 , m_previous(0)
45 , m_nameInPrevious(0)
46 , m_transitionCount(0)
47{
48 ASSERT(m_prototype);
49 ASSERT(m_prototype->isObject() || m_prototype->isNull());
50}
51
52void StructureID::getEnumerablePropertyNames(ExecState* exec, PropertyNameArray& propertyNames, JSObject* baseObject)
53{
54 bool shouldCache = !(propertyNames.size() || m_isDictionary);
55
56 if (shouldCache && m_cachedPropertyNameArrayData) {
57 if (structureIDChainsAreEqual(m_cachedPropertyNameArrayData->cachedPrototypeChain(), cachedPrototypeChain())) {
58 propertyNames.setData(m_cachedPropertyNameArrayData);
59 return;
60 }
61 }
62
63 m_propertyMap.getEnumerablePropertyNames(propertyNames);
64
65 // Add properties from the static hashtables of properties
66 for (const ClassInfo* info = baseObject->classInfo(); info; info = info->parentClass) {
67 const HashTable* table = info->propHashTable(exec);
68 if (!table)
69 continue;
70 table->initializeIfNeeded(exec);
71 ASSERT(table->table);
72 int hashSizeMask = table->hashSizeMask;
73 const HashEntry* entry = table->table;
74 for (int i = 0; i <= hashSizeMask; ++i, ++entry) {
75 if (entry->key && !(entry->attributes & DontEnum))
76 propertyNames.add(entry->key);
77 }
78 }
79
80 if (m_prototype->isObject())
81 static_cast<JSObject*>(m_prototype)->getPropertyNames(exec, propertyNames);
82
83 if (shouldCache) {
84 m_cachedPropertyNameArrayData = propertyNames.data();
85
86 StructureIDChain* chain = cachedPrototypeChain();
87 if (!chain)
88 chain = createCachedPrototypeChain();
89 m_cachedPropertyNameArrayData->setCachedPrototypeChain(chain);
90 m_cachedPropertyNameArrayData->setCachedStructureID(this);
91 }
92}
93
94void StructureID::clearEnumerationCache()
95{
96 m_cachedPropertyNameArrayData.clear();
97}
98
99void StructureID::transitionTo(StructureID* oldStructureID, StructureID* newStructureID, JSObject* slotBase)
100{
101 if (!slotBase->usingInlineStorage() && oldStructureID->m_propertyMap.size() != newStructureID->m_propertyMap.size())
102 slotBase->allocatePropertyStorage(oldStructureID->m_propertyMap.size(), newStructureID->m_propertyMap.size());
103}
104
105PassRefPtr<StructureID> StructureID::addPropertyTransition(StructureID* structureID, const Identifier& propertyName, JSValue* value, unsigned attributes, JSObject* slotBase, PutPropertySlot& slot, PropertyStorage& propertyStorage)
106{
107 ASSERT(!structureID->m_isDictionary);
108 ASSERT(structureID->typeInfo().type() == ObjectType);
109
110 if (StructureID* existingTransition = structureID->m_transitionTable.get(make_pair(propertyName.ustring().rep(), attributes))) {
111 if (!slotBase->usingInlineStorage() && structureID->m_propertyMap.size() != existingTransition->m_propertyMap.size())
112 slotBase->allocatePropertyStorage(structureID->m_propertyMap.size(), existingTransition->m_propertyMap.size());
113
114 size_t offset = existingTransition->propertyMap().getOffset(propertyName);
115 ASSERT(offset != WTF::notFound);
116 propertyStorage[offset] = value;
117 slot.setNewProperty(slotBase, offset);
118
119 return existingTransition;
120 }
121
122 if (structureID->m_transitionCount > s_maxTransitionLength) {
123 RefPtr<StructureID> transition = toDictionaryTransition(structureID);
124 transition->m_propertyMap.put(propertyName, value, attributes, false, slotBase, slot, propertyStorage);
125 return transition.release();
126 }
127
128 RefPtr<StructureID> transition = create(structureID->m_prototype, structureID->typeInfo());
129 transition->m_cachedPrototypeChain = structureID->m_cachedPrototypeChain;
130 transition->m_previous = structureID;
131 transition->m_nameInPrevious = propertyName.ustring().rep();
132 transition->m_attributesInPrevious = attributes;
133 transition->m_transitionCount = structureID->m_transitionCount + 1;
134 transition->m_propertyMap = structureID->m_propertyMap;
135
136 transition->m_propertyMap.put(propertyName, value, attributes, false, slotBase, slot, propertyStorage);
137
138 structureID->m_transitionTable.add(make_pair(propertyName.ustring().rep(), attributes), transition.get());
139 return transition.release();
140}
141
142PassRefPtr<StructureID> StructureID::toDictionaryTransition(StructureID* structureID)
143{
144 ASSERT(!structureID->m_isDictionary);
145
146 RefPtr<StructureID> transition = create(structureID->m_prototype, structureID->typeInfo());
147 transition->m_isDictionary = true;
148 transition->m_propertyMap = structureID->m_propertyMap;
149 return transition.release();
150}
151
152PassRefPtr<StructureID> StructureID::fromDictionaryTransition(StructureID* structureID)
153{
154 ASSERT(structureID->m_isDictionary);
155
156 // Since dictionary StructureIDs are not shared, and no opcodes specialize
157 // for them, we don't need to allocate a new StructureID when transitioning
158 // to non-dictionary status.
159 structureID->m_isDictionary = false;
160 return structureID;
161}
162
163PassRefPtr<StructureID> StructureID::changePrototypeTransition(StructureID* structureID, JSValue* prototype)
164{
165 RefPtr<StructureID> transition = create(prototype, structureID->typeInfo());
166 transition->m_transitionCount = structureID->m_transitionCount + 1;
167 transition->m_propertyMap = structureID->m_propertyMap;
168 return transition.release();
169}
170
171PassRefPtr<StructureID> StructureID::getterSetterTransition(StructureID* structureID)
172{
173 RefPtr<StructureID> transition = create(structureID->storedPrototype(), structureID->typeInfo());
174 transition->m_transitionCount = structureID->m_transitionCount + 1;
175 transition->m_propertyMap = structureID->m_propertyMap;
176 return transition.release();
177}
178
179StructureID::~StructureID()
180{
181 if (m_previous) {
182 ASSERT(m_previous->m_transitionTable.contains(make_pair(m_nameInPrevious, m_attributesInPrevious)));
183 m_previous->m_transitionTable.remove(make_pair(m_nameInPrevious, m_attributesInPrevious));
184 }
185}
186
187StructureIDChain* StructureID::createCachedPrototypeChain()
188{
189 ASSERT(typeInfo().type() == ObjectType);
190 ASSERT(!m_cachedPrototypeChain);
191
192 JSValue* prototype = storedPrototype();
193 if (JSImmediate::isImmediate(prototype))
194 return 0;
195
196 RefPtr<StructureIDChain> chain = StructureIDChain::create(static_cast<JSObject*>(prototype)->structureID());
197 setCachedPrototypeChain(chain.release());
198 return cachedPrototypeChain();
199}
200
201StructureIDChain::StructureIDChain(StructureID* structureID)
202{
203 size_t size = 1;
204
205 StructureID* tmp = structureID;
206 while (!tmp->storedPrototype()->isNull()) {
207 ++size;
208 tmp = static_cast<JSCell*>(tmp->storedPrototype())->structureID();
209 }
210
211 m_vector.set(new RefPtr<StructureID>[size + 1]);
212
213 size_t i;
214 for (i = 0; i < size - 1; ++i) {
215 m_vector[i] = structureID;
216 structureID = static_cast<JSObject*>(structureID->storedPrototype())->structureID();
217 }
218 m_vector[i] = structureID;
219 m_vector[i + 1] = 0;
220}
221
222bool structureIDChainsAreEqual(StructureIDChain* chainA, StructureIDChain* chainB)
223{
224 if (!chainA || !chainB)
225 return false;
226
227 RefPtr<StructureID>* a = chainA->head();
228 RefPtr<StructureID>* b = chainB->head();
229 while (1) {
230 if (*a != *b)
231 return false;
232 if (!*a)
233 return true;
234 a++;
235 b++;
236 }
237}
238
239} // namespace JSC
Note: See TracBrowser for help on using the repository browser.