1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla JavaScript code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1999-2001
20 * the Initial Developer. All Rights Reserved.
23 * Brendan Eich <brendan@mozilla.org> (Original Author)
24 * Chris Waterson <waterson@netscape.com>
25 * L. David Baron <dbaron@dbaron.org>, Mozilla Corporation
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
42 * Double hashing implementation.
50 #include "jsutil.h" /* for JS_ASSERT */
53 # if defined MOZILLA_CLIENT && defined DEBUG_XXXbrendan
54 # include "nsTraceMalloc.h"
58 # define METER(x) /* nothing */
62 * The following DEBUG-only code is used to assert that calls to one of
63 * table->ops or to an enumerator do not cause re-entry into a call that
64 * can mutate the table. The recursion level is stored in additional
65 * space allocated at the end of the entry store to avoid changing
66 * JSDHashTable, which could cause issues when mixing DEBUG and
67 * non-DEBUG components.
71 #define JSDHASH_ONELINE_ASSERT JS_ASSERT
72 #define RECURSION_LEVEL(table_) (*(uint32*)(table_->entryStore + \
73 JS_DHASH_TABLE_SIZE(table_) * \
76 * Most callers that assert about the recursion level don't care about
77 * this magical value because they are asserting that mutation is
78 * allowed (and therefore the level is 0 or 1, depending on whether they
81 * Only PL_DHashTableFinish needs to allow this special value.
83 #define IMMUTABLE_RECURSION_LEVEL ((uint32)-1)
85 #define RECURSION_LEVEL_SAFE_TO_FINISH(table_) \
86 (RECURSION_LEVEL(table_) == 0 || \
87 RECURSION_LEVEL(table_) == IMMUTABLE_RECURSION_LEVEL)
89 #define ENTRY_STORE_EXTRA sizeof(uint32)
90 #define INCREMENT_RECURSION_LEVEL(table_) \
92 if (RECURSION_LEVEL(table_) != IMMUTABLE_RECURSION_LEVEL) \
93 ++RECURSION_LEVEL(table_); \
95 #define DECREMENT_RECURSION_LEVEL(table_) \
97 if (RECURSION_LEVEL(table_) != IMMUTABLE_RECURSION_LEVEL) { \
98 JSDHASH_ONELINE_ASSERT(RECURSION_LEVEL(table_) > 0); \
99 --RECURSION_LEVEL(table_); \
105 #define ENTRY_STORE_EXTRA 0
106 #define INCREMENT_RECURSION_LEVEL(table_) JS_BEGIN_MACRO JS_END_MACRO
107 #define DECREMENT_RECURSION_LEVEL(table_) JS_BEGIN_MACRO JS_END_MACRO
109 #endif /* defined(DEBUG) */
111 JS_PUBLIC_API(void *)
112 JS_DHashAllocTable(JSDHashTable
*table
, uint32 nbytes
)
114 return js_malloc(nbytes
);
118 JS_DHashFreeTable(JSDHashTable
*table
, void *ptr
)
123 JS_PUBLIC_API(JSDHashNumber
)
124 JS_DHashStringKey(JSDHashTable
*table
, const void *key
)
127 const unsigned char *s
;
130 for (s
= (const unsigned char *) key
; *s
!= '\0'; s
++)
131 h
= JS_ROTATE_LEFT32(h
, 4) ^ *s
;
135 JS_PUBLIC_API(JSDHashNumber
)
136 JS_DHashVoidPtrKeyStub(JSDHashTable
*table
, const void *key
)
138 return (JSDHashNumber
)(uintptr_t)key
>> 2;
141 JS_PUBLIC_API(JSBool
)
142 JS_DHashMatchEntryStub(JSDHashTable
*table
,
143 const JSDHashEntryHdr
*entry
,
146 const JSDHashEntryStub
*stub
= (const JSDHashEntryStub
*)entry
;
148 return stub
->key
== key
;
151 JS_PUBLIC_API(JSBool
)
152 JS_DHashMatchStringKey(JSDHashTable
*table
,
153 const JSDHashEntryHdr
*entry
,
156 const JSDHashEntryStub
*stub
= (const JSDHashEntryStub
*)entry
;
158 /* XXX tolerate null keys on account of sloppy Mozilla callers. */
159 return stub
->key
== key
||
161 strcmp((const char *) stub
->key
, (const char *) key
) == 0);
165 JS_DHashMoveEntryStub(JSDHashTable
*table
,
166 const JSDHashEntryHdr
*from
,
169 memcpy(to
, from
, table
->entrySize
);
173 JS_DHashClearEntryStub(JSDHashTable
*table
, JSDHashEntryHdr
*entry
)
175 memset(entry
, 0, table
->entrySize
);
179 JS_DHashFreeStringKey(JSDHashTable
*table
, JSDHashEntryHdr
*entry
)
181 const JSDHashEntryStub
*stub
= (const JSDHashEntryStub
*)entry
;
183 js_free((void *) stub
->key
);
184 memset(entry
, 0, table
->entrySize
);
188 JS_DHashFinalizeStub(JSDHashTable
*table
)
192 static const JSDHashTableOps stub_ops
= {
195 JS_DHashVoidPtrKeyStub
,
196 JS_DHashMatchEntryStub
,
197 JS_DHashMoveEntryStub
,
198 JS_DHashClearEntryStub
,
199 JS_DHashFinalizeStub
,
203 JS_PUBLIC_API(const JSDHashTableOps
*)
204 JS_DHashGetStubOps(void)
209 JS_PUBLIC_API(JSDHashTable
*)
210 JS_NewDHashTable(const JSDHashTableOps
*ops
, void *data
, uint32 entrySize
,
215 table
= (JSDHashTable
*) js_malloc(sizeof *table
);
218 if (!JS_DHashTableInit(table
, ops
, data
, entrySize
, capacity
)) {
226 JS_DHashTableDestroy(JSDHashTable
*table
)
228 JS_DHashTableFinish(table
);
232 JS_PUBLIC_API(JSBool
)
233 JS_DHashTableInit(JSDHashTable
*table
, const JSDHashTableOps
*ops
, void *data
,
234 uint32 entrySize
, uint32 capacity
)
240 if (entrySize
> 10 * sizeof(void *)) {
242 "jsdhash: for the table at address %p, the given entrySize"
243 " of %lu %s favors chaining over double hashing.\n",
245 (unsigned long) entrySize
,
246 (entrySize
> 16 * sizeof(void*)) ? "definitely" : "probably");
252 if (capacity
< JS_DHASH_MIN_SIZE
)
253 capacity
= JS_DHASH_MIN_SIZE
;
255 JS_CEILING_LOG2(log2
, capacity
);
257 capacity
= JS_BIT(log2
);
258 if (capacity
>= JS_DHASH_SIZE_LIMIT
)
260 table
->hashShift
= JS_DHASH_BITS
- log2
;
261 table
->maxAlphaFrac
= (uint8
)(0x100 * JS_DHASH_DEFAULT_MAX_ALPHA
);
262 table
->minAlphaFrac
= (uint8
)(0x100 * JS_DHASH_DEFAULT_MIN_ALPHA
);
263 table
->entrySize
= entrySize
;
264 table
->entryCount
= table
->removedCount
= 0;
265 table
->generation
= 0;
266 nbytes
= capacity
* entrySize
;
268 table
->entryStore
= (char *) ops
->allocTable(table
,
269 nbytes
+ ENTRY_STORE_EXTRA
);
270 if (!table
->entryStore
)
272 memset(table
->entryStore
, 0, nbytes
);
273 METER(memset(&table
->stats
, 0, sizeof table
->stats
));
276 RECURSION_LEVEL(table
) = 0;
283 * Compute max and min load numbers (entry counts) from table params.
285 #define MAX_LOAD(table, size) (((table)->maxAlphaFrac * (size)) >> 8)
286 #define MIN_LOAD(table, size) (((table)->minAlphaFrac * (size)) >> 8)
289 JS_DHashTableSetAlphaBounds(JSDHashTable
*table
,
296 * Reject obviously insane bounds, rather than trying to guess what the
297 * buggy caller intended.
299 JS_ASSERT(0.5 <= maxAlpha
&& maxAlpha
< 1 && 0 <= minAlpha
);
300 if (maxAlpha
< 0.5 || 1 <= maxAlpha
|| minAlpha
< 0)
304 * Ensure that at least one entry will always be free. If maxAlpha at
305 * minimum size leaves no entries free, reduce maxAlpha based on minimum
306 * size and the precision limit of maxAlphaFrac's fixed point format.
308 JS_ASSERT(JS_DHASH_MIN_SIZE
- (maxAlpha
* JS_DHASH_MIN_SIZE
) >= 1);
309 if (JS_DHASH_MIN_SIZE
- (maxAlpha
* JS_DHASH_MIN_SIZE
) < 1) {
311 (JS_DHASH_MIN_SIZE
- JS_MAX(JS_DHASH_MIN_SIZE
/ 256, 1))
316 * Ensure that minAlpha is strictly less than half maxAlpha. Take care
317 * not to truncate an entry's worth of alpha when storing in minAlphaFrac
318 * (8-bit fixed point format).
320 JS_ASSERT(minAlpha
< maxAlpha
/ 2);
321 if (minAlpha
>= maxAlpha
/ 2) {
322 size
= JS_DHASH_TABLE_SIZE(table
);
323 minAlpha
= (size
* maxAlpha
- JS_MAX(size
/ 256, 1)) / (2 * size
);
326 table
->maxAlphaFrac
= (uint8
)(maxAlpha
* 256);
327 table
->minAlphaFrac
= (uint8
)(minAlpha
* 256);
331 * Double hashing needs the second hash code to be relatively prime to table
332 * size, so we simply make hash2 odd.
334 #define HASH1(hash0, shift) ((hash0) >> (shift))
335 #define HASH2(hash0,log2,shift) ((((hash0) << (log2)) >> (shift)) | 1)
338 * Reserve keyHash 0 for free entries and 1 for removed-entry sentinels. Note
339 * that a removed-entry sentinel need be stored only if the removed entry had
340 * a colliding entry added after it. Therefore we can use 1 as the collision
341 * flag in addition to the removed-entry sentinel value. Multiplicative hash
342 * uses the high order bits of keyHash, so this least-significant reservation
343 * should not hurt the hash function's effectiveness much.
345 * If you change any of these magic numbers, also update JS_DHASH_ENTRY_IS_LIVE
346 * in jsdhash.h. It used to be private to jsdhash.c, but then became public to
347 * assist iterator writers who inspect table->entryStore directly.
349 #define COLLISION_FLAG ((JSDHashNumber) 1)
350 #define MARK_ENTRY_FREE(entry) ((entry)->keyHash = 0)
351 #define MARK_ENTRY_REMOVED(entry) ((entry)->keyHash = 1)
352 #define ENTRY_IS_REMOVED(entry) ((entry)->keyHash == 1)
353 #define ENTRY_IS_LIVE(entry) JS_DHASH_ENTRY_IS_LIVE(entry)
354 #define ENSURE_LIVE_KEYHASH(hash0) if (hash0 < 2) hash0 -= 2; else (void)0
356 /* Match an entry's keyHash against an unstored one computed from a key. */
357 #define MATCH_ENTRY_KEYHASH(entry,hash0) \
358 (((entry)->keyHash & ~COLLISION_FLAG) == (hash0))
360 /* Compute the address of the indexed entry in table. */
361 #define ADDRESS_ENTRY(table, index) \
362 ((JSDHashEntryHdr *)((table)->entryStore + (index) * (table)->entrySize))
365 JS_DHashTableFinish(JSDHashTable
*table
)
367 char *entryAddr
, *entryLimit
;
369 JSDHashEntryHdr
*entry
;
371 #ifdef DEBUG_XXXbrendan
372 static FILE *dumpfp
= NULL
;
373 if (!dumpfp
) dumpfp
= fopen("/tmp/jsdhash.bigdump", "w");
375 #ifdef MOZILLA_CLIENT
376 NS_TraceStack(1, dumpfp
);
378 JS_DHashTableDumpMeter(table
, NULL
, dumpfp
);
383 INCREMENT_RECURSION_LEVEL(table
);
385 /* Call finalize before clearing entries, so it can enumerate them. */
386 table
->ops
->finalize(table
);
388 /* Clear any remaining live entries. */
389 entryAddr
= table
->entryStore
;
390 entrySize
= table
->entrySize
;
391 entryLimit
= entryAddr
+ JS_DHASH_TABLE_SIZE(table
) * entrySize
;
392 while (entryAddr
< entryLimit
) {
393 entry
= (JSDHashEntryHdr
*)entryAddr
;
394 if (ENTRY_IS_LIVE(entry
)) {
395 METER(table
->stats
.removeEnums
++);
396 table
->ops
->clearEntry(table
, entry
);
398 entryAddr
+= entrySize
;
401 DECREMENT_RECURSION_LEVEL(table
);
402 JS_ASSERT(RECURSION_LEVEL_SAFE_TO_FINISH(table
));
404 /* Free entry storage last. */
405 table
->ops
->freeTable(table
, table
->entryStore
);
408 static JSDHashEntryHdr
* JS_DHASH_FASTCALL
409 SearchTable(JSDHashTable
*table
, const void *key
, JSDHashNumber keyHash
,
412 JSDHashNumber hash1
, hash2
;
413 int hashShift
, sizeLog2
;
414 JSDHashEntryHdr
*entry
, *firstRemoved
;
415 JSDHashMatchEntry matchEntry
;
418 METER(table
->stats
.searches
++);
419 JS_ASSERT(!(keyHash
& COLLISION_FLAG
));
421 /* Compute the primary hash address. */
422 hashShift
= table
->hashShift
;
423 hash1
= HASH1(keyHash
, hashShift
);
424 entry
= ADDRESS_ENTRY(table
, hash1
);
426 /* Miss: return space for a new entry. */
427 if (JS_DHASH_ENTRY_IS_FREE(entry
)) {
428 METER(table
->stats
.misses
++);
432 /* Hit: return entry. */
433 matchEntry
= table
->ops
->matchEntry
;
434 if (MATCH_ENTRY_KEYHASH(entry
, keyHash
) && matchEntry(table
, entry
, key
)) {
435 METER(table
->stats
.hits
++);
439 /* Collision: double hash. */
440 sizeLog2
= JS_DHASH_BITS
- table
->hashShift
;
441 hash2
= HASH2(keyHash
, sizeLog2
, hashShift
);
442 sizeMask
= JS_BITMASK(sizeLog2
);
444 /* Save the first removed entry pointer so JS_DHASH_ADD can recycle it. */
448 if (JS_UNLIKELY(ENTRY_IS_REMOVED(entry
))) {
450 firstRemoved
= entry
;
452 if (op
== JS_DHASH_ADD
)
453 entry
->keyHash
|= COLLISION_FLAG
;
456 METER(table
->stats
.steps
++);
460 entry
= ADDRESS_ENTRY(table
, hash1
);
461 if (JS_DHASH_ENTRY_IS_FREE(entry
)) {
462 METER(table
->stats
.misses
++);
463 return (firstRemoved
&& op
== JS_DHASH_ADD
) ? firstRemoved
: entry
;
466 if (MATCH_ENTRY_KEYHASH(entry
, keyHash
) &&
467 matchEntry(table
, entry
, key
)) {
468 METER(table
->stats
.hits
++);
478 * This is a copy of SearchTable, used by ChangeTable, hardcoded to
479 * 1. assume |op == PL_DHASH_ADD|,
480 * 2. assume that |key| will never match an existing entry, and
481 * 3. assume that no entries have been removed from the current table
483 * Avoiding the need for |key| means we can avoid needing a way to map
484 * entries to keys, which means callers can use complex key types more
487 static JSDHashEntryHdr
* JS_DHASH_FASTCALL
488 FindFreeEntry(JSDHashTable
*table
, JSDHashNumber keyHash
)
490 JSDHashNumber hash1
, hash2
;
491 int hashShift
, sizeLog2
;
492 JSDHashEntryHdr
*entry
;
495 METER(table
->stats
.searches
++);
496 JS_ASSERT(!(keyHash
& COLLISION_FLAG
));
498 /* Compute the primary hash address. */
499 hashShift
= table
->hashShift
;
500 hash1
= HASH1(keyHash
, hashShift
);
501 entry
= ADDRESS_ENTRY(table
, hash1
);
503 /* Miss: return space for a new entry. */
504 if (JS_DHASH_ENTRY_IS_FREE(entry
)) {
505 METER(table
->stats
.misses
++);
509 /* Collision: double hash. */
510 sizeLog2
= JS_DHASH_BITS
- table
->hashShift
;
511 hash2
= HASH2(keyHash
, sizeLog2
, hashShift
);
512 sizeMask
= JS_BITMASK(sizeLog2
);
515 JS_ASSERT(!ENTRY_IS_REMOVED(entry
));
516 entry
->keyHash
|= COLLISION_FLAG
;
518 METER(table
->stats
.steps
++);
522 entry
= ADDRESS_ENTRY(table
, hash1
);
523 if (JS_DHASH_ENTRY_IS_FREE(entry
)) {
524 METER(table
->stats
.misses
++);
534 ChangeTable(JSDHashTable
*table
, int deltaLog2
)
536 int oldLog2
, newLog2
;
537 uint32 oldCapacity
, newCapacity
;
538 char *newEntryStore
, *oldEntryStore
, *oldEntryAddr
;
539 uint32 entrySize
, i
, nbytes
;
540 JSDHashEntryHdr
*oldEntry
, *newEntry
;
541 JSDHashMoveEntry moveEntry
;
543 uint32 recursionLevel
;
546 /* Look, but don't touch, until we succeed in getting new entry store. */
547 oldLog2
= JS_DHASH_BITS
- table
->hashShift
;
548 newLog2
= oldLog2
+ deltaLog2
;
549 oldCapacity
= JS_BIT(oldLog2
);
550 newCapacity
= JS_BIT(newLog2
);
551 if (newCapacity
>= JS_DHASH_SIZE_LIMIT
)
553 entrySize
= table
->entrySize
;
554 nbytes
= newCapacity
* entrySize
;
556 newEntryStore
= (char *) table
->ops
->allocTable(table
,
557 nbytes
+ ENTRY_STORE_EXTRA
);
561 /* We can't fail from here on, so update table parameters. */
563 recursionLevel
= RECURSION_LEVEL(table
);
565 table
->hashShift
= JS_DHASH_BITS
- newLog2
;
566 table
->removedCount
= 0;
569 /* Assign the new entry store to table. */
570 memset(newEntryStore
, 0, nbytes
);
571 oldEntryAddr
= oldEntryStore
= table
->entryStore
;
572 table
->entryStore
= newEntryStore
;
573 moveEntry
= table
->ops
->moveEntry
;
575 RECURSION_LEVEL(table
) = recursionLevel
;
578 /* Copy only live entries, leaving removed ones behind. */
579 for (i
= 0; i
< oldCapacity
; i
++) {
580 oldEntry
= (JSDHashEntryHdr
*)oldEntryAddr
;
581 if (ENTRY_IS_LIVE(oldEntry
)) {
582 oldEntry
->keyHash
&= ~COLLISION_FLAG
;
583 newEntry
= FindFreeEntry(table
, oldEntry
->keyHash
);
584 JS_ASSERT(JS_DHASH_ENTRY_IS_FREE(newEntry
));
585 moveEntry(table
, oldEntry
, newEntry
);
586 newEntry
->keyHash
= oldEntry
->keyHash
;
588 oldEntryAddr
+= entrySize
;
591 table
->ops
->freeTable(table
, oldEntryStore
);
595 JS_PUBLIC_API(JSDHashEntryHdr
*) JS_DHASH_FASTCALL
596 JS_DHashTableOperate(JSDHashTable
*table
, const void *key
, JSDHashOperator op
)
598 JSDHashNumber keyHash
;
599 JSDHashEntryHdr
*entry
;
603 JS_ASSERT(op
== JS_DHASH_LOOKUP
|| RECURSION_LEVEL(table
) == 0);
604 INCREMENT_RECURSION_LEVEL(table
);
606 keyHash
= table
->ops
->hashKey(table
, key
);
607 keyHash
*= JS_DHASH_GOLDEN_RATIO
;
609 /* Avoid 0 and 1 hash codes, they indicate free and removed entries. */
610 ENSURE_LIVE_KEYHASH(keyHash
);
611 keyHash
&= ~COLLISION_FLAG
;
614 case JS_DHASH_LOOKUP
:
615 METER(table
->stats
.lookups
++);
616 entry
= SearchTable(table
, key
, keyHash
, op
);
621 * If alpha is >= .75, grow or compress the table. If key is already
622 * in the table, we may grow once more than necessary, but only if we
623 * are on the edge of being overloaded.
625 size
= JS_DHASH_TABLE_SIZE(table
);
626 if (table
->entryCount
+ table
->removedCount
>= MAX_LOAD(table
, size
)) {
627 /* Compress if a quarter or more of all entries are removed. */
628 if (table
->removedCount
>= size
>> 2) {
629 METER(table
->stats
.compresses
++);
632 METER(table
->stats
.grows
++);
637 * Grow or compress table, returning null if ChangeTable fails and
638 * falling through might claim the last free entry.
640 if (!ChangeTable(table
, deltaLog2
) &&
641 table
->entryCount
+ table
->removedCount
== size
- 1) {
642 METER(table
->stats
.addFailures
++);
649 * Look for entry after possibly growing, so we don't have to add it,
650 * then skip it while growing the table and re-add it after.
652 entry
= SearchTable(table
, key
, keyHash
, op
);
653 if (!ENTRY_IS_LIVE(entry
)) {
654 /* Initialize the entry, indicating that it's no longer free. */
655 METER(table
->stats
.addMisses
++);
656 if (ENTRY_IS_REMOVED(entry
)) {
657 METER(table
->stats
.addOverRemoved
++);
658 table
->removedCount
--;
659 keyHash
|= COLLISION_FLAG
;
661 if (table
->ops
->initEntry
&&
662 !table
->ops
->initEntry(table
, entry
, key
)) {
663 /* We haven't claimed entry yet; fail with null return. */
664 memset(entry
+ 1, 0, table
->entrySize
- sizeof *entry
);
668 entry
->keyHash
= keyHash
;
671 METER(else table
->stats
.addHits
++);
674 case JS_DHASH_REMOVE
:
675 entry
= SearchTable(table
, key
, keyHash
, op
);
676 if (ENTRY_IS_LIVE(entry
)) {
677 /* Clear this entry and mark it as "removed". */
678 METER(table
->stats
.removeHits
++);
679 JS_DHashTableRawRemove(table
, entry
);
681 /* Shrink if alpha is <= .25 and table isn't too small already. */
682 size
= JS_DHASH_TABLE_SIZE(table
);
683 if (size
> JS_DHASH_MIN_SIZE
&&
684 table
->entryCount
<= MIN_LOAD(table
, size
)) {
685 METER(table
->stats
.shrinks
++);
686 (void) ChangeTable(table
, -1);
689 METER(else table
->stats
.removeMisses
++);
698 DECREMENT_RECURSION_LEVEL(table
);
704 JS_DHashTableRawRemove(JSDHashTable
*table
, JSDHashEntryHdr
*entry
)
706 JSDHashNumber keyHash
; /* load first in case clearEntry goofs it */
708 JS_ASSERT(RECURSION_LEVEL(table
) != IMMUTABLE_RECURSION_LEVEL
);
710 JS_ASSERT(JS_DHASH_ENTRY_IS_LIVE(entry
));
711 keyHash
= entry
->keyHash
;
712 table
->ops
->clearEntry(table
, entry
);
713 if (keyHash
& COLLISION_FLAG
) {
714 MARK_ENTRY_REMOVED(entry
);
715 table
->removedCount
++;
717 METER(table
->stats
.removeFrees
++);
718 MARK_ENTRY_FREE(entry
);
723 JS_PUBLIC_API(uint32
)
724 JS_DHashTableEnumerate(JSDHashTable
*table
, JSDHashEnumerator etor
, void *arg
)
726 char *entryAddr
, *entryLimit
;
727 uint32 i
, capacity
, entrySize
, ceiling
;
729 JSDHashEntryHdr
*entry
;
732 INCREMENT_RECURSION_LEVEL(table
);
734 entryAddr
= table
->entryStore
;
735 entrySize
= table
->entrySize
;
736 capacity
= JS_DHASH_TABLE_SIZE(table
);
737 entryLimit
= entryAddr
+ capacity
* entrySize
;
739 didRemove
= JS_FALSE
;
740 while (entryAddr
< entryLimit
) {
741 entry
= (JSDHashEntryHdr
*)entryAddr
;
742 if (ENTRY_IS_LIVE(entry
)) {
743 op
= etor(table
, entry
, i
++, arg
);
744 if (op
& JS_DHASH_REMOVE
) {
745 METER(table
->stats
.removeEnums
++);
746 JS_DHashTableRawRemove(table
, entry
);
749 if (op
& JS_DHASH_STOP
)
752 entryAddr
+= entrySize
;
755 JS_ASSERT(!didRemove
|| RECURSION_LEVEL(table
) == 1);
758 * Shrink or compress if a quarter or more of all entries are removed, or
759 * if the table is underloaded according to the configured minimum alpha,
760 * and is not minimal-size already. Do this only if we removed above, so
761 * non-removing enumerations can count on stable table->entryStore until
762 * the next non-lookup-Operate or removing-Enumerate.
765 (table
->removedCount
>= capacity
>> 2 ||
766 (capacity
> JS_DHASH_MIN_SIZE
&&
767 table
->entryCount
<= MIN_LOAD(table
, capacity
)))) {
768 METER(table
->stats
.enumShrinks
++);
769 capacity
= table
->entryCount
;
770 capacity
+= capacity
>> 1;
771 if (capacity
< JS_DHASH_MIN_SIZE
)
772 capacity
= JS_DHASH_MIN_SIZE
;
774 JS_CEILING_LOG2(ceiling
, capacity
);
775 ceiling
-= JS_DHASH_BITS
- table
->hashShift
;
777 (void) ChangeTable(table
, ceiling
);
780 DECREMENT_RECURSION_LEVEL(table
);
787 JS_DHashMarkTableImmutable(JSDHashTable
*table
)
789 RECURSION_LEVEL(table
) = IMMUTABLE_RECURSION_LEVEL
;
797 JS_DHashTableDumpMeter(JSDHashTable
*table
, JSDHashEnumerator dump
, FILE *fp
)
800 uint32 entrySize
, entryCount
;
801 int hashShift
, sizeLog2
;
802 uint32 i
, tableSize
, sizeMask
, chainLen
, maxChainLen
, chainCount
;
803 JSDHashNumber hash1
, hash2
, saveHash1
, maxChainHash1
, maxChainHash2
;
804 double sqsum
, mean
, variance
, sigma
;
805 JSDHashEntryHdr
*entry
, *probe
;
807 entryAddr
= table
->entryStore
;
808 entrySize
= table
->entrySize
;
809 hashShift
= table
->hashShift
;
810 sizeLog2
= JS_DHASH_BITS
- hashShift
;
811 tableSize
= JS_DHASH_TABLE_SIZE(table
);
812 sizeMask
= JS_BITMASK(sizeLog2
);
813 chainCount
= maxChainLen
= 0;
817 for (i
= 0; i
< tableSize
; i
++) {
818 entry
= (JSDHashEntryHdr
*)entryAddr
;
819 entryAddr
+= entrySize
;
820 if (!ENTRY_IS_LIVE(entry
))
822 hash1
= HASH1(entry
->keyHash
& ~COLLISION_FLAG
, hashShift
);
824 probe
= ADDRESS_ENTRY(table
, hash1
);
826 if (probe
== entry
) {
827 /* Start of a (possibly unit-length) chain. */
830 hash2
= HASH2(entry
->keyHash
& ~COLLISION_FLAG
, sizeLog2
,
836 probe
= ADDRESS_ENTRY(table
, hash1
);
837 } while (probe
!= entry
);
839 sqsum
+= chainLen
* chainLen
;
840 if (chainLen
> maxChainLen
) {
841 maxChainLen
= chainLen
;
842 maxChainHash1
= saveHash1
;
843 maxChainHash2
= hash2
;
847 entryCount
= table
->entryCount
;
848 if (entryCount
&& chainCount
) {
849 mean
= (double)entryCount
/ chainCount
;
850 variance
= chainCount
* sqsum
- entryCount
* entryCount
;
851 if (variance
< 0 || chainCount
== 1)
854 variance
/= chainCount
* (chainCount
- 1);
855 sigma
= sqrt(variance
);
860 fprintf(fp
, "Double hashing statistics:\n");
861 fprintf(fp
, " table size (in entries): %u\n", tableSize
);
862 fprintf(fp
, " number of entries: %u\n", table
->entryCount
);
863 fprintf(fp
, " number of removed entries: %u\n", table
->removedCount
);
864 fprintf(fp
, " number of searches: %u\n", table
->stats
.searches
);
865 fprintf(fp
, " number of hits: %u\n", table
->stats
.hits
);
866 fprintf(fp
, " number of misses: %u\n", table
->stats
.misses
);
867 fprintf(fp
, " mean steps per search: %g\n", table
->stats
.searches
?
868 (double)table
->stats
.steps
869 / table
->stats
.searches
:
871 fprintf(fp
, " mean hash chain length: %g\n", mean
);
872 fprintf(fp
, " standard deviation: %g\n", sigma
);
873 fprintf(fp
, " maximum hash chain length: %u\n", maxChainLen
);
874 fprintf(fp
, " number of lookups: %u\n", table
->stats
.lookups
);
875 fprintf(fp
, " adds that made a new entry: %u\n", table
->stats
.addMisses
);
876 fprintf(fp
, "adds that recycled removeds: %u\n", table
->stats
.addOverRemoved
);
877 fprintf(fp
, " adds that found an entry: %u\n", table
->stats
.addHits
);
878 fprintf(fp
, " add failures: %u\n", table
->stats
.addFailures
);
879 fprintf(fp
, " useful removes: %u\n", table
->stats
.removeHits
);
880 fprintf(fp
, " useless removes: %u\n", table
->stats
.removeMisses
);
881 fprintf(fp
, "removes that freed an entry: %u\n", table
->stats
.removeFrees
);
882 fprintf(fp
, " removes while enumerating: %u\n", table
->stats
.removeEnums
);
883 fprintf(fp
, " number of grows: %u\n", table
->stats
.grows
);
884 fprintf(fp
, " number of shrinks: %u\n", table
->stats
.shrinks
);
885 fprintf(fp
, " number of compresses: %u\n", table
->stats
.compresses
);
886 fprintf(fp
, "number of enumerate shrinks: %u\n", table
->stats
.enumShrinks
);
888 if (dump
&& maxChainLen
&& hash2
) {
889 fputs("Maximum hash chain:\n", fp
);
890 hash1
= maxChainHash1
;
891 hash2
= maxChainHash2
;
892 entry
= ADDRESS_ENTRY(table
, hash1
);
895 if (dump(table
, entry
, i
++, fp
) != JS_DHASH_NEXT
)
899 entry
= ADDRESS_ENTRY(table
, hash1
);
900 } while (JS_DHASH_ENTRY_IS_BUSY(entry
));
903 #endif /* JS_DHASHMETER */