Coverage Report

Created: 2025-12-20 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nspr/lib/ds/plhash.c
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
/*
7
 * PL hash table package.
8
 */
9
#include "plhash.h"
10
#include "prbit.h"
11
#include "prlog.h"
12
#include "prmem.h"
13
#include "prtypes.h"
14
#include <stdlib.h>
15
#include <string.h>
16
17
/* Compute the number of buckets in ht */
18
779k
#define NBUCKETS(ht) (1 << (PL_HASH_BITS - (ht)->shift))
19
20
/* The smallest table has 16 buckets */
21
386k
#define MINBUCKETSLOG2 4
22
386k
#define MINBUCKETS (1 << MINBUCKETSLOG2)
23
24
/* Compute the maximum entries given n buckets that we will tolerate, ~90% */
25
393k
#define OVERLOADED(n) ((n) - ((n) >> 3))
26
27
/* Compute the number of entries below which we shrink the table by half */
28
385k
#define UNDERLOADED(n) (((n) > MINBUCKETS) ? ((n) >> 2) : 0)
29
30
/*
31
** Stubs for default hash allocator ops.
32
*/
33
468
static void* PR_CALLBACK DefaultAllocTable(void* pool, PRSize size) {
34
468
  return PR_MALLOC(size);
35
468
}
36
37
428
static void PR_CALLBACK DefaultFreeTable(void* pool, void* item) {
38
428
  PR_Free(item);
39
428
}
40
41
209k
static PLHashEntry* PR_CALLBACK DefaultAllocEntry(void* pool, const void* key) {
42
209k
  return PR_NEW(PLHashEntry);
43
209k
}
44
45
static void PR_CALLBACK DefaultFreeEntry(void* pool, PLHashEntry* he,
46
209k
                                         PRUintn flag) {
47
209k
  if (flag == HT_FREE_ENTRY) {
48
208k
    PR_Free(he);
49
208k
  }
50
209k
}
51
52
static PLHashAllocOps defaultHashAllocOps = {
53
    DefaultAllocTable, DefaultFreeTable, DefaultAllocEntry, DefaultFreeEntry};
54
55
PR_IMPLEMENT(PLHashTable*)
56
PL_NewHashTable(PRUint32 n, PLHashFunction keyHash, PLHashComparator keyCompare,
57
                PLHashComparator valueCompare, const PLHashAllocOps* allocOps,
58
274
                void* allocPriv) {
59
274
  PLHashTable* ht;
60
274
  PRSize nb;
61
62
274
  if (n <= MINBUCKETS) {
63
162
    n = MINBUCKETSLOG2;
64
162
  } else {
65
112
    n = PR_CeilingLog2(n);
66
112
    if ((PRInt32)n < 0) {
67
0
      return 0;
68
0
    }
69
112
  }
70
71
274
  if (!allocOps) {
72
146
    allocOps = &defaultHashAllocOps;
73
146
  }
74
75
274
  ht = (PLHashTable*)((*allocOps->allocTable)(allocPriv, sizeof *ht));
76
274
  if (!ht) {
77
0
    return 0;
78
0
  }
79
274
  memset(ht, 0, sizeof *ht);
80
274
  ht->shift = PL_HASH_BITS - n;
81
274
  n = 1 << n;
82
274
  nb = n * sizeof(PLHashEntry*);
83
274
  ht->buckets = (PLHashEntry**)((*allocOps->allocTable)(allocPriv, nb));
84
274
  if (!ht->buckets) {
85
0
    (*allocOps->freeTable)(allocPriv, ht);
86
0
    return 0;
87
0
  }
88
274
  memset(ht->buckets, 0, nb);
89
90
274
  ht->keyHash = keyHash;
91
274
  ht->keyCompare = keyCompare;
92
274
  ht->valueCompare = valueCompare;
93
274
  ht->allocOps = allocOps;
94
274
  ht->allocPriv = allocPriv;
95
274
  return ht;
96
274
}
97
98
PR_IMPLEMENT(void)
99
238
PL_HashTableDestroy(PLHashTable* ht) {
100
238
  PRUint32 i, n;
101
238
  PLHashEntry *he, *next;
102
238
  const PLHashAllocOps* allocOps = ht->allocOps;
103
238
  void* allocPriv = ht->allocPriv;
104
105
238
  n = NBUCKETS(ht);
106
14.7k
  for (i = 0; i < n; i++) {
107
20.6k
    for (he = ht->buckets[i]; he; he = next) {
108
6.06k
      next = he->next;
109
6.06k
      (*allocOps->freeEntry)(allocPriv, he, HT_FREE_ENTRY);
110
6.06k
    }
111
14.5k
  }
112
238
#ifdef DEBUG
113
238
  memset(ht->buckets, 0xDB, n * sizeof ht->buckets[0]);
114
238
#endif
115
238
  (*allocOps->freeTable)(allocPriv, ht->buckets);
116
238
#ifdef DEBUG
117
238
  memset(ht, 0xDB, sizeof *ht);
118
238
#endif
119
238
  (*allocOps->freeTable)(allocPriv, ht);
120
238
}
121
122
/*
123
** Multiplicative hash, from Knuth 6.4.
124
*/
125
44.3M
#define GOLDEN_RATIO 0x9E3779B9U /* 2/(1+sqrt(5))*(2^32) */
126
127
PR_IMPLEMENT(PLHashEntry**)
128
2.20M
PL_HashTableRawLookup(PLHashTable* ht, PLHashNumber keyHash, const void* key) {
129
2.20M
  PLHashEntry *he, **hep, **hep0;
130
2.20M
  PLHashNumber h;
131
132
#ifdef HASHMETER
133
  ht->nlookups++;
134
#endif
135
2.20M
  h = keyHash * GOLDEN_RATIO;
136
2.20M
  h >>= ht->shift;
137
2.20M
  hep = hep0 = &ht->buckets[h];
138
2.42M
  while ((he = *hep) != 0) {
139
1.34M
    if (he->keyHash == keyHash && (*ht->keyCompare)(key, he->key)) {
140
      /* Move to front of chain if not already there */
141
1.12M
      if (hep != hep0) {
142
59.9k
        *hep = he->next;
143
59.9k
        he->next = *hep0;
144
59.9k
        *hep0 = he;
145
59.9k
      }
146
1.12M
      return hep0;
147
1.12M
    }
148
219k
    hep = &he->next;
149
#ifdef HASHMETER
150
    ht->nsteps++;
151
#endif
152
219k
  }
153
1.07M
  return hep;
154
2.20M
}
155
156
/*
157
** Same as PL_HashTableRawLookup but doesn't reorder the hash entries.
158
*/
159
PR_IMPLEMENT(PLHashEntry**)
160
PL_HashTableRawLookupConst(PLHashTable* ht, PLHashNumber keyHash,
161
42.1M
                           const void* key) {
162
42.1M
  PLHashEntry *he, **hep;
163
42.1M
  PLHashNumber h;
164
165
#ifdef HASHMETER
166
  ht->nlookups++;
167
#endif
168
42.1M
  h = keyHash * GOLDEN_RATIO;
169
42.1M
  h >>= ht->shift;
170
42.1M
  hep = &ht->buckets[h];
171
44.2M
  while ((he = *hep) != 0) {
172
43.4M
    if (he->keyHash == keyHash && (*ht->keyCompare)(key, he->key)) {
173
41.3M
      break;
174
41.3M
    }
175
2.13M
    hep = &he->next;
176
#ifdef HASHMETER
177
    ht->nsteps++;
178
#endif
179
2.13M
  }
180
42.1M
  return hep;
181
42.1M
}
182
183
PR_IMPLEMENT(PLHashEntry*)
184
PL_HashTableRawAdd(PLHashTable* ht, PLHashEntry** hep, PLHashNumber keyHash,
185
393k
                   const void* key, void* value) {
186
393k
  PRUint32 i, n;
187
393k
  PLHashEntry *he, *next, **oldbuckets;
188
393k
  PRSize nb;
189
190
  /* Grow the table if it is overloaded */
191
393k
  n = NBUCKETS(ht);
192
393k
  if (ht->nentries >= OVERLOADED(n)) {
193
142
    oldbuckets = ht->buckets;
194
142
    nb = 2 * n * sizeof(PLHashEntry*);
195
142
    ht->buckets =
196
142
        (PLHashEntry**)((*ht->allocOps->allocTable)(ht->allocPriv, nb));
197
142
    if (!ht->buckets) {
198
0
      ht->buckets = oldbuckets;
199
0
      return 0;
200
0
    }
201
142
    memset(ht->buckets, 0, nb);
202
#ifdef HASHMETER
203
    ht->ngrows++;
204
#endif
205
142
    ht->shift--;
206
207
10.5k
    for (i = 0; i < n; i++) {
208
19.5k
      for (he = oldbuckets[i]; he; he = next) {
209
9.12k
        next = he->next;
210
9.12k
        hep = PL_HashTableRawLookup(ht, he->keyHash, he->key);
211
9.12k
        PR_ASSERT(*hep == 0);
212
9.12k
        he->next = 0;
213
9.12k
        *hep = he;
214
9.12k
      }
215
10.4k
    }
216
142
#ifdef DEBUG
217
142
    memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]);
218
142
#endif
219
142
    (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
220
142
    hep = PL_HashTableRawLookup(ht, keyHash, key);
221
142
  }
222
223
  /* Make a new key value entry */
224
393k
  he = (*ht->allocOps->allocEntry)(ht->allocPriv, key);
225
393k
  if (!he) {
226
0
    return 0;
227
0
  }
228
393k
  he->keyHash = keyHash;
229
393k
  he->key = key;
230
393k
  he->value = value;
231
393k
  he->next = *hep;
232
393k
  *hep = he;
233
393k
  ht->nentries++;
234
393k
  return he;
235
393k
}
236
237
PR_IMPLEMENT(PLHashEntry*)
238
394k
PL_HashTableAdd(PLHashTable* ht, const void* key, void* value) {
239
394k
  PLHashNumber keyHash;
240
394k
  PLHashEntry *he, **hep;
241
242
394k
  keyHash = (*ht->keyHash)(key);
243
394k
  hep = PL_HashTableRawLookup(ht, keyHash, key);
244
394k
  if ((he = *hep) != 0) {
245
    /* Hit; see if values match */
246
1.27k
    if ((*ht->valueCompare)(he->value, value)) {
247
      /* key,value pair is already present in table */
248
0
      return he;
249
0
    }
250
1.27k
    if (he->value) {
251
1.27k
      (*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_VALUE);
252
1.27k
    }
253
1.27k
    he->value = value;
254
1.27k
    return he;
255
1.27k
  }
256
393k
  return PL_HashTableRawAdd(ht, hep, keyHash, key, value);
257
394k
}
258
259
PR_IMPLEMENT(void)
260
385k
PL_HashTableRawRemove(PLHashTable* ht, PLHashEntry** hep, PLHashEntry* he) {
261
385k
  PRUint32 i, n;
262
385k
  PLHashEntry *next, **oldbuckets;
263
385k
  PRSize nb;
264
265
385k
  *hep = he->next;
266
385k
  (*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_ENTRY);
267
268
  /* Shrink table if it's underloaded */
269
385k
  n = NBUCKETS(ht);
270
385k
  if (--ht->nentries < UNDERLOADED(n)) {
271
34
    oldbuckets = ht->buckets;
272
34
    nb = n * sizeof(PLHashEntry*) / 2;
273
34
    ht->buckets =
274
34
        (PLHashEntry**)((*ht->allocOps->allocTable)(ht->allocPriv, nb));
275
34
    if (!ht->buckets) {
276
0
      ht->buckets = oldbuckets;
277
0
      return;
278
0
    }
279
34
    memset(ht->buckets, 0, nb);
280
#ifdef HASHMETER
281
    ht->nshrinks++;
282
#endif
283
34
    ht->shift++;
284
285
1.57k
    for (i = 0; i < n; i++) {
286
1.59k
      for (he = oldbuckets[i]; he; he = next) {
287
56
        next = he->next;
288
56
        hep = PL_HashTableRawLookup(ht, he->keyHash, he->key);
289
56
        PR_ASSERT(*hep == 0);
290
56
        he->next = 0;
291
56
        *hep = he;
292
56
      }
293
1.53k
    }
294
34
#ifdef DEBUG
295
34
    memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]);
296
34
#endif
297
34
    (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
298
34
  }
299
385k
}
300
301
PR_IMPLEMENT(PRBool)
302
385k
PL_HashTableRemove(PLHashTable* ht, const void* key) {
303
385k
  PLHashNumber keyHash;
304
385k
  PLHashEntry *he, **hep;
305
306
385k
  keyHash = (*ht->keyHash)(key);
307
385k
  hep = PL_HashTableRawLookup(ht, keyHash, key);
308
385k
  if ((he = *hep) == 0) {
309
0
    return PR_FALSE;
310
0
  }
311
312
  /* Hit; remove element */
313
385k
  PL_HashTableRawRemove(ht, hep, he);
314
385k
  return PR_TRUE;
315
385k
}
316
317
PR_IMPLEMENT(void*)
318
1.41M
PL_HashTableLookup(PLHashTable* ht, const void* key) {
319
1.41M
  PLHashNumber keyHash;
320
1.41M
  PLHashEntry *he, **hep;
321
322
1.41M
  keyHash = (*ht->keyHash)(key);
323
1.41M
  hep = PL_HashTableRawLookup(ht, keyHash, key);
324
1.41M
  if ((he = *hep) != 0) {
325
742k
    return he->value;
326
742k
  }
327
669k
  return 0;
328
1.41M
}
329
330
/*
331
** Same as PL_HashTableLookup but doesn't reorder the hash entries.
332
*/
333
PR_IMPLEMENT(void*)
334
42.1M
PL_HashTableLookupConst(PLHashTable* ht, const void* key) {
335
42.1M
  PLHashNumber keyHash;
336
42.1M
  PLHashEntry *he, **hep;
337
338
42.1M
  keyHash = (*ht->keyHash)(key);
339
42.1M
  hep = PL_HashTableRawLookupConst(ht, keyHash, key);
340
42.1M
  if ((he = *hep) != 0) {
341
41.3M
    return he->value;
342
41.3M
  }
343
772k
  return 0;
344
42.1M
}
345
346
/*
347
** Iterate over the entries in the hash table calling func for each
348
** entry found. Stop if "f" says to (return value & PR_ENUMERATE_STOP).
349
** Return a count of the number of elements scanned.
350
*/
351
PR_IMPLEMENT(int)
352
61
PL_HashTableEnumerateEntries(PLHashTable* ht, PLHashEnumerator f, void* arg) {
353
61
  PLHashEntry *he, **hep;
354
61
  PRUint32 i, nbuckets;
355
61
  int rv, n = 0;
356
61
  PLHashEntry* todo = 0;
357
358
61
  nbuckets = NBUCKETS(ht);
359
2.46k
  for (i = 0; i < nbuckets; i++) {
360
2.40k
    hep = &ht->buckets[i];
361
2.41k
    while ((he = *hep) != 0) {
362
12
      rv = (*f)(he, n, arg);
363
12
      n++;
364
12
      if (rv & (HT_ENUMERATE_REMOVE | HT_ENUMERATE_UNHASH)) {
365
0
        *hep = he->next;
366
0
        if (rv & HT_ENUMERATE_REMOVE) {
367
0
          he->next = todo;
368
0
          todo = he;
369
0
        }
370
12
      } else {
371
12
        hep = &he->next;
372
12
      }
373
12
      if (rv & HT_ENUMERATE_STOP) {
374
0
        goto out;
375
0
      }
376
12
    }
377
2.40k
  }
378
379
61
out:
380
61
  hep = &todo;
381
61
  while ((he = *hep) != 0) {
382
0
    PL_HashTableRawRemove(ht, hep, he);
383
0
  }
384
61
  return n;
385
61
}
386
387
#ifdef HASHMETER
388
#  include <math.h>
389
#  include <stdio.h>
390
391
PR_IMPLEMENT(void)
392
PL_HashTableDumpMeter(PLHashTable* ht, PLHashEnumerator dump, FILE* fp) {
393
  double mean, variance;
394
  PRUint32 nchains, nbuckets;
395
  PRUint32 i, n, maxChain, maxChainLen;
396
  PLHashEntry* he;
397
398
  variance = 0;
399
  nchains = 0;
400
  maxChainLen = 0;
401
  nbuckets = NBUCKETS(ht);
402
  for (i = 0; i < nbuckets; i++) {
403
    he = ht->buckets[i];
404
    if (!he) {
405
      continue;
406
    }
407
    nchains++;
408
    for (n = 0; he; he = he->next) {
409
      n++;
410
    }
411
    variance += n * n;
412
    if (n > maxChainLen) {
413
      maxChainLen = n;
414
      maxChain = i;
415
    }
416
  }
417
  mean = (double)ht->nentries / nchains;
418
  variance = fabs(variance / nchains - mean * mean);
419
420
  fprintf(fp, "\nHash table statistics:\n");
421
  fprintf(fp, "     number of lookups: %u\n", ht->nlookups);
422
  fprintf(fp, "     number of entries: %u\n", ht->nentries);
423
  fprintf(fp, "       number of grows: %u\n", ht->ngrows);
424
  fprintf(fp, "     number of shrinks: %u\n", ht->nshrinks);
425
  fprintf(fp, "   mean steps per hash: %g\n",
426
          (double)ht->nsteps / ht->nlookups);
427
  fprintf(fp, "mean hash chain length: %g\n", mean);
428
  fprintf(fp, "    standard deviation: %g\n", sqrt(variance));
429
  fprintf(fp, " max hash chain length: %u\n", maxChainLen);
430
  fprintf(fp, "        max hash chain: [%u]\n", maxChain);
431
432
  for (he = ht->buckets[maxChain], i = 0; he; he = he->next, i++)
433
    if ((*dump)(he, i, fp) != HT_ENUMERATE_NEXT) {
434
      break;
435
    }
436
}
437
#endif /* HASHMETER */
438
439
PR_IMPLEMENT(int)
440
0
PL_HashTableDump(PLHashTable* ht, PLHashEnumerator dump, FILE* fp) {
441
0
  int count;
442
443
0
  count = PL_HashTableEnumerateEntries(ht, dump, fp);
444
#ifdef HASHMETER
445
  PL_HashTableDumpMeter(ht, dump, fp);
446
#endif
447
0
  return count;
448
0
}
449
450
PR_IMPLEMENT(PLHashNumber)
451
0
PL_HashString(const void* key) {
452
0
  PLHashNumber h;
453
0
  const PRUint8* s;
454
455
0
  h = 0;
456
0
  for (s = (const PRUint8*)key; *s; s++) {
457
0
    h = PR_ROTATE_LEFT32(h, 4) ^ *s;
458
0
  }
459
0
  return h;
460
0
}
461
462
PR_IMPLEMENT(int)
463
0
PL_CompareStrings(const void* v1, const void* v2) {
464
0
  return strcmp((const char*)v1, (const char*)v2) == 0;
465
0
}
466
467
PR_IMPLEMENT(int)
468
39.4M
PL_CompareValues(const void* v1, const void* v2) { return v1 == v2; }