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.
43 * GENERATED BY js/src/plify_jsdhash.sed -- DO NOT EDIT!!!
50 #include "nsDebug.h" /* for PR_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 * PLDHashTable, which could cause issues when mixing DEBUG and
67 * non-DEBUG components.
71 #define JSDHASH_ONELINE_ASSERT PR_ASSERT
72 #define RECURSION_LEVEL(table_) (*(PRUint32*)(table_->entryStore + \
73 PL_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 ((PRUint32)-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(PRUint32)
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 NS_ASSERTION(RECURSION_LEVEL(table_) > 0, "RECURSION_LEVEL(table_) > 0"); \
99 --RECURSION_LEVEL(table_); \
105 #define ENTRY_STORE_EXTRA 0
106 #define INCREMENT_RECURSION_LEVEL(table_) PR_BEGIN_MACRO PR_END_MACRO
107 #define DECREMENT_RECURSION_LEVEL(table_) PR_BEGIN_MACRO PR_END_MACRO
109 #endif /* defined(DEBUG) */
112 PL_DHashAllocTable(PLDHashTable
*table
, PRUint32 nbytes
)
114 return malloc(nbytes
);
118 PL_DHashFreeTable(PLDHashTable
*table
, void *ptr
)
124 PL_DHashStringKey(PLDHashTable
*table
, const void *key
)
127 const unsigned char *s
;
130 for (s
= (const unsigned char *) key
; *s
!= '\0'; s
++)
131 h
= PR_ROTATE_LEFT32(h
, 4) ^ *s
;
136 PL_DHashVoidPtrKeyStub(PLDHashTable
*table
, const void *key
)
138 return (PLDHashNumber
)(unsigned long)key
>> 2;
142 PL_DHashMatchEntryStub(PLDHashTable
*table
,
143 const PLDHashEntryHdr
*entry
,
146 const PLDHashEntryStub
*stub
= (const PLDHashEntryStub
*)entry
;
148 return stub
->key
== key
;
152 PL_DHashMatchStringKey(PLDHashTable
*table
,
153 const PLDHashEntryHdr
*entry
,
156 const PLDHashEntryStub
*stub
= (const PLDHashEntryStub
*)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 PL_DHashMoveEntryStub(PLDHashTable
*table
,
166 const PLDHashEntryHdr
*from
,
169 memcpy(to
, from
, table
->entrySize
);
173 PL_DHashClearEntryStub(PLDHashTable
*table
, PLDHashEntryHdr
*entry
)
175 memset(entry
, 0, table
->entrySize
);
179 PL_DHashFreeStringKey(PLDHashTable
*table
, PLDHashEntryHdr
*entry
)
181 const PLDHashEntryStub
*stub
= (const PLDHashEntryStub
*)entry
;
183 free((void *) stub
->key
);
184 memset(entry
, 0, table
->entrySize
);
188 PL_DHashFinalizeStub(PLDHashTable
*table
)
192 static const PLDHashTableOps stub_ops
= {
195 PL_DHashVoidPtrKeyStub
,
196 PL_DHashMatchEntryStub
,
197 PL_DHashMoveEntryStub
,
198 PL_DHashClearEntryStub
,
199 PL_DHashFinalizeStub
,
203 const PLDHashTableOps
*
204 PL_DHashGetStubOps(void)
210 PL_NewDHashTable(const PLDHashTableOps
*ops
, void *data
, PRUint32 entrySize
,
215 table
= (PLDHashTable
*) malloc(sizeof *table
);
218 if (!PL_DHashTableInit(table
, ops
, data
, entrySize
, capacity
)) {
226 PL_DHashTableDestroy(PLDHashTable
*table
)
228 PL_DHashTableFinish(table
);
233 PL_DHashTableInit(PLDHashTable
*table
, const PLDHashTableOps
*ops
, void *data
,
234 PRUint32 entrySize
, PRUint32 capacity
)
240 if (entrySize
> 10 * sizeof(void *)) {
242 "pldhash: 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
< PL_DHASH_MIN_SIZE
)
253 capacity
= PL_DHASH_MIN_SIZE
;
255 PR_CEILING_LOG2(log2
, capacity
);
257 capacity
= PR_BIT(log2
);
258 if (capacity
>= PL_DHASH_SIZE_LIMIT
)
260 table
->hashShift
= PL_DHASH_BITS
- log2
;
261 table
->maxAlphaFrac
= (PRUint8
)(0x100 * PL_DHASH_DEFAULT_MAX_ALPHA
);
262 table
->minAlphaFrac
= (PRUint8
)(0x100 * PL_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 PL_DHashTableSetAlphaBounds(PLDHashTable
*table
,
296 * Reject obviously insane bounds, rather than trying to guess what the
297 * buggy caller intended.
299 NS_ASSERTION(0.5 <= maxAlpha
&& maxAlpha
< 1 && 0 <= minAlpha
,
300 "0.5 <= maxAlpha && maxAlpha < 1 && 0 <= minAlpha");
301 if (maxAlpha
< 0.5 || 1 <= maxAlpha
|| minAlpha
< 0)
305 * Ensure that at least one entry will always be free. If maxAlpha at
306 * minimum size leaves no entries free, reduce maxAlpha based on minimum
307 * size and the precision limit of maxAlphaFrac's fixed point format.
309 NS_ASSERTION(PL_DHASH_MIN_SIZE
- (maxAlpha
* PL_DHASH_MIN_SIZE
) >= 1,
310 "PL_DHASH_MIN_SIZE - (maxAlpha * PL_DHASH_MIN_SIZE) >= 1");
311 if (PL_DHASH_MIN_SIZE
- (maxAlpha
* PL_DHASH_MIN_SIZE
) < 1) {
313 (PL_DHASH_MIN_SIZE
- PR_MAX(PL_DHASH_MIN_SIZE
/ 256, 1))
318 * Ensure that minAlpha is strictly less than half maxAlpha. Take care
319 * not to truncate an entry's worth of alpha when storing in minAlphaFrac
320 * (8-bit fixed point format).
322 NS_ASSERTION(minAlpha
< maxAlpha
/ 2,
323 "minAlpha < maxAlpha / 2");
324 if (minAlpha
>= maxAlpha
/ 2) {
325 size
= PL_DHASH_TABLE_SIZE(table
);
326 minAlpha
= (size
* maxAlpha
- PR_MAX(size
/ 256, 1)) / (2 * size
);
329 table
->maxAlphaFrac
= (PRUint8
)(maxAlpha
* 256);
330 table
->minAlphaFrac
= (PRUint8
)(minAlpha
* 256);
334 * Double hashing needs the second hash code to be relatively prime to table
335 * size, so we simply make hash2 odd.
337 #define HASH1(hash0, shift) ((hash0) >> (shift))
338 #define HASH2(hash0,log2,shift) ((((hash0) << (log2)) >> (shift)) | 1)
341 * Reserve keyHash 0 for free entries and 1 for removed-entry sentinels. Note
342 * that a removed-entry sentinel need be stored only if the removed entry had
343 * a colliding entry added after it. Therefore we can use 1 as the collision
344 * flag in addition to the removed-entry sentinel value. Multiplicative hash
345 * uses the high order bits of keyHash, so this least-significant reservation
346 * should not hurt the hash function's effectiveness much.
348 * If you change any of these magic numbers, also update PL_DHASH_ENTRY_IS_LIVE
349 * in pldhash.h. It used to be private to pldhash.c, but then became public to
350 * assist iterator writers who inspect table->entryStore directly.
352 #define COLLISION_FLAG ((PLDHashNumber) 1)
353 #define MARK_ENTRY_FREE(entry) ((entry)->keyHash = 0)
354 #define MARK_ENTRY_REMOVED(entry) ((entry)->keyHash = 1)
355 #define ENTRY_IS_REMOVED(entry) ((entry)->keyHash == 1)
356 #define ENTRY_IS_LIVE(entry) PL_DHASH_ENTRY_IS_LIVE(entry)
357 #define ENSURE_LIVE_KEYHASH(hash0) if (hash0 < 2) hash0 -= 2; else (void)0
359 /* Match an entry's keyHash against an unstored one computed from a key. */
360 #define MATCH_ENTRY_KEYHASH(entry,hash0) \
361 (((entry)->keyHash & ~COLLISION_FLAG) == (hash0))
363 /* Compute the address of the indexed entry in table. */
364 #define ADDRESS_ENTRY(table, index) \
365 ((PLDHashEntryHdr *)((table)->entryStore + (index) * (table)->entrySize))
368 PL_DHashTableFinish(PLDHashTable
*table
)
370 char *entryAddr
, *entryLimit
;
372 PLDHashEntryHdr
*entry
;
374 #ifdef DEBUG_XXXbrendan
375 static FILE *dumpfp
= NULL
;
376 if (!dumpfp
) dumpfp
= fopen("/tmp/pldhash.bigdump", "w");
378 #ifdef MOZILLA_CLIENT
379 NS_TraceStack(1, dumpfp
);
381 PL_DHashTableDumpMeter(table
, NULL
, dumpfp
);
386 INCREMENT_RECURSION_LEVEL(table
);
388 /* Call finalize before clearing entries, so it can enumerate them. */
389 table
->ops
->finalize(table
);
391 /* Clear any remaining live entries. */
392 entryAddr
= table
->entryStore
;
393 entrySize
= table
->entrySize
;
394 entryLimit
= entryAddr
+ PL_DHASH_TABLE_SIZE(table
) * entrySize
;
395 while (entryAddr
< entryLimit
) {
396 entry
= (PLDHashEntryHdr
*)entryAddr
;
397 if (ENTRY_IS_LIVE(entry
)) {
398 METER(table
->stats
.removeEnums
++);
399 table
->ops
->clearEntry(table
, entry
);
401 entryAddr
+= entrySize
;
404 DECREMENT_RECURSION_LEVEL(table
);
405 NS_ASSERTION(RECURSION_LEVEL_SAFE_TO_FINISH(table
),
406 "RECURSION_LEVEL_SAFE_TO_FINISH(table)");
408 /* Free entry storage last. */
409 table
->ops
->freeTable(table
, table
->entryStore
);
412 static PLDHashEntryHdr
* PL_DHASH_FASTCALL
413 SearchTable(PLDHashTable
*table
, const void *key
, PLDHashNumber keyHash
,
416 PLDHashNumber hash1
, hash2
;
417 int hashShift
, sizeLog2
;
418 PLDHashEntryHdr
*entry
, *firstRemoved
;
419 PLDHashMatchEntry matchEntry
;
422 METER(table
->stats
.searches
++);
423 NS_ASSERTION(!(keyHash
& COLLISION_FLAG
),
424 "!(keyHash & COLLISION_FLAG)");
426 /* Compute the primary hash address. */
427 hashShift
= table
->hashShift
;
428 hash1
= HASH1(keyHash
, hashShift
);
429 entry
= ADDRESS_ENTRY(table
, hash1
);
431 /* Miss: return space for a new entry. */
432 if (PL_DHASH_ENTRY_IS_FREE(entry
)) {
433 METER(table
->stats
.misses
++);
437 /* Hit: return entry. */
438 matchEntry
= table
->ops
->matchEntry
;
439 if (MATCH_ENTRY_KEYHASH(entry
, keyHash
) && matchEntry(table
, entry
, key
)) {
440 METER(table
->stats
.hits
++);
444 /* Collision: double hash. */
445 sizeLog2
= PL_DHASH_BITS
- table
->hashShift
;
446 hash2
= HASH2(keyHash
, sizeLog2
, hashShift
);
447 sizeMask
= PR_BITMASK(sizeLog2
);
449 /* Save the first removed entry pointer so PL_DHASH_ADD can recycle it. */
453 if (NS_UNLIKELY(ENTRY_IS_REMOVED(entry
))) {
455 firstRemoved
= entry
;
457 if (op
== PL_DHASH_ADD
)
458 entry
->keyHash
|= COLLISION_FLAG
;
461 METER(table
->stats
.steps
++);
465 entry
= ADDRESS_ENTRY(table
, hash1
);
466 if (PL_DHASH_ENTRY_IS_FREE(entry
)) {
467 METER(table
->stats
.misses
++);
468 return (firstRemoved
&& op
== PL_DHASH_ADD
) ? firstRemoved
: entry
;
471 if (MATCH_ENTRY_KEYHASH(entry
, keyHash
) &&
472 matchEntry(table
, entry
, key
)) {
473 METER(table
->stats
.hits
++);
483 * This is a copy of SearchTable, used by ChangeTable, hardcoded to
484 * 1. assume |op == PL_DHASH_ADD|,
485 * 2. assume that |key| will never match an existing entry, and
486 * 3. assume that no entries have been removed from the current table
488 * Avoiding the need for |key| means we can avoid needing a way to map
489 * entries to keys, which means callers can use complex key types more
492 static PLDHashEntryHdr
* PL_DHASH_FASTCALL
493 FindFreeEntry(PLDHashTable
*table
, PLDHashNumber keyHash
)
495 PLDHashNumber hash1
, hash2
;
496 int hashShift
, sizeLog2
;
497 PLDHashEntryHdr
*entry
;
500 METER(table
->stats
.searches
++);
501 NS_ASSERTION(!(keyHash
& COLLISION_FLAG
),
502 "!(keyHash & COLLISION_FLAG)");
504 /* Compute the primary hash address. */
505 hashShift
= table
->hashShift
;
506 hash1
= HASH1(keyHash
, hashShift
);
507 entry
= ADDRESS_ENTRY(table
, hash1
);
509 /* Miss: return space for a new entry. */
510 if (PL_DHASH_ENTRY_IS_FREE(entry
)) {
511 METER(table
->stats
.misses
++);
515 /* Collision: double hash. */
516 sizeLog2
= PL_DHASH_BITS
- table
->hashShift
;
517 hash2
= HASH2(keyHash
, sizeLog2
, hashShift
);
518 sizeMask
= PR_BITMASK(sizeLog2
);
521 NS_ASSERTION(!ENTRY_IS_REMOVED(entry
),
522 "!ENTRY_IS_REMOVED(entry)");
523 entry
->keyHash
|= COLLISION_FLAG
;
525 METER(table
->stats
.steps
++);
529 entry
= ADDRESS_ENTRY(table
, hash1
);
530 if (PL_DHASH_ENTRY_IS_FREE(entry
)) {
531 METER(table
->stats
.misses
++);
541 ChangeTable(PLDHashTable
*table
, int deltaLog2
)
543 int oldLog2
, newLog2
;
544 PRUint32 oldCapacity
, newCapacity
;
545 char *newEntryStore
, *oldEntryStore
, *oldEntryAddr
;
546 PRUint32 entrySize
, i
, nbytes
;
547 PLDHashEntryHdr
*oldEntry
, *newEntry
;
548 PLDHashMoveEntry moveEntry
;
550 PRUint32 recursionLevel
;
553 /* Look, but don't touch, until we succeed in getting new entry store. */
554 oldLog2
= PL_DHASH_BITS
- table
->hashShift
;
555 newLog2
= oldLog2
+ deltaLog2
;
556 oldCapacity
= PR_BIT(oldLog2
);
557 newCapacity
= PR_BIT(newLog2
);
558 if (newCapacity
>= PL_DHASH_SIZE_LIMIT
)
560 entrySize
= table
->entrySize
;
561 nbytes
= newCapacity
* entrySize
;
563 newEntryStore
= (char *) table
->ops
->allocTable(table
,
564 nbytes
+ ENTRY_STORE_EXTRA
);
568 /* We can't fail from here on, so update table parameters. */
570 recursionLevel
= RECURSION_LEVEL(table
);
572 table
->hashShift
= PL_DHASH_BITS
- newLog2
;
573 table
->removedCount
= 0;
576 /* Assign the new entry store to table. */
577 memset(newEntryStore
, 0, nbytes
);
578 oldEntryAddr
= oldEntryStore
= table
->entryStore
;
579 table
->entryStore
= newEntryStore
;
580 moveEntry
= table
->ops
->moveEntry
;
582 RECURSION_LEVEL(table
) = recursionLevel
;
585 /* Copy only live entries, leaving removed ones behind. */
586 for (i
= 0; i
< oldCapacity
; i
++) {
587 oldEntry
= (PLDHashEntryHdr
*)oldEntryAddr
;
588 if (ENTRY_IS_LIVE(oldEntry
)) {
589 oldEntry
->keyHash
&= ~COLLISION_FLAG
;
590 newEntry
= FindFreeEntry(table
, oldEntry
->keyHash
);
591 NS_ASSERTION(PL_DHASH_ENTRY_IS_FREE(newEntry
),
592 "PL_DHASH_ENTRY_IS_FREE(newEntry)");
593 moveEntry(table
, oldEntry
, newEntry
);
594 newEntry
->keyHash
= oldEntry
->keyHash
;
596 oldEntryAddr
+= entrySize
;
599 table
->ops
->freeTable(table
, oldEntryStore
);
603 PLDHashEntryHdr
* PL_DHASH_FASTCALL
604 PL_DHashTableOperate(PLDHashTable
*table
, const void *key
, PLDHashOperator op
)
606 PLDHashNumber keyHash
;
607 PLDHashEntryHdr
*entry
;
611 NS_ASSERTION(op
== PL_DHASH_LOOKUP
|| RECURSION_LEVEL(table
) == 0,
612 "op == PL_DHASH_LOOKUP || RECURSION_LEVEL(table) == 0");
613 INCREMENT_RECURSION_LEVEL(table
);
615 keyHash
= table
->ops
->hashKey(table
, key
);
616 keyHash
*= PL_DHASH_GOLDEN_RATIO
;
618 /* Avoid 0 and 1 hash codes, they indicate free and removed entries. */
619 ENSURE_LIVE_KEYHASH(keyHash
);
620 keyHash
&= ~COLLISION_FLAG
;
623 case PL_DHASH_LOOKUP
:
624 METER(table
->stats
.lookups
++);
625 entry
= SearchTable(table
, key
, keyHash
, op
);
630 * If alpha is >= .75, grow or compress the table. If key is already
631 * in the table, we may grow once more than necessary, but only if we
632 * are on the edge of being overloaded.
634 size
= PL_DHASH_TABLE_SIZE(table
);
635 if (table
->entryCount
+ table
->removedCount
>= MAX_LOAD(table
, size
)) {
636 /* Compress if a quarter or more of all entries are removed. */
637 if (table
->removedCount
>= size
>> 2) {
638 METER(table
->stats
.compresses
++);
641 METER(table
->stats
.grows
++);
646 * Grow or compress table, returning null if ChangeTable fails and
647 * falling through might claim the last free entry.
649 if (!ChangeTable(table
, deltaLog2
) &&
650 table
->entryCount
+ table
->removedCount
== size
- 1) {
651 METER(table
->stats
.addFailures
++);
658 * Look for entry after possibly growing, so we don't have to add it,
659 * then skip it while growing the table and re-add it after.
661 entry
= SearchTable(table
, key
, keyHash
, op
);
662 if (!ENTRY_IS_LIVE(entry
)) {
663 /* Initialize the entry, indicating that it's no longer free. */
664 METER(table
->stats
.addMisses
++);
665 if (ENTRY_IS_REMOVED(entry
)) {
666 METER(table
->stats
.addOverRemoved
++);
667 table
->removedCount
--;
668 keyHash
|= COLLISION_FLAG
;
670 if (table
->ops
->initEntry
&&
671 !table
->ops
->initEntry(table
, entry
, key
)) {
672 /* We haven't claimed entry yet; fail with null return. */
673 memset(entry
+ 1, 0, table
->entrySize
- sizeof *entry
);
677 entry
->keyHash
= keyHash
;
680 METER(else table
->stats
.addHits
++);
683 case PL_DHASH_REMOVE
:
684 entry
= SearchTable(table
, key
, keyHash
, op
);
685 if (ENTRY_IS_LIVE(entry
)) {
686 /* Clear this entry and mark it as "removed". */
687 METER(table
->stats
.removeHits
++);
688 PL_DHashTableRawRemove(table
, entry
);
690 /* Shrink if alpha is <= .25 and table isn't too small already. */
691 size
= PL_DHASH_TABLE_SIZE(table
);
692 if (size
> PL_DHASH_MIN_SIZE
&&
693 table
->entryCount
<= MIN_LOAD(table
, size
)) {
694 METER(table
->stats
.shrinks
++);
695 (void) ChangeTable(table
, -1);
698 METER(else table
->stats
.removeMisses
++);
707 DECREMENT_RECURSION_LEVEL(table
);
713 PL_DHashTableRawRemove(PLDHashTable
*table
, PLDHashEntryHdr
*entry
)
715 PLDHashNumber keyHash
; /* load first in case clearEntry goofs it */
717 NS_ASSERTION(RECURSION_LEVEL(table
) != IMMUTABLE_RECURSION_LEVEL
,
718 "RECURSION_LEVEL(table) != IMMUTABLE_RECURSION_LEVEL");
720 NS_ASSERTION(PL_DHASH_ENTRY_IS_LIVE(entry
),
721 "PL_DHASH_ENTRY_IS_LIVE(entry)");
722 keyHash
= entry
->keyHash
;
723 table
->ops
->clearEntry(table
, entry
);
724 if (keyHash
& COLLISION_FLAG
) {
725 MARK_ENTRY_REMOVED(entry
);
726 table
->removedCount
++;
728 METER(table
->stats
.removeFrees
++);
729 MARK_ENTRY_FREE(entry
);
735 PL_DHashTableEnumerate(PLDHashTable
*table
, PLDHashEnumerator etor
, void *arg
)
737 char *entryAddr
, *entryLimit
;
738 PRUint32 i
, capacity
, entrySize
, ceiling
;
740 PLDHashEntryHdr
*entry
;
743 INCREMENT_RECURSION_LEVEL(table
);
745 entryAddr
= table
->entryStore
;
746 entrySize
= table
->entrySize
;
747 capacity
= PL_DHASH_TABLE_SIZE(table
);
748 entryLimit
= entryAddr
+ capacity
* entrySize
;
750 didRemove
= PR_FALSE
;
751 while (entryAddr
< entryLimit
) {
752 entry
= (PLDHashEntryHdr
*)entryAddr
;
753 if (ENTRY_IS_LIVE(entry
)) {
754 op
= etor(table
, entry
, i
++, arg
);
755 if (op
& PL_DHASH_REMOVE
) {
756 METER(table
->stats
.removeEnums
++);
757 PL_DHashTableRawRemove(table
, entry
);
760 if (op
& PL_DHASH_STOP
)
763 entryAddr
+= entrySize
;
766 NS_ASSERTION(!didRemove
|| RECURSION_LEVEL(table
) == 1,
767 "!didRemove || RECURSION_LEVEL(table) == 1");
770 * Shrink or compress if a quarter or more of all entries are removed, or
771 * if the table is underloaded according to the configured minimum alpha,
772 * and is not minimal-size already. Do this only if we removed above, so
773 * non-removing enumerations can count on stable table->entryStore until
774 * the next non-lookup-Operate or removing-Enumerate.
777 (table
->removedCount
>= capacity
>> 2 ||
778 (capacity
> PL_DHASH_MIN_SIZE
&&
779 table
->entryCount
<= MIN_LOAD(table
, capacity
)))) {
780 METER(table
->stats
.enumShrinks
++);
781 capacity
= table
->entryCount
;
782 capacity
+= capacity
>> 1;
783 if (capacity
< PL_DHASH_MIN_SIZE
)
784 capacity
= PL_DHASH_MIN_SIZE
;
786 PR_CEILING_LOG2(ceiling
, capacity
);
787 ceiling
-= PL_DHASH_BITS
- table
->hashShift
;
789 (void) ChangeTable(table
, ceiling
);
792 DECREMENT_RECURSION_LEVEL(table
);
799 PL_DHashMarkTableImmutable(PLDHashTable
*table
)
801 RECURSION_LEVEL(table
) = IMMUTABLE_RECURSION_LEVEL
;
809 PL_DHashTableDumpMeter(PLDHashTable
*table
, PLDHashEnumerator dump
, FILE *fp
)
812 PRUint32 entrySize
, entryCount
;
813 int hashShift
, sizeLog2
;
814 PRUint32 i
, tableSize
, sizeMask
, chainLen
, maxChainLen
, chainCount
;
815 PLDHashNumber hash1
, hash2
, saveHash1
, maxChainHash1
, maxChainHash2
;
816 double sqsum
, mean
, variance
, sigma
;
817 PLDHashEntryHdr
*entry
, *probe
;
819 entryAddr
= table
->entryStore
;
820 entrySize
= table
->entrySize
;
821 hashShift
= table
->hashShift
;
822 sizeLog2
= PL_DHASH_BITS
- hashShift
;
823 tableSize
= PL_DHASH_TABLE_SIZE(table
);
824 sizeMask
= PR_BITMASK(sizeLog2
);
825 chainCount
= maxChainLen
= 0;
829 for (i
= 0; i
< tableSize
; i
++) {
830 entry
= (PLDHashEntryHdr
*)entryAddr
;
831 entryAddr
+= entrySize
;
832 if (!ENTRY_IS_LIVE(entry
))
834 hash1
= HASH1(entry
->keyHash
& ~COLLISION_FLAG
, hashShift
);
836 probe
= ADDRESS_ENTRY(table
, hash1
);
838 if (probe
== entry
) {
839 /* Start of a (possibly unit-length) chain. */
842 hash2
= HASH2(entry
->keyHash
& ~COLLISION_FLAG
, sizeLog2
,
848 probe
= ADDRESS_ENTRY(table
, hash1
);
849 } while (probe
!= entry
);
851 sqsum
+= chainLen
* chainLen
;
852 if (chainLen
> maxChainLen
) {
853 maxChainLen
= chainLen
;
854 maxChainHash1
= saveHash1
;
855 maxChainHash2
= hash2
;
859 entryCount
= table
->entryCount
;
860 if (entryCount
&& chainCount
) {
861 mean
= (double)entryCount
/ chainCount
;
862 variance
= chainCount
* sqsum
- entryCount
* entryCount
;
863 if (variance
< 0 || chainCount
== 1)
866 variance
/= chainCount
* (chainCount
- 1);
867 sigma
= sqrt(variance
);
872 fprintf(fp
, "Double hashing statistics:\n");
873 fprintf(fp
, " table size (in entries): %u\n", tableSize
);
874 fprintf(fp
, " number of entries: %u\n", table
->entryCount
);
875 fprintf(fp
, " number of removed entries: %u\n", table
->removedCount
);
876 fprintf(fp
, " number of searches: %u\n", table
->stats
.searches
);
877 fprintf(fp
, " number of hits: %u\n", table
->stats
.hits
);
878 fprintf(fp
, " number of misses: %u\n", table
->stats
.misses
);
879 fprintf(fp
, " mean steps per search: %g\n", table
->stats
.searches
?
880 (double)table
->stats
.steps
881 / table
->stats
.searches
:
883 fprintf(fp
, " mean hash chain length: %g\n", mean
);
884 fprintf(fp
, " standard deviation: %g\n", sigma
);
885 fprintf(fp
, " maximum hash chain length: %u\n", maxChainLen
);
886 fprintf(fp
, " number of lookups: %u\n", table
->stats
.lookups
);
887 fprintf(fp
, " adds that made a new entry: %u\n", table
->stats
.addMisses
);
888 fprintf(fp
, "adds that recycled removeds: %u\n", table
->stats
.addOverRemoved
);
889 fprintf(fp
, " adds that found an entry: %u\n", table
->stats
.addHits
);
890 fprintf(fp
, " add failures: %u\n", table
->stats
.addFailures
);
891 fprintf(fp
, " useful removes: %u\n", table
->stats
.removeHits
);
892 fprintf(fp
, " useless removes: %u\n", table
->stats
.removeMisses
);
893 fprintf(fp
, "removes that freed an entry: %u\n", table
->stats
.removeFrees
);
894 fprintf(fp
, " removes while enumerating: %u\n", table
->stats
.removeEnums
);
895 fprintf(fp
, " number of grows: %u\n", table
->stats
.grows
);
896 fprintf(fp
, " number of shrinks: %u\n", table
->stats
.shrinks
);
897 fprintf(fp
, " number of compresses: %u\n", table
->stats
.compresses
);
898 fprintf(fp
, "number of enumerate shrinks: %u\n", table
->stats
.enumShrinks
);
900 if (dump
&& maxChainLen
&& hash2
) {
901 fputs("Maximum hash chain:\n", fp
);
902 hash1
= maxChainHash1
;
903 hash2
= maxChainHash2
;
904 entry
= ADDRESS_ENTRY(table
, hash1
);
907 if (dump(table
, entry
, i
++, fp
) != PL_DHASH_NEXT
)
911 entry
= ADDRESS_ENTRY(table
, hash1
);
912 } while (PL_DHASH_ENTRY_IS_BUSY(entry
));
915 #endif /* PL_DHASHMETER */