2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Double hashing implementation.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "prbit.h"
|
|
|
|
#include "pldhash.h"
|
2012-03-02 14:20:44 -08:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsDebug.h" /* for PR_ASSERT */
|
2012-09-27 14:44:47 -07:00
|
|
|
#include "nsAlgorithm.h"
|
2012-10-26 06:32:10 -07:00
|
|
|
#include "mozilla/Likely.h"
|
2013-06-23 05:03:39 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifdef PL_DHASHMETER
|
|
|
|
# if defined MOZILLA_CLIENT && defined DEBUG_XXXbrendan
|
|
|
|
# include "nsTraceMalloc.h"
|
|
|
|
# endif
|
|
|
|
# define METER(x) x
|
|
|
|
#else
|
|
|
|
# define METER(x) /* nothing */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following DEBUG-only code is used to assert that calls to one of
|
|
|
|
* table->ops or to an enumerator do not cause re-entry into a call that
|
|
|
|
* can mutate the table. The recursion level is stored in additional
|
|
|
|
* space allocated at the end of the entry store to avoid changing
|
|
|
|
* PLDHashTable, which could cause issues when mixing DEBUG and
|
|
|
|
* non-DEBUG components.
|
|
|
|
*/
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
#define RECURSION_LEVEL(table_) (*(uint32_t*)(table_->entryStore + \
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHASH_TABLE_SIZE(table_) * \
|
|
|
|
table_->entrySize))
|
2009-01-10 08:28:16 -08:00
|
|
|
/*
|
|
|
|
* Most callers that assert about the recursion level don't care about
|
|
|
|
* this magical value because they are asserting that mutation is
|
|
|
|
* allowed (and therefore the level is 0 or 1, depending on whether they
|
|
|
|
* incremented it).
|
|
|
|
*
|
|
|
|
* Only PL_DHashTableFinish needs to allow this special value.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
#define IMMUTABLE_RECURSION_LEVEL ((uint32_t)-1)
|
2009-01-10 08:28:16 -08:00
|
|
|
|
|
|
|
#define RECURSION_LEVEL_SAFE_TO_FINISH(table_) \
|
|
|
|
(RECURSION_LEVEL(table_) == 0 || \
|
|
|
|
RECURSION_LEVEL(table_) == IMMUTABLE_RECURSION_LEVEL)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
#define ENTRY_STORE_EXTRA sizeof(uint32_t)
|
2009-01-10 08:28:16 -08:00
|
|
|
#define INCREMENT_RECURSION_LEVEL(table_) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
if (RECURSION_LEVEL(table_) != IMMUTABLE_RECURSION_LEVEL) \
|
|
|
|
++RECURSION_LEVEL(table_); \
|
2008-08-26 10:02:04 -07:00
|
|
|
PR_END_MACRO
|
2009-01-10 08:28:16 -08:00
|
|
|
#define DECREMENT_RECURSION_LEVEL(table_) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
if (RECURSION_LEVEL(table_) != IMMUTABLE_RECURSION_LEVEL) { \
|
2013-03-14 09:24:52 -07:00
|
|
|
MOZ_ASSERT(RECURSION_LEVEL(table_) > 0); \
|
2009-01-10 08:28:16 -08:00
|
|
|
--RECURSION_LEVEL(table_); \
|
|
|
|
} \
|
2008-08-26 10:02:04 -07:00
|
|
|
PR_END_MACRO
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define ENTRY_STORE_EXTRA 0
|
2008-08-26 10:02:04 -07:00
|
|
|
#define INCREMENT_RECURSION_LEVEL(table_) PR_BEGIN_MACRO PR_END_MACRO
|
|
|
|
#define DECREMENT_RECURSION_LEVEL(table_) PR_BEGIN_MACRO PR_END_MACRO
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#endif /* defined(DEBUG) */
|
|
|
|
|
2012-03-02 14:20:44 -08:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void *
|
2012-08-22 08:56:38 -07:00
|
|
|
PL_DHashAllocTable(PLDHashTable *table, uint32_t nbytes)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return malloc(nbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PL_DHashFreeTable(PLDHashTable *table, void *ptr)
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
PLDHashNumber
|
|
|
|
PL_DHashStringKey(PLDHashTable *table, const void *key)
|
|
|
|
{
|
2012-03-12 15:53:18 -07:00
|
|
|
return HashString(static_cast<const char*>(key));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PLDHashNumber
|
|
|
|
PL_DHashVoidPtrKeyStub(PLDHashTable *table, const void *key)
|
|
|
|
{
|
2012-08-09 00:09:31 -07:00
|
|
|
return (PLDHashNumber)(ptrdiff_t)key >> 2;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHashMatchEntryStub(PLDHashTable *table,
|
|
|
|
const PLDHashEntryHdr *entry,
|
|
|
|
const void *key)
|
|
|
|
{
|
|
|
|
const PLDHashEntryStub *stub = (const PLDHashEntryStub *)entry;
|
|
|
|
|
|
|
|
return stub->key == key;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHashMatchStringKey(PLDHashTable *table,
|
|
|
|
const PLDHashEntryHdr *entry,
|
|
|
|
const void *key)
|
|
|
|
{
|
|
|
|
const PLDHashEntryStub *stub = (const PLDHashEntryStub *)entry;
|
|
|
|
|
|
|
|
/* XXX tolerate null keys on account of sloppy Mozilla callers. */
|
|
|
|
return stub->key == key ||
|
2008-08-26 10:02:04 -07:00
|
|
|
(stub->key && key &&
|
|
|
|
strcmp((const char *) stub->key, (const char *) key) == 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PL_DHashMoveEntryStub(PLDHashTable *table,
|
|
|
|
const PLDHashEntryHdr *from,
|
|
|
|
PLDHashEntryHdr *to)
|
|
|
|
{
|
|
|
|
memcpy(to, from, table->entrySize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PL_DHashClearEntryStub(PLDHashTable *table, PLDHashEntryHdr *entry)
|
|
|
|
{
|
|
|
|
memset(entry, 0, table->entrySize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PL_DHashFreeStringKey(PLDHashTable *table, PLDHashEntryHdr *entry)
|
|
|
|
{
|
|
|
|
const PLDHashEntryStub *stub = (const PLDHashEntryStub *)entry;
|
|
|
|
|
|
|
|
free((void *) stub->key);
|
|
|
|
memset(entry, 0, table->entrySize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PL_DHashFinalizeStub(PLDHashTable *table)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static const PLDHashTableOps stub_ops = {
|
|
|
|
PL_DHashAllocTable,
|
|
|
|
PL_DHashFreeTable,
|
|
|
|
PL_DHashVoidPtrKeyStub,
|
|
|
|
PL_DHashMatchEntryStub,
|
|
|
|
PL_DHashMoveEntryStub,
|
|
|
|
PL_DHashClearEntryStub,
|
|
|
|
PL_DHashFinalizeStub,
|
2013-10-10 13:41:39 -07:00
|
|
|
nullptr
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const PLDHashTableOps *
|
|
|
|
PL_DHashGetStubOps(void)
|
|
|
|
{
|
|
|
|
return &stub_ops;
|
|
|
|
}
|
|
|
|
|
|
|
|
PLDHashTable *
|
2012-08-22 08:56:38 -07:00
|
|
|
PL_NewDHashTable(const PLDHashTableOps *ops, void *data, uint32_t entrySize,
|
|
|
|
uint32_t capacity)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
PLDHashTable *table;
|
|
|
|
|
|
|
|
table = (PLDHashTable *) malloc(sizeof *table);
|
|
|
|
if (!table)
|
2013-10-10 13:41:39 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!PL_DHashTableInit(table, ops, data, entrySize, capacity)) {
|
|
|
|
free(table);
|
2013-10-10 13:41:39 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PL_DHashTableDestroy(PLDHashTable *table)
|
|
|
|
{
|
|
|
|
PL_DHashTableFinish(table);
|
|
|
|
free(table);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHashTableInit(PLDHashTable *table, const PLDHashTableOps *ops, void *data,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t entrySize, uint32_t capacity)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
int log2;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t nbytes;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2012-03-16 10:09:14 -07:00
|
|
|
if (entrySize > 16 * sizeof(void *)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
printf_stderr(
|
|
|
|
"pldhash: for the table at address %p, the given entrySize"
|
2012-03-16 10:09:14 -07:00
|
|
|
" of %lu definitely favors chaining over double hashing.\n",
|
2008-08-26 10:02:04 -07:00
|
|
|
(void *) table,
|
2012-03-16 10:09:14 -07:00
|
|
|
(unsigned long) entrySize);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
table->ops = ops;
|
|
|
|
table->data = data;
|
|
|
|
if (capacity < PL_DHASH_MIN_SIZE)
|
|
|
|
capacity = PL_DHASH_MIN_SIZE;
|
|
|
|
|
|
|
|
PR_CEILING_LOG2(log2, capacity);
|
|
|
|
|
2012-10-19 19:01:43 -07:00
|
|
|
capacity = 1u << log2;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (capacity >= PL_DHASH_SIZE_LIMIT)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
table->hashShift = PL_DHASH_BITS - log2;
|
2012-08-22 08:56:38 -07:00
|
|
|
table->maxAlphaFrac = (uint8_t)(0x100 * PL_DHASH_DEFAULT_MAX_ALPHA);
|
|
|
|
table->minAlphaFrac = (uint8_t)(0x100 * PL_DHASH_DEFAULT_MIN_ALPHA);
|
2007-03-22 10:30:00 -07:00
|
|
|
table->entrySize = entrySize;
|
|
|
|
table->entryCount = table->removedCount = 0;
|
|
|
|
table->generation = 0;
|
|
|
|
nbytes = capacity * entrySize;
|
|
|
|
|
2008-08-26 10:02:04 -07:00
|
|
|
table->entryStore = (char *) ops->allocTable(table,
|
|
|
|
nbytes + ENTRY_STORE_EXTRA);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!table->entryStore)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
memset(table->entryStore, 0, nbytes);
|
|
|
|
METER(memset(&table->stats, 0, sizeof table->stats));
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
RECURSION_LEVEL(table) = 0;
|
|
|
|
#endif
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute max and min load numbers (entry counts) from table params.
|
|
|
|
*/
|
|
|
|
#define MAX_LOAD(table, size) (((table)->maxAlphaFrac * (size)) >> 8)
|
|
|
|
#define MIN_LOAD(table, size) (((table)->minAlphaFrac * (size)) >> 8)
|
|
|
|
|
|
|
|
void
|
|
|
|
PL_DHashTableSetAlphaBounds(PLDHashTable *table,
|
|
|
|
float maxAlpha,
|
|
|
|
float minAlpha)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t size;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Reject obviously insane bounds, rather than trying to guess what the
|
|
|
|
* buggy caller intended.
|
|
|
|
*/
|
|
|
|
NS_ASSERTION(0.5 <= maxAlpha && maxAlpha < 1 && 0 <= minAlpha,
|
|
|
|
"0.5 <= maxAlpha && maxAlpha < 1 && 0 <= minAlpha");
|
|
|
|
if (maxAlpha < 0.5 || 1 <= maxAlpha || minAlpha < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure that at least one entry will always be free. If maxAlpha at
|
|
|
|
* minimum size leaves no entries free, reduce maxAlpha based on minimum
|
|
|
|
* size and the precision limit of maxAlphaFrac's fixed point format.
|
|
|
|
*/
|
|
|
|
NS_ASSERTION(PL_DHASH_MIN_SIZE - (maxAlpha * PL_DHASH_MIN_SIZE) >= 1,
|
|
|
|
"PL_DHASH_MIN_SIZE - (maxAlpha * PL_DHASH_MIN_SIZE) >= 1");
|
|
|
|
if (PL_DHASH_MIN_SIZE - (maxAlpha * PL_DHASH_MIN_SIZE) < 1) {
|
|
|
|
maxAlpha = (float)
|
2013-01-15 04:22:03 -08:00
|
|
|
(PL_DHASH_MIN_SIZE - XPCOM_MAX(PL_DHASH_MIN_SIZE / 256, 1))
|
2007-03-22 10:30:00 -07:00
|
|
|
/ PL_DHASH_MIN_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure that minAlpha is strictly less than half maxAlpha. Take care
|
|
|
|
* not to truncate an entry's worth of alpha when storing in minAlphaFrac
|
|
|
|
* (8-bit fixed point format).
|
|
|
|
*/
|
|
|
|
NS_ASSERTION(minAlpha < maxAlpha / 2,
|
|
|
|
"minAlpha < maxAlpha / 2");
|
|
|
|
if (minAlpha >= maxAlpha / 2) {
|
|
|
|
size = PL_DHASH_TABLE_SIZE(table);
|
2013-01-15 04:22:03 -08:00
|
|
|
minAlpha = (size * maxAlpha - XPCOM_MAX(size / 256, 1u)) / (2 * size);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
table->maxAlphaFrac = (uint8_t)(maxAlpha * 256);
|
|
|
|
table->minAlphaFrac = (uint8_t)(minAlpha * 256);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Double hashing needs the second hash code to be relatively prime to table
|
|
|
|
* size, so we simply make hash2 odd.
|
|
|
|
*/
|
|
|
|
#define HASH1(hash0, shift) ((hash0) >> (shift))
|
|
|
|
#define HASH2(hash0,log2,shift) ((((hash0) << (log2)) >> (shift)) | 1)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reserve keyHash 0 for free entries and 1 for removed-entry sentinels. Note
|
|
|
|
* that a removed-entry sentinel need be stored only if the removed entry had
|
|
|
|
* a colliding entry added after it. Therefore we can use 1 as the collision
|
|
|
|
* flag in addition to the removed-entry sentinel value. Multiplicative hash
|
|
|
|
* uses the high order bits of keyHash, so this least-significant reservation
|
|
|
|
* should not hurt the hash function's effectiveness much.
|
|
|
|
*
|
|
|
|
* If you change any of these magic numbers, also update PL_DHASH_ENTRY_IS_LIVE
|
|
|
|
* in pldhash.h. It used to be private to pldhash.c, but then became public to
|
|
|
|
* assist iterator writers who inspect table->entryStore directly.
|
|
|
|
*/
|
|
|
|
#define COLLISION_FLAG ((PLDHashNumber) 1)
|
|
|
|
#define MARK_ENTRY_FREE(entry) ((entry)->keyHash = 0)
|
|
|
|
#define MARK_ENTRY_REMOVED(entry) ((entry)->keyHash = 1)
|
|
|
|
#define ENTRY_IS_REMOVED(entry) ((entry)->keyHash == 1)
|
|
|
|
#define ENTRY_IS_LIVE(entry) PL_DHASH_ENTRY_IS_LIVE(entry)
|
|
|
|
#define ENSURE_LIVE_KEYHASH(hash0) if (hash0 < 2) hash0 -= 2; else (void)0
|
|
|
|
|
|
|
|
/* Match an entry's keyHash against an unstored one computed from a key. */
|
|
|
|
#define MATCH_ENTRY_KEYHASH(entry,hash0) \
|
|
|
|
(((entry)->keyHash & ~COLLISION_FLAG) == (hash0))
|
|
|
|
|
|
|
|
/* Compute the address of the indexed entry in table. */
|
|
|
|
#define ADDRESS_ENTRY(table, index) \
|
|
|
|
((PLDHashEntryHdr *)((table)->entryStore + (index) * (table)->entrySize))
|
|
|
|
|
|
|
|
void
|
|
|
|
PL_DHashTableFinish(PLDHashTable *table)
|
|
|
|
{
|
|
|
|
char *entryAddr, *entryLimit;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t entrySize;
|
2007-03-22 10:30:00 -07:00
|
|
|
PLDHashEntryHdr *entry;
|
|
|
|
|
|
|
|
#ifdef DEBUG_XXXbrendan
|
2013-10-10 13:41:39 -07:00
|
|
|
static FILE *dumpfp = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!dumpfp) dumpfp = fopen("/tmp/pldhash.bigdump", "w");
|
|
|
|
if (dumpfp) {
|
|
|
|
#ifdef MOZILLA_CLIENT
|
|
|
|
NS_TraceStack(1, dumpfp);
|
|
|
|
#endif
|
2013-10-10 13:41:39 -07:00
|
|
|
PL_DHashTableDumpMeter(table, nullptr, dumpfp);
|
2007-03-22 10:30:00 -07:00
|
|
|
fputc('\n', dumpfp);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
INCREMENT_RECURSION_LEVEL(table);
|
|
|
|
|
|
|
|
/* Call finalize before clearing entries, so it can enumerate them. */
|
|
|
|
table->ops->finalize(table);
|
|
|
|
|
|
|
|
/* Clear any remaining live entries. */
|
|
|
|
entryAddr = table->entryStore;
|
|
|
|
entrySize = table->entrySize;
|
|
|
|
entryLimit = entryAddr + PL_DHASH_TABLE_SIZE(table) * entrySize;
|
|
|
|
while (entryAddr < entryLimit) {
|
|
|
|
entry = (PLDHashEntryHdr *)entryAddr;
|
|
|
|
if (ENTRY_IS_LIVE(entry)) {
|
|
|
|
METER(table->stats.removeEnums++);
|
|
|
|
table->ops->clearEntry(table, entry);
|
|
|
|
}
|
|
|
|
entryAddr += entrySize;
|
|
|
|
}
|
|
|
|
|
|
|
|
DECREMENT_RECURSION_LEVEL(table);
|
2013-03-14 09:24:52 -07:00
|
|
|
MOZ_ASSERT(RECURSION_LEVEL_SAFE_TO_FINISH(table));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Free entry storage last. */
|
|
|
|
table->ops->freeTable(table, table->entryStore);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashEntryHdr * PL_DHASH_FASTCALL
|
|
|
|
SearchTable(PLDHashTable *table, const void *key, PLDHashNumber keyHash,
|
|
|
|
PLDHashOperator op)
|
|
|
|
{
|
|
|
|
PLDHashNumber hash1, hash2;
|
|
|
|
int hashShift, sizeLog2;
|
|
|
|
PLDHashEntryHdr *entry, *firstRemoved;
|
|
|
|
PLDHashMatchEntry matchEntry;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t sizeMask;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
METER(table->stats.searches++);
|
|
|
|
NS_ASSERTION(!(keyHash & COLLISION_FLAG),
|
|
|
|
"!(keyHash & COLLISION_FLAG)");
|
|
|
|
|
|
|
|
/* Compute the primary hash address. */
|
|
|
|
hashShift = table->hashShift;
|
|
|
|
hash1 = HASH1(keyHash, hashShift);
|
|
|
|
entry = ADDRESS_ENTRY(table, hash1);
|
|
|
|
|
|
|
|
/* Miss: return space for a new entry. */
|
|
|
|
if (PL_DHASH_ENTRY_IS_FREE(entry)) {
|
|
|
|
METER(table->stats.misses++);
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Hit: return entry. */
|
|
|
|
matchEntry = table->ops->matchEntry;
|
|
|
|
if (MATCH_ENTRY_KEYHASH(entry, keyHash) && matchEntry(table, entry, key)) {
|
|
|
|
METER(table->stats.hits++);
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Collision: double hash. */
|
|
|
|
sizeLog2 = PL_DHASH_BITS - table->hashShift;
|
|
|
|
hash2 = HASH2(keyHash, sizeLog2, hashShift);
|
2012-10-19 19:01:43 -07:00
|
|
|
sizeMask = (1u << sizeLog2) - 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Save the first removed entry pointer so PL_DHASH_ADD can recycle it. */
|
2013-10-10 13:41:39 -07:00
|
|
|
firstRemoved = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
for (;;) {
|
2012-10-26 06:32:10 -07:00
|
|
|
if (MOZ_UNLIKELY(ENTRY_IS_REMOVED(entry))) {
|
2007-03-27 08:32:38 -07:00
|
|
|
if (!firstRemoved)
|
|
|
|
firstRemoved = entry;
|
|
|
|
} else {
|
|
|
|
if (op == PL_DHASH_ADD)
|
|
|
|
entry->keyHash |= COLLISION_FLAG;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
METER(table->stats.steps++);
|
|
|
|
hash1 -= hash2;
|
|
|
|
hash1 &= sizeMask;
|
|
|
|
|
|
|
|
entry = ADDRESS_ENTRY(table, hash1);
|
|
|
|
if (PL_DHASH_ENTRY_IS_FREE(entry)) {
|
|
|
|
METER(table->stats.misses++);
|
|
|
|
return (firstRemoved && op == PL_DHASH_ADD) ? firstRemoved : entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MATCH_ENTRY_KEYHASH(entry, keyHash) &&
|
|
|
|
matchEntry(table, entry, key)) {
|
|
|
|
METER(table->stats.hits++);
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTREACHED */
|
2013-10-10 13:41:39 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-03-23 16:32:40 -07:00
|
|
|
/*
|
|
|
|
* This is a copy of SearchTable, used by ChangeTable, hardcoded to
|
|
|
|
* 1. assume |op == PL_DHASH_ADD|,
|
|
|
|
* 2. assume that |key| will never match an existing entry, and
|
|
|
|
* 3. assume that no entries have been removed from the current table
|
|
|
|
* structure.
|
|
|
|
* Avoiding the need for |key| means we can avoid needing a way to map
|
|
|
|
* entries to keys, which means callers can use complex key types more
|
|
|
|
* easily.
|
|
|
|
*/
|
|
|
|
static PLDHashEntryHdr * PL_DHASH_FASTCALL
|
|
|
|
FindFreeEntry(PLDHashTable *table, PLDHashNumber keyHash)
|
|
|
|
{
|
|
|
|
PLDHashNumber hash1, hash2;
|
|
|
|
int hashShift, sizeLog2;
|
|
|
|
PLDHashEntryHdr *entry;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t sizeMask;
|
2007-03-23 16:32:40 -07:00
|
|
|
|
|
|
|
METER(table->stats.searches++);
|
|
|
|
NS_ASSERTION(!(keyHash & COLLISION_FLAG),
|
|
|
|
"!(keyHash & COLLISION_FLAG)");
|
|
|
|
|
|
|
|
/* Compute the primary hash address. */
|
|
|
|
hashShift = table->hashShift;
|
|
|
|
hash1 = HASH1(keyHash, hashShift);
|
|
|
|
entry = ADDRESS_ENTRY(table, hash1);
|
|
|
|
|
|
|
|
/* Miss: return space for a new entry. */
|
|
|
|
if (PL_DHASH_ENTRY_IS_FREE(entry)) {
|
|
|
|
METER(table->stats.misses++);
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Collision: double hash. */
|
|
|
|
sizeLog2 = PL_DHASH_BITS - table->hashShift;
|
|
|
|
hash2 = HASH2(keyHash, sizeLog2, hashShift);
|
2012-10-19 19:01:43 -07:00
|
|
|
sizeMask = (1u << sizeLog2) - 1;
|
2007-03-23 16:32:40 -07:00
|
|
|
|
|
|
|
for (;;) {
|
2007-03-27 08:32:38 -07:00
|
|
|
NS_ASSERTION(!ENTRY_IS_REMOVED(entry),
|
|
|
|
"!ENTRY_IS_REMOVED(entry)");
|
|
|
|
entry->keyHash |= COLLISION_FLAG;
|
|
|
|
|
2007-03-23 16:32:40 -07:00
|
|
|
METER(table->stats.steps++);
|
|
|
|
hash1 -= hash2;
|
|
|
|
hash1 &= sizeMask;
|
|
|
|
|
|
|
|
entry = ADDRESS_ENTRY(table, hash1);
|
|
|
|
if (PL_DHASH_ENTRY_IS_FREE(entry)) {
|
|
|
|
METER(table->stats.misses++);
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTREACHED */
|
2013-10-10 13:41:39 -07:00
|
|
|
return nullptr;
|
2007-03-23 16:32:40 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
ChangeTable(PLDHashTable *table, int deltaLog2)
|
|
|
|
{
|
|
|
|
int oldLog2, newLog2;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t oldCapacity, newCapacity;
|
2007-03-22 10:30:00 -07:00
|
|
|
char *newEntryStore, *oldEntryStore, *oldEntryAddr;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t entrySize, i, nbytes;
|
2007-03-22 10:30:00 -07:00
|
|
|
PLDHashEntryHdr *oldEntry, *newEntry;
|
|
|
|
PLDHashMoveEntry moveEntry;
|
|
|
|
#ifdef DEBUG
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t recursionLevel;
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Look, but don't touch, until we succeed in getting new entry store. */
|
|
|
|
oldLog2 = PL_DHASH_BITS - table->hashShift;
|
|
|
|
newLog2 = oldLog2 + deltaLog2;
|
2012-10-19 19:01:43 -07:00
|
|
|
oldCapacity = 1u << oldLog2;
|
|
|
|
newCapacity = 1u << newLog2;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (newCapacity >= PL_DHASH_SIZE_LIMIT)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
entrySize = table->entrySize;
|
|
|
|
nbytes = newCapacity * entrySize;
|
|
|
|
|
2008-08-26 10:02:04 -07:00
|
|
|
newEntryStore = (char *) table->ops->allocTable(table,
|
|
|
|
nbytes + ENTRY_STORE_EXTRA);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!newEntryStore)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* We can't fail from here on, so update table parameters. */
|
|
|
|
#ifdef DEBUG
|
|
|
|
recursionLevel = RECURSION_LEVEL(table);
|
|
|
|
#endif
|
|
|
|
table->hashShift = PL_DHASH_BITS - newLog2;
|
|
|
|
table->removedCount = 0;
|
|
|
|
table->generation++;
|
|
|
|
|
|
|
|
/* Assign the new entry store to table. */
|
|
|
|
memset(newEntryStore, 0, nbytes);
|
|
|
|
oldEntryAddr = oldEntryStore = table->entryStore;
|
|
|
|
table->entryStore = newEntryStore;
|
|
|
|
moveEntry = table->ops->moveEntry;
|
|
|
|
#ifdef DEBUG
|
|
|
|
RECURSION_LEVEL(table) = recursionLevel;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Copy only live entries, leaving removed ones behind. */
|
|
|
|
for (i = 0; i < oldCapacity; i++) {
|
|
|
|
oldEntry = (PLDHashEntryHdr *)oldEntryAddr;
|
|
|
|
if (ENTRY_IS_LIVE(oldEntry)) {
|
|
|
|
oldEntry->keyHash &= ~COLLISION_FLAG;
|
2007-03-23 16:32:40 -07:00
|
|
|
newEntry = FindFreeEntry(table, oldEntry->keyHash);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(PL_DHASH_ENTRY_IS_FREE(newEntry),
|
|
|
|
"PL_DHASH_ENTRY_IS_FREE(newEntry)");
|
|
|
|
moveEntry(table, oldEntry, newEntry);
|
|
|
|
newEntry->keyHash = oldEntry->keyHash;
|
|
|
|
}
|
|
|
|
oldEntryAddr += entrySize;
|
|
|
|
}
|
|
|
|
|
|
|
|
table->ops->freeTable(table, oldEntryStore);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PLDHashEntryHdr * PL_DHASH_FASTCALL
|
|
|
|
PL_DHashTableOperate(PLDHashTable *table, const void *key, PLDHashOperator op)
|
|
|
|
{
|
|
|
|
PLDHashNumber keyHash;
|
|
|
|
PLDHashEntryHdr *entry;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t size;
|
2007-03-22 10:30:00 -07:00
|
|
|
int deltaLog2;
|
|
|
|
|
2013-03-14 09:24:52 -07:00
|
|
|
MOZ_ASSERT(op == PL_DHASH_LOOKUP || RECURSION_LEVEL(table) == 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
INCREMENT_RECURSION_LEVEL(table);
|
|
|
|
|
|
|
|
keyHash = table->ops->hashKey(table, key);
|
|
|
|
keyHash *= PL_DHASH_GOLDEN_RATIO;
|
|
|
|
|
|
|
|
/* Avoid 0 and 1 hash codes, they indicate free and removed entries. */
|
|
|
|
ENSURE_LIVE_KEYHASH(keyHash);
|
|
|
|
keyHash &= ~COLLISION_FLAG;
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case PL_DHASH_LOOKUP:
|
|
|
|
METER(table->stats.lookups++);
|
|
|
|
entry = SearchTable(table, key, keyHash, op);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PL_DHASH_ADD:
|
|
|
|
/*
|
|
|
|
* If alpha is >= .75, grow or compress the table. If key is already
|
|
|
|
* in the table, we may grow once more than necessary, but only if we
|
|
|
|
* are on the edge of being overloaded.
|
|
|
|
*/
|
|
|
|
size = PL_DHASH_TABLE_SIZE(table);
|
|
|
|
if (table->entryCount + table->removedCount >= MAX_LOAD(table, size)) {
|
|
|
|
/* Compress if a quarter or more of all entries are removed. */
|
|
|
|
if (table->removedCount >= size >> 2) {
|
|
|
|
METER(table->stats.compresses++);
|
|
|
|
deltaLog2 = 0;
|
|
|
|
} else {
|
|
|
|
METER(table->stats.grows++);
|
|
|
|
deltaLog2 = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Grow or compress table, returning null if ChangeTable fails and
|
|
|
|
* falling through might claim the last free entry.
|
|
|
|
*/
|
|
|
|
if (!ChangeTable(table, deltaLog2) &&
|
|
|
|
table->entryCount + table->removedCount == size - 1) {
|
|
|
|
METER(table->stats.addFailures++);
|
2013-10-10 13:41:39 -07:00
|
|
|
entry = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for entry after possibly growing, so we don't have to add it,
|
|
|
|
* then skip it while growing the table and re-add it after.
|
|
|
|
*/
|
|
|
|
entry = SearchTable(table, key, keyHash, op);
|
|
|
|
if (!ENTRY_IS_LIVE(entry)) {
|
|
|
|
/* Initialize the entry, indicating that it's no longer free. */
|
|
|
|
METER(table->stats.addMisses++);
|
|
|
|
if (ENTRY_IS_REMOVED(entry)) {
|
|
|
|
METER(table->stats.addOverRemoved++);
|
|
|
|
table->removedCount--;
|
|
|
|
keyHash |= COLLISION_FLAG;
|
|
|
|
}
|
|
|
|
if (table->ops->initEntry &&
|
|
|
|
!table->ops->initEntry(table, entry, key)) {
|
|
|
|
/* We haven't claimed entry yet; fail with null return. */
|
|
|
|
memset(entry + 1, 0, table->entrySize - sizeof *entry);
|
2013-10-10 13:41:39 -07:00
|
|
|
entry = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
entry->keyHash = keyHash;
|
|
|
|
table->entryCount++;
|
|
|
|
}
|
|
|
|
METER(else table->stats.addHits++);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PL_DHASH_REMOVE:
|
|
|
|
entry = SearchTable(table, key, keyHash, op);
|
|
|
|
if (ENTRY_IS_LIVE(entry)) {
|
|
|
|
/* Clear this entry and mark it as "removed". */
|
|
|
|
METER(table->stats.removeHits++);
|
|
|
|
PL_DHashTableRawRemove(table, entry);
|
|
|
|
|
|
|
|
/* Shrink if alpha is <= .25 and table isn't too small already. */
|
|
|
|
size = PL_DHASH_TABLE_SIZE(table);
|
|
|
|
if (size > PL_DHASH_MIN_SIZE &&
|
|
|
|
table->entryCount <= MIN_LOAD(table, size)) {
|
|
|
|
METER(table->stats.shrinks++);
|
|
|
|
(void) ChangeTable(table, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
METER(else table->stats.removeMisses++);
|
2013-10-10 13:41:39 -07:00
|
|
|
entry = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("0");
|
2013-10-10 13:41:39 -07:00
|
|
|
entry = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
DECREMENT_RECURSION_LEVEL(table);
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PL_DHashTableRawRemove(PLDHashTable *table, PLDHashEntryHdr *entry)
|
|
|
|
{
|
|
|
|
PLDHashNumber keyHash; /* load first in case clearEntry goofs it */
|
|
|
|
|
2013-03-14 09:24:52 -07:00
|
|
|
MOZ_ASSERT(RECURSION_LEVEL(table) != IMMUTABLE_RECURSION_LEVEL);
|
2009-01-10 08:28:16 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(PL_DHASH_ENTRY_IS_LIVE(entry),
|
|
|
|
"PL_DHASH_ENTRY_IS_LIVE(entry)");
|
|
|
|
keyHash = entry->keyHash;
|
|
|
|
table->ops->clearEntry(table, entry);
|
|
|
|
if (keyHash & COLLISION_FLAG) {
|
|
|
|
MARK_ENTRY_REMOVED(entry);
|
|
|
|
table->removedCount++;
|
|
|
|
} else {
|
|
|
|
METER(table->stats.removeFrees++);
|
|
|
|
MARK_ENTRY_FREE(entry);
|
|
|
|
}
|
|
|
|
table->entryCount--;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHashTableEnumerate(PLDHashTable *table, PLDHashEnumerator etor, void *arg)
|
|
|
|
{
|
|
|
|
char *entryAddr, *entryLimit;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i, capacity, entrySize, ceiling;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool didRemove;
|
2007-03-22 10:30:00 -07:00
|
|
|
PLDHashEntryHdr *entry;
|
|
|
|
PLDHashOperator op;
|
|
|
|
|
|
|
|
INCREMENT_RECURSION_LEVEL(table);
|
|
|
|
|
|
|
|
entryAddr = table->entryStore;
|
|
|
|
entrySize = table->entrySize;
|
|
|
|
capacity = PL_DHASH_TABLE_SIZE(table);
|
|
|
|
entryLimit = entryAddr + capacity * entrySize;
|
|
|
|
i = 0;
|
2011-10-17 07:59:28 -07:00
|
|
|
didRemove = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
while (entryAddr < entryLimit) {
|
|
|
|
entry = (PLDHashEntryHdr *)entryAddr;
|
|
|
|
if (ENTRY_IS_LIVE(entry)) {
|
|
|
|
op = etor(table, entry, i++, arg);
|
|
|
|
if (op & PL_DHASH_REMOVE) {
|
|
|
|
METER(table->stats.removeEnums++);
|
|
|
|
PL_DHashTableRawRemove(table, entry);
|
2011-10-17 07:59:28 -07:00
|
|
|
didRemove = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (op & PL_DHASH_STOP)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
entryAddr += entrySize;
|
|
|
|
}
|
|
|
|
|
2013-03-14 09:24:52 -07:00
|
|
|
MOZ_ASSERT(!didRemove || RECURSION_LEVEL(table) == 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Shrink or compress if a quarter or more of all entries are removed, or
|
|
|
|
* if the table is underloaded according to the configured minimum alpha,
|
|
|
|
* and is not minimal-size already. Do this only if we removed above, so
|
|
|
|
* non-removing enumerations can count on stable table->entryStore until
|
|
|
|
* the next non-lookup-Operate or removing-Enumerate.
|
|
|
|
*/
|
|
|
|
if (didRemove &&
|
|
|
|
(table->removedCount >= capacity >> 2 ||
|
|
|
|
(capacity > PL_DHASH_MIN_SIZE &&
|
|
|
|
table->entryCount <= MIN_LOAD(table, capacity)))) {
|
|
|
|
METER(table->stats.enumShrinks++);
|
|
|
|
capacity = table->entryCount;
|
|
|
|
capacity += capacity >> 1;
|
|
|
|
if (capacity < PL_DHASH_MIN_SIZE)
|
|
|
|
capacity = PL_DHASH_MIN_SIZE;
|
|
|
|
|
|
|
|
PR_CEILING_LOG2(ceiling, capacity);
|
|
|
|
ceiling -= PL_DHASH_BITS - table->hashShift;
|
|
|
|
|
|
|
|
(void) ChangeTable(table, ceiling);
|
|
|
|
}
|
|
|
|
|
|
|
|
DECREMENT_RECURSION_LEVEL(table);
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2011-12-15 14:59:53 -08:00
|
|
|
struct SizeOfEntryExcludingThisArg
|
2011-12-05 14:24:28 -08:00
|
|
|
{
|
|
|
|
size_t total;
|
2011-12-15 14:59:53 -08:00
|
|
|
PLDHashSizeOfEntryExcludingThisFun sizeOfEntryExcludingThis;
|
2013-06-23 05:03:39 -07:00
|
|
|
MallocSizeOf mallocSizeOf;
|
2011-12-15 14:59:53 -08:00
|
|
|
void *arg; // the arg passed by the user
|
2011-12-05 14:24:28 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
static PLDHashOperator
|
2011-12-15 14:59:53 -08:00
|
|
|
SizeOfEntryExcludingThisEnumerator(PLDHashTable *table, PLDHashEntryHdr *hdr,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t number, void *arg)
|
2011-12-05 14:24:28 -08:00
|
|
|
{
|
2011-12-15 14:59:53 -08:00
|
|
|
SizeOfEntryExcludingThisArg *e = (SizeOfEntryExcludingThisArg *)arg;
|
|
|
|
e->total += e->sizeOfEntryExcludingThis(hdr, e->mallocSizeOf, e->arg);
|
2011-12-05 14:24:28 -08:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2011-12-08 21:01:52 -08:00
|
|
|
PL_DHashTableSizeOfExcludingThis(const PLDHashTable *table,
|
2011-12-15 14:59:53 -08:00
|
|
|
PLDHashSizeOfEntryExcludingThisFun sizeOfEntryExcludingThis,
|
2013-06-23 05:03:39 -07:00
|
|
|
MallocSizeOf mallocSizeOf,
|
2013-10-10 13:41:39 -07:00
|
|
|
void *arg /* = nullptr */)
|
2011-12-05 14:24:28 -08:00
|
|
|
{
|
|
|
|
size_t n = 0;
|
2012-01-25 00:52:51 -08:00
|
|
|
n += mallocSizeOf(table->entryStore);
|
2011-12-15 14:59:53 -08:00
|
|
|
if (sizeOfEntryExcludingThis) {
|
|
|
|
SizeOfEntryExcludingThisArg arg2 = { 0, sizeOfEntryExcludingThis, mallocSizeOf, arg };
|
2011-12-08 21:01:52 -08:00
|
|
|
PL_DHashTableEnumerate(const_cast<PLDHashTable *>(table),
|
2011-12-15 14:59:53 -08:00
|
|
|
SizeOfEntryExcludingThisEnumerator, &arg2);
|
|
|
|
n += arg2.total;
|
2011-12-05 14:24:28 -08:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2011-11-27 19:03:14 -08:00
|
|
|
size_t
|
2011-12-08 21:01:52 -08:00
|
|
|
PL_DHashTableSizeOfIncludingThis(const PLDHashTable *table,
|
2011-12-15 14:59:53 -08:00
|
|
|
PLDHashSizeOfEntryExcludingThisFun sizeOfEntryExcludingThis,
|
2013-06-23 05:03:39 -07:00
|
|
|
MallocSizeOf mallocSizeOf,
|
2013-10-10 13:41:39 -07:00
|
|
|
void *arg /* = nullptr */)
|
2011-09-14 17:37:45 -07:00
|
|
|
{
|
2012-01-25 00:52:51 -08:00
|
|
|
return mallocSizeOf(table) +
|
2011-12-15 14:59:53 -08:00
|
|
|
PL_DHashTableSizeOfExcludingThis(table, sizeOfEntryExcludingThis,
|
|
|
|
mallocSizeOf, arg);
|
2011-09-14 17:37:45 -07:00
|
|
|
}
|
|
|
|
|
2009-01-10 08:28:16 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
PL_DHashMarkTableImmutable(PLDHashTable *table)
|
|
|
|
{
|
|
|
|
RECURSION_LEVEL(table) = IMMUTABLE_RECURSION_LEVEL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef PL_DHASHMETER
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
void
|
|
|
|
PL_DHashTableDumpMeter(PLDHashTable *table, PLDHashEnumerator dump, FILE *fp)
|
|
|
|
{
|
|
|
|
char *entryAddr;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t entrySize, entryCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
int hashShift, sizeLog2;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i, tableSize, sizeMask, chainLen, maxChainLen, chainCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
PLDHashNumber hash1, hash2, saveHash1, maxChainHash1, maxChainHash2;
|
|
|
|
double sqsum, mean, variance, sigma;
|
|
|
|
PLDHashEntryHdr *entry, *probe;
|
|
|
|
|
|
|
|
entryAddr = table->entryStore;
|
|
|
|
entrySize = table->entrySize;
|
|
|
|
hashShift = table->hashShift;
|
|
|
|
sizeLog2 = PL_DHASH_BITS - hashShift;
|
|
|
|
tableSize = PL_DHASH_TABLE_SIZE(table);
|
2012-10-19 19:01:43 -07:00
|
|
|
sizeMask = (1u << sizeLog2) - 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
chainCount = maxChainLen = 0;
|
|
|
|
hash2 = 0;
|
|
|
|
sqsum = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < tableSize; i++) {
|
|
|
|
entry = (PLDHashEntryHdr *)entryAddr;
|
|
|
|
entryAddr += entrySize;
|
|
|
|
if (!ENTRY_IS_LIVE(entry))
|
|
|
|
continue;
|
|
|
|
hash1 = HASH1(entry->keyHash & ~COLLISION_FLAG, hashShift);
|
|
|
|
saveHash1 = hash1;
|
|
|
|
probe = ADDRESS_ENTRY(table, hash1);
|
|
|
|
chainLen = 1;
|
|
|
|
if (probe == entry) {
|
|
|
|
/* Start of a (possibly unit-length) chain. */
|
|
|
|
chainCount++;
|
|
|
|
} else {
|
|
|
|
hash2 = HASH2(entry->keyHash & ~COLLISION_FLAG, sizeLog2,
|
|
|
|
hashShift);
|
|
|
|
do {
|
|
|
|
chainLen++;
|
|
|
|
hash1 -= hash2;
|
|
|
|
hash1 &= sizeMask;
|
|
|
|
probe = ADDRESS_ENTRY(table, hash1);
|
|
|
|
} while (probe != entry);
|
|
|
|
}
|
|
|
|
sqsum += chainLen * chainLen;
|
|
|
|
if (chainLen > maxChainLen) {
|
|
|
|
maxChainLen = chainLen;
|
|
|
|
maxChainHash1 = saveHash1;
|
|
|
|
maxChainHash2 = hash2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entryCount = table->entryCount;
|
|
|
|
if (entryCount && chainCount) {
|
|
|
|
mean = (double)entryCount / chainCount;
|
|
|
|
variance = chainCount * sqsum - entryCount * entryCount;
|
|
|
|
if (variance < 0 || chainCount == 1)
|
|
|
|
variance = 0;
|
|
|
|
else
|
|
|
|
variance /= chainCount * (chainCount - 1);
|
|
|
|
sigma = sqrt(variance);
|
|
|
|
} else {
|
|
|
|
mean = sigma = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fp, "Double hashing statistics:\n");
|
|
|
|
fprintf(fp, " table size (in entries): %u\n", tableSize);
|
|
|
|
fprintf(fp, " number of entries: %u\n", table->entryCount);
|
|
|
|
fprintf(fp, " number of removed entries: %u\n", table->removedCount);
|
|
|
|
fprintf(fp, " number of searches: %u\n", table->stats.searches);
|
|
|
|
fprintf(fp, " number of hits: %u\n", table->stats.hits);
|
|
|
|
fprintf(fp, " number of misses: %u\n", table->stats.misses);
|
|
|
|
fprintf(fp, " mean steps per search: %g\n", table->stats.searches ?
|
|
|
|
(double)table->stats.steps
|
|
|
|
/ table->stats.searches :
|
|
|
|
0.);
|
|
|
|
fprintf(fp, " mean hash chain length: %g\n", mean);
|
|
|
|
fprintf(fp, " standard deviation: %g\n", sigma);
|
|
|
|
fprintf(fp, " maximum hash chain length: %u\n", maxChainLen);
|
|
|
|
fprintf(fp, " number of lookups: %u\n", table->stats.lookups);
|
|
|
|
fprintf(fp, " adds that made a new entry: %u\n", table->stats.addMisses);
|
|
|
|
fprintf(fp, "adds that recycled removeds: %u\n", table->stats.addOverRemoved);
|
|
|
|
fprintf(fp, " adds that found an entry: %u\n", table->stats.addHits);
|
|
|
|
fprintf(fp, " add failures: %u\n", table->stats.addFailures);
|
|
|
|
fprintf(fp, " useful removes: %u\n", table->stats.removeHits);
|
|
|
|
fprintf(fp, " useless removes: %u\n", table->stats.removeMisses);
|
|
|
|
fprintf(fp, "removes that freed an entry: %u\n", table->stats.removeFrees);
|
|
|
|
fprintf(fp, " removes while enumerating: %u\n", table->stats.removeEnums);
|
|
|
|
fprintf(fp, " number of grows: %u\n", table->stats.grows);
|
|
|
|
fprintf(fp, " number of shrinks: %u\n", table->stats.shrinks);
|
|
|
|
fprintf(fp, " number of compresses: %u\n", table->stats.compresses);
|
|
|
|
fprintf(fp, "number of enumerate shrinks: %u\n", table->stats.enumShrinks);
|
|
|
|
|
|
|
|
if (dump && maxChainLen && hash2) {
|
|
|
|
fputs("Maximum hash chain:\n", fp);
|
|
|
|
hash1 = maxChainHash1;
|
|
|
|
hash2 = maxChainHash2;
|
|
|
|
entry = ADDRESS_ENTRY(table, hash1);
|
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
if (dump(table, entry, i++, fp) != PL_DHASH_NEXT)
|
|
|
|
break;
|
|
|
|
hash1 -= hash2;
|
|
|
|
hash1 &= sizeMask;
|
|
|
|
entry = ADDRESS_ENTRY(table, hash1);
|
|
|
|
} while (PL_DHASH_ENTRY_IS_BUSY(entry));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* PL_DHASHMETER */
|