Bug 927705 (part 1) - Remove PL_DHashTableSetAlphaBounds() and the supporting machinery. r=jorendorff.

--HG--
extra : rebase_source : 0b97b3276ec91d417fcfc12fe871d93b0cbd2263
This commit is contained in:
Nicholas Nethercote 2013-10-20 20:17:48 -07:00
parent ae2decfeda
commit 5b2c2e1985
2 changed files with 15 additions and 106 deletions

@ -220,8 +220,6 @@ PL_DHashTableInit(PLDHashTable *table, const PLDHashTableOps *ops, void *data,
if (capacity >= PL_DHASH_SIZE_LIMIT)
return false;
table->hashShift = PL_DHASH_BITS - log2;
table->maxAlphaFrac = (uint8_t)(0x100 * PL_DHASH_DEFAULT_MAX_ALPHA);
table->minAlphaFrac = (uint8_t)(0x100 * PL_DHASH_DEFAULT_MIN_ALPHA);
table->entrySize = entrySize;
table->entryCount = table->removedCount = 0;
table->generation = 0;
@ -242,54 +240,13 @@ PL_DHashTableInit(PLDHashTable *table, const PLDHashTableOps *ops, void *data,
}
/*
* Compute max and min load numbers (entry counts) from table params.
* Compute max and min load numbers (entry counts).
*/
#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)
{
uint32_t size;
/*
* 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)
(PL_DHASH_MIN_SIZE - XPCOM_MAX(PL_DHASH_MIN_SIZE / 256, 1))
/ 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);
minAlpha = (size * maxAlpha - XPCOM_MAX(size / 256, 1u)) / (2 * size);
}
table->maxAlphaFrac = (uint8_t)(maxAlpha * 256);
table->minAlphaFrac = (uint8_t)(minAlpha * 256);
static inline uint32_t MaxLoad(uint32_t size) {
return size - (size >> 2); // == size * 0.75
}
static inline uint32_t MinLoad(uint32_t size) {
return size >> 2; // == size * 0.25
}
/*
@ -592,7 +549,7 @@ PL_DHashTableOperate(PLDHashTable *table, const void *key, PLDHashOperator op)
* are on the edge of being overloaded.
*/
size = PL_DHASH_TABLE_SIZE(table);
if (table->entryCount + table->removedCount >= MAX_LOAD(table, size)) {
if (table->entryCount + table->removedCount >= MaxLoad(size)) {
/* Compress if a quarter or more of all entries are removed. */
if (table->removedCount >= size >> 2) {
METER(table->stats.compresses++);
@ -650,7 +607,7 @@ PL_DHashTableOperate(PLDHashTable *table, const void *key, PLDHashOperator op)
/* 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)) {
table->entryCount <= MinLoad(size)) {
METER(table->stats.shrinks++);
(void) ChangeTable(table, -1);
}
@ -726,15 +683,15 @@ PL_DHashTableEnumerate(PLDHashTable *table, PLDHashEnumerator etor, void *arg)
/*
* 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 the table is underloaded according to the 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)))) {
table->entryCount <= MinLoad(capacity)))) {
METER(table->stats.enumShrinks++);
capacity = table->entryCount;
capacity += capacity >> 1;

@ -28,7 +28,7 @@ extern "C" {
#define PL_DHASHMETER 1
#endif
/* Table size limit, do not equal or exceed (see min&maxAlphaFrac, below). */
/* Table size limit, do not equal or exceed. */
#undef PL_DHASH_SIZE_LIMIT
#define PL_DHASH_SIZE_LIMIT ((uint32_t)1 << 24)
@ -159,7 +159,7 @@ PL_DHASH_ENTRY_IS_LIVE(PLDHashEntryHdr* entry)
* assuming esize is not too large (in which case, chaining should probably be
* used for any alpha). For esize=2 and k=3, we want alpha >= .2; for esize=3
* and k=2, we want alpha >= .4. For k=4, esize could be 6, and alpha >= .5
* would still obtain. See the PL_DHASH_MIN_ALPHA macro further below.
* would still obtain.
*
* The current implementation uses a configurable lower bound on alpha, which
* defaults to .25, when deciding to shrink the table (while still respecting
@ -180,8 +180,6 @@ struct PLDHashTable {
const PLDHashTableOps *ops; /* virtual operations, see below */
void *data; /* ops- and instance-specific data */
int16_t hashShift; /* multiplicative hash shift */
uint8_t maxAlphaFrac; /* 8-bit fixed point max alpha */
uint8_t minAlphaFrac; /* 8-bit fixed point min alpha */
uint32_t entrySize; /* number of bytes in an entry */
uint32_t entryCount; /* number of entries in table */
uint32_t removedCount; /* removed entry sentinels in table */
@ -402,52 +400,6 @@ NS_COM_GLUE bool
PL_DHashTableInit(PLDHashTable *table, const PLDHashTableOps *ops, void *data,
uint32_t entrySize, uint32_t capacity);
/*
* Set maximum and minimum alpha for table. The defaults are 0.75 and .25.
* maxAlpha must be in [0.5, 0.9375] for the default PL_DHASH_MIN_SIZE; or if
* MinSize=PL_DHASH_MIN_SIZE <= 256, in [0.5, (float)(MinSize-1)/MinSize]; or
* else in [0.5, 255.0/256]. minAlpha must be in [0, maxAlpha / 2), so that
* we don't shrink on the very next remove after growing a table upon adding
* an entry that brings entryCount past maxAlpha * tableSize.
*/
NS_COM_GLUE void
PL_DHashTableSetAlphaBounds(PLDHashTable *table,
float maxAlpha,
float minAlpha);
/*
* Call this macro with k, the number of pointer-sized words wasted per entry
* under chaining, to compute the minimum alpha at which double hashing still
* beats chaining.
*/
#define PL_DHASH_MIN_ALPHA(table, k) \
((float)((table)->entrySize / sizeof(void *) - 1) \
/ ((table)->entrySize / sizeof(void *) + (k)))
/*
* Default max/min alpha, and macros to compute the value for the |capacity|
* parameter to PL_NewDHashTable and PL_DHashTableInit, given default or any
* max alpha, such that adding entryCount entries right after initializing the
* table will not require a reallocation (so PL_DHASH_ADD can't fail for those
* PL_DHashTableOperate calls).
*
* NB: PL_DHASH_CAP is a helper macro meant for use only in PL_DHASH_CAPACITY.
* Don't use it directly!
*/
#define PL_DHASH_DEFAULT_MAX_ALPHA 0.75
#define PL_DHASH_DEFAULT_MIN_ALPHA 0.25
#define PL_DHASH_CAP(entryCount, maxAlpha) \
((uint32_t)((double)(entryCount) / (maxAlpha)))
#define PL_DHASH_CAPACITY(entryCount, maxAlpha) \
(PL_DHASH_CAP(entryCount, maxAlpha) + \
(((PL_DHASH_CAP(entryCount, maxAlpha) * (uint8_t)(0x100 * (maxAlpha))) \
>> 8) < (entryCount)))
#define PL_DHASH_DEFAULT_CAPACITY(entryCount) \
PL_DHASH_CAPACITY(entryCount, PL_DHASH_DEFAULT_MAX_ALPHA)
/*
* Finalize table's data, free its entry storage using table->ops->freeTable,
* and leave its members unchanged from their last live values (which leaves