2 * This file is part of the KDE libraries
3 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
23 #include "property_map.h"
27 #include "PropertyNameArray.h"
29 #include <wtf/FastMalloc.h>
30 #include <wtf/Vector.h>
34 #define DEBUG_PROPERTIES 0
35 #define DO_CONSISTENCY_CHECK 0
36 #define DUMP_STATISTICS 0
37 #define USE_SINGLE_ENTRY 1
39 // 2/28/2006 ggaren: super accurate JS iBench says that USE_SINGLE_ENTRY is a
40 // 3.2% performance boost.
42 // FIXME: _singleEntry.index is unused.
44 #if !DO_CONSISTENCY_CHECK
45 #define checkConsistency() ((void)0)
50 // Choose a number for the following so that most property maps are smaller,
51 // but it's not going to blow out the stack to allocate this number of pointers.
52 const int smallMapThreshold = 1024;
57 static int numCollisions;
58 static int numRehashes;
59 static int numRemoves;
61 struct PropertyMapStatisticsExitLogger { ~PropertyMapStatisticsExitLogger(); };
63 static PropertyMapStatisticsExitLogger logger;
65 PropertyMapStatisticsExitLogger::~PropertyMapStatisticsExitLogger()
67 printf("\nKJS::PropertyMap statistics\n\n");
68 printf("%d probes\n", numProbes);
69 printf("%d collisions (%.1f%%)\n", numCollisions, 100.0 * numCollisions / numProbes);
70 printf("%d rehashes\n", numRehashes);
71 printf("%d removes\n", numRemoves);
76 // lastIndexUsed is an ever-increasing index used to identify the order items
77 // were inserted into the property map. It's vital that getEnumerablePropertyNames
78 // return the properties in the order they were added for compatibility with other
79 // browsers' JavaScript implementations.
80 struct PropertyMapHashTable
87 PropertyMapHashTableEntry entries[1];
93 ProtectedPtr<JSValue> value;
97 SavedProperties::SavedProperties() : _count(0) { }
98 SavedProperties::~SavedProperties() { }
100 // Algorithm concepts from Algorithms in C++, Sedgewick.
102 // This is a method rather than a variable to work around <rdar://problem/4462053>
103 static inline UString::Rep* deletedSentinel() { return reinterpret_cast<UString::Rep*>(0x1); }
105 // Returns true if the key is not null or the deleted sentinel, false otherwise
106 static inline bool isValid(UString::Rep* key)
108 return reinterpret_cast<uintptr_t>(key) & ~0x1;
111 PropertyMap::~PropertyMap()
115 UString::Rep *key = _singleEntry.key;
122 int minimumKeysToProcess = _table->keyCount + _table->sentinelCount;
123 Entry *entries = _table->entries;
124 for (int i = 0; i < minimumKeysToProcess; i++) {
125 UString::Rep *key = entries[i].key;
127 if (key != deletedSentinel())
130 ++minimumKeysToProcess;
135 void PropertyMap::clear()
139 UString::Rep *key = _singleEntry.key;
142 _singleEntry.key = 0;
148 int size = _table->size;
149 Entry *entries = _table->entries;
150 for (int i = 0; i < size; i++) {
151 UString::Rep *key = entries[i].key;
155 entries[i].value = 0;
158 _table->keyCount = 0;
159 _table->sentinelCount = 0;
162 JSValue *PropertyMap::get(const Identifier &name, unsigned &attributes) const
164 assert(!name.isNull());
166 UString::Rep *rep = name._ustring.rep();
170 UString::Rep *key = _singleEntry.key;
172 attributes = _singleEntry.attributes;
173 return _singleEntry.value;
179 unsigned h = rep->hash();
180 int sizeMask = _table->sizeMask;
181 Entry *entries = _table->entries;
182 int i = h & sizeMask;
186 numCollisions += entries[i].key && entries[i].key != rep;
188 while (UString::Rep *key = entries[i].key) {
190 attributes = entries[i].attributes;
191 return entries[i].value;
194 k = 1 | (h % sizeMask);
195 i = (i + k) & sizeMask;
203 JSValue *PropertyMap::get(const Identifier &name) const
205 assert(!name.isNull());
207 UString::Rep *rep = name._ustring.rep();
211 UString::Rep *key = _singleEntry.key;
213 return _singleEntry.value;
218 unsigned h = rep->hash();
219 int sizeMask = _table->sizeMask;
220 Entry *entries = _table->entries;
221 int i = h & sizeMask;
225 numCollisions += entries[i].key && entries[i].key != rep;
227 while (UString::Rep *key = entries[i].key) {
229 return entries[i].value;
231 k = 1 | (h % sizeMask);
232 i = (i + k) & sizeMask;
240 JSValue **PropertyMap::getLocation(const Identifier &name)
242 assert(!name.isNull());
244 UString::Rep *rep = name._ustring.rep();
248 UString::Rep *key = _singleEntry.key;
250 return &_singleEntry.value;
255 unsigned h = rep->hash();
256 int sizeMask = _table->sizeMask;
257 Entry *entries = _table->entries;
258 int i = h & sizeMask;
262 numCollisions += entries[i].key && entries[i].key != rep;
264 while (UString::Rep *key = entries[i].key) {
266 return &entries[i].value;
268 k = 1 | (h % sizeMask);
269 i = (i + k) & sizeMask;
278 static void printAttributes(int attributes)
283 if (attributes & ReadOnly)
285 if (attributes & DontEnum)
287 if (attributes & DontDelete)
288 printf("DontDelete ");
289 if (attributes & Internal)
291 if (attributes & Function)
297 void PropertyMap::put(const Identifier &name, JSValue *value, int attributes, bool roCheck)
299 assert(!name.isNull());
304 UString::Rep *rep = name._ustring.rep();
307 printf("adding property %s, attributes = 0x%08x (", name.ascii(), attributes);
308 printAttributes(attributes);
314 UString::Rep *key = _singleEntry.key;
316 if (rep == key && !(roCheck && (_singleEntry.attributes & ReadOnly))) {
317 _singleEntry.value = value;
322 _singleEntry.key = rep;
323 _singleEntry.value = value;
324 _singleEntry.attributes = attributes;
331 if (!_table || _table->keyCount * 2 >= _table->size)
334 unsigned h = rep->hash();
335 int sizeMask = _table->sizeMask;
336 Entry *entries = _table->entries;
337 int i = h & sizeMask;
339 bool foundDeletedElement = false;
340 int deletedElementIndex = 0; /* initialize to make the compiler happy */
343 numCollisions += entries[i].key && entries[i].key != rep;
345 while (UString::Rep *key = entries[i].key) {
347 if (roCheck && (_table->entries[i].attributes & ReadOnly))
349 // Put a new value in an existing hash table entry.
350 entries[i].value = value;
351 // Attributes are intentionally not updated.
354 // If we find the deleted-element sentinel, remember it for use later.
355 if (key == deletedSentinel() && !foundDeletedElement) {
356 foundDeletedElement = true;
357 deletedElementIndex = i;
360 k = 1 | (h % sizeMask);
361 i = (i + k) & sizeMask;
367 // Use either the deleted element or the 0 at the end of the chain.
368 if (foundDeletedElement) {
369 i = deletedElementIndex;
370 --_table->sentinelCount;
373 // Create a new hash table entry.
375 entries[i].key = rep;
376 entries[i].value = value;
377 entries[i].attributes = attributes;
378 entries[i].index = ++_table->lastIndexUsed;
384 void PropertyMap::insert(UString::Rep *key, JSValue *value, int attributes, int index)
388 unsigned h = key->hash();
389 int sizeMask = _table->sizeMask;
390 Entry *entries = _table->entries;
391 int i = h & sizeMask;
395 numCollisions += entries[i].key && entries[i].key != key;
397 while (entries[i].key) {
398 assert(entries[i].key != deletedSentinel());
400 k = 1 | (h % sizeMask);
401 i = (i + k) & sizeMask;
407 entries[i].key = key;
408 entries[i].value = value;
409 entries[i].attributes = attributes;
410 entries[i].index = index;
413 void PropertyMap::expand()
415 Table *oldTable = _table;
416 int oldTableSize = oldTable ? oldTable->size : 0;
417 rehash(oldTableSize ? oldTableSize * 2 : 16);
420 void PropertyMap::rehash()
423 assert(_table->size);
424 rehash(_table->size);
427 void PropertyMap::rehash(int newTableSize)
431 Table *oldTable = _table;
432 int oldTableSize = oldTable ? oldTable->size : 0;
433 int oldTableKeyCount = oldTable ? oldTable->keyCount : 0;
435 _table = (Table *)fastCalloc(1, sizeof(Table) + (newTableSize - 1) * sizeof(Entry) );
436 _table->size = newTableSize;
437 _table->sizeMask = newTableSize - 1;
438 _table->keyCount = oldTableKeyCount;
441 UString::Rep *key = _singleEntry.key;
443 insert(key, _singleEntry.value, _singleEntry.attributes, 0);
444 _singleEntry.key = 0;
445 // update the count, because single entries don't count towards
446 // the table key count
448 assert(_table->keyCount == 1);
452 int lastIndexUsed = 0;
453 for (int i = 0; i != oldTableSize; ++i) {
454 Entry &entry = oldTable->entries[i];
455 UString::Rep *key = entry.key;
457 int index = entry.index;
458 lastIndexUsed = max(index, lastIndexUsed);
459 insert(key, entry.value, entry.attributes, index);
462 _table->lastIndexUsed = lastIndexUsed;
469 void PropertyMap::remove(const Identifier &name)
471 assert(!name.isNull());
475 UString::Rep *rep = name._ustring.rep();
481 key = _singleEntry.key;
484 _singleEntry.key = 0;
491 // Find the thing to remove.
492 unsigned h = rep->hash();
493 int sizeMask = _table->sizeMask;
494 Entry *entries = _table->entries;
495 int i = h & sizeMask;
500 numCollisions += entries[i].key && entries[i].key != rep;
502 while ((key = entries[i].key)) {
506 k = 1 | (h % sizeMask);
507 i = (i + k) & sizeMask;
515 // Replace this one element with the deleted sentinel. Also set value to 0 and attributes to DontEnum
516 // to help callers that iterate all keys not have to check for the sentinel.
518 key = deletedSentinel();
519 entries[i].key = key;
520 entries[i].value = 0;
521 entries[i].attributes = DontEnum;
522 assert(_table->keyCount >= 1);
524 ++_table->sentinelCount;
526 if (_table->sentinelCount * 4 >= _table->size)
532 void PropertyMap::mark() const
536 if (_singleEntry.key) {
537 JSValue *v = _singleEntry.value;
545 int minimumKeysToProcess = _table->keyCount;
546 Entry *entries = _table->entries;
547 for (int i = 0; i < minimumKeysToProcess; i++) {
548 JSValue *v = entries[i].value;
553 ++minimumKeysToProcess;
558 static int comparePropertyMapEntryIndices(const void *a, const void *b)
560 int ia = static_cast<PropertyMapHashTableEntry * const *>(a)[0]->index;
561 int ib = static_cast<PropertyMapHashTableEntry * const *>(b)[0]->index;
569 bool PropertyMap::containsGettersOrSetters() const
573 return _singleEntry.attributes & GetterSetter;
578 for (int i = 0; i != _table->size; ++i) {
579 if (_table->entries[i].attributes & GetterSetter)
586 void PropertyMap::getEnumerablePropertyNames(PropertyNameArray& propertyNames) const
590 UString::Rep *key = _singleEntry.key;
591 if (key && !(_singleEntry.attributes & DontEnum))
592 propertyNames.add(Identifier(key));
597 // Allocate a buffer to use to sort the keys.
598 Vector<Entry*, smallMapThreshold> sortedEnumerables(_table->keyCount);
600 // Get pointers to the enumerable entries in the buffer.
601 Entry** p = sortedEnumerables.data();
602 int size = _table->size;
603 Entry* entries = _table->entries;
604 for (int i = 0; i != size; ++i) {
605 Entry* e = &entries[i];
606 if (e->key && !(e->attributes & DontEnum))
610 // Sort the entries by index.
611 qsort(sortedEnumerables.data(), p - sortedEnumerables.data(), sizeof(Entry*), comparePropertyMapEntryIndices);
613 // Put the keys of the sorted entries into the list.
614 for (Entry** q = sortedEnumerables.data(); q != p; ++q)
615 propertyNames.add(Identifier(q[0]->key));
618 void PropertyMap::getSparseArrayPropertyNames(PropertyNameArray& propertyNames) const
622 UString::Rep *key = _singleEntry.key;
626 k.toUInt32(&fitsInUInt32);
628 propertyNames.add(Identifier(key));
634 int size = _table->size;
635 Entry *entries = _table->entries;
636 for (int i = 0; i != size; ++i) {
637 UString::Rep *key = entries[i].key;
641 k.toUInt32(&fitsInUInt32);
643 propertyNames.add(Identifier(key));
648 void PropertyMap::save(SavedProperties &p) const
654 if (_singleEntry.key && !(_singleEntry.attributes & (ReadOnly | Function)))
658 int size = _table->size;
659 Entry *entries = _table->entries;
660 for (int i = 0; i != size; ++i)
661 if (isValid(entries[i].key) && !(entries[i].attributes & (ReadOnly | Function)))
665 p._properties.clear();
671 p._properties.set(new SavedProperty [count]);
673 SavedProperty *prop = p._properties.get();
677 if (_singleEntry.key && !(_singleEntry.attributes & (ReadOnly | Function))) {
678 prop->key = Identifier(_singleEntry.key);
679 prop->value = _singleEntry.value;
680 prop->attributes = _singleEntry.attributes;
685 // Save in the right order so we don't lose the order.
686 // Another possibility would be to save the indices.
688 // Allocate a buffer to use to sort the keys.
689 Vector<Entry*, smallMapThreshold> sortedEntries(count);
691 // Get pointers to the entries in the buffer.
692 Entry** p = sortedEntries.data();
693 int size = _table->size;
694 Entry* entries = _table->entries;
695 for (int i = 0; i != size; ++i) {
696 Entry *e = &entries[i];
697 if (isValid(e->key) && !(e->attributes & (ReadOnly | Function)))
700 assert(p - sortedEntries.data() == count);
702 // Sort the entries by index.
703 qsort(sortedEntries.data(), p - sortedEntries.data(), sizeof(Entry*), comparePropertyMapEntryIndices);
705 // Put the sorted entries into the saved properties list.
706 for (Entry** q = sortedEntries.data(); q != p; ++q, ++prop) {
708 prop->key = Identifier(e->key);
709 prop->value = e->value;
710 prop->attributes = e->attributes;
715 void PropertyMap::restore(const SavedProperties &p)
717 for (int i = 0; i != p._count; ++i)
718 put(p._properties[i].key, p._properties[i].value, p._properties[i].attributes);
721 #if DO_CONSISTENCY_CHECK
723 void PropertyMap::checkConsistency()
729 int sentinelCount = 0;
730 for (int j = 0; j != _table->size; ++j) {
731 UString::Rep *rep = _table->entries[j].key;
734 if (rep == deletedSentinel()) {
738 unsigned h = rep->hash();
739 int i = h & _table->sizeMask;
741 while (UString::Rep *key = _table->entries[i].key) {
745 k = 1 | (h % _table->sizeMask);
746 i = (i + k) & _table->sizeMask;
751 assert(count == _table->keyCount);
752 assert(sentinelCount == _table->sentinelCount);
753 assert(_table->size >= 16);
754 assert(_table->sizeMask);
755 assert(_table->size == _table->sizeMask + 1);
758 #endif // DO_CONSISTENCY_CHECK