2747eff128
Traditionally the hcreate() function creates a hash table that uses chaining, using a fixed user-provided size. The problem with this approach is that this often either wastes memory (table too big) or yields bad performance (table too small). For applications it may not always be easy to estimate the right hash table size. A fixed number only increases performance compared to a linked list by a constant factor. This problem can be solved easily by dynamically resizing the hash table. If the size of the hash table is at least doubled, this has no negative on the running time complexity. If a dynamically sized hash table is used, we can also switch to using open addressing instead of chaining, which has the advantage of just using a single allocation for the entire table, instead of allocating many small objects. Finally, a problem with the existing implementation is that its deterministic algorithm for hashing makes it possible to come up with fixed patterns to trigger an excessive number of collisions. We can easily solve this by using FNV-1a as a hashing algorithm in combination with a randomly generated offset basis. Measurements have shown that this implementation is about 20-25% faster than the existing implementation (even if the existing implementation is given an excessive number of buckets). Though it allocates more memory through malloc() than the old implementation (between 4-8 pointers per used entry instead of 3), process memory use is similar to the old implementation as if the estimated size was underestimated by a factor 10. This is due to the fact that malloc() needs to perform less bookkeeping. Reviewed by: jilles, pfg Obtained from: https://github.com/NuxiNL/cloudlibc Differential Revision: https://reviews.freebsd.org/D4644
81 lines
1.6 KiB
C
81 lines
1.6 KiB
C
/*-
|
|
* Written by J.T. Conklin <jtc@NetBSD.org>
|
|
* Public domain.
|
|
*
|
|
* $NetBSD: search.h,v 1.16 2005/02/03 04:39:32 perry Exp $
|
|
* $FreeBSD$
|
|
*/
|
|
|
|
#ifndef _SEARCH_H_
|
|
#define _SEARCH_H_
|
|
|
|
#include <sys/cdefs.h>
|
|
#include <sys/_types.h>
|
|
|
|
#ifndef _SIZE_T_DECLARED
|
|
typedef __size_t size_t;
|
|
#define _SIZE_T_DECLARED
|
|
#endif
|
|
|
|
typedef struct entry {
|
|
char *key;
|
|
void *data;
|
|
} ENTRY;
|
|
|
|
typedef enum {
|
|
FIND, ENTER
|
|
} ACTION;
|
|
|
|
typedef enum {
|
|
preorder,
|
|
postorder,
|
|
endorder,
|
|
leaf
|
|
} VISIT;
|
|
|
|
#ifdef _SEARCH_PRIVATE
|
|
typedef struct node {
|
|
void *key;
|
|
struct node *llink, *rlink;
|
|
signed char balance;
|
|
} node_t;
|
|
|
|
struct que_elem {
|
|
struct que_elem *next;
|
|
struct que_elem *prev;
|
|
};
|
|
#endif
|
|
|
|
#if __BSD_VISIBLE
|
|
struct hsearch_data {
|
|
struct __hsearch *__hsearch;
|
|
};
|
|
#endif
|
|
|
|
__BEGIN_DECLS
|
|
int hcreate(size_t);
|
|
void hdestroy(void);
|
|
ENTRY *hsearch(ENTRY, ACTION);
|
|
void insque(void *, void *);
|
|
void *lfind(const void *, const void *, size_t *, size_t,
|
|
int (*)(const void *, const void *));
|
|
void *lsearch(const void *, void *, size_t *, size_t,
|
|
int (*)(const void *, const void *));
|
|
void remque(void *);
|
|
void *tdelete(const void * __restrict, void ** __restrict,
|
|
int (*)(const void *, const void *));
|
|
void *tfind(const void *, void * const *,
|
|
int (*)(const void *, const void *));
|
|
void *tsearch(const void *, void **, int (*)(const void *, const void *));
|
|
void twalk(const void *, void (*)(const void *, VISIT, int));
|
|
|
|
#if __BSD_VISIBLE
|
|
int hcreate_r(size_t, struct hsearch_data *);
|
|
void hdestroy_r(struct hsearch_data *);
|
|
int hsearch_r(ENTRY, ACTION, ENTRY **, struct hsearch_data *);
|
|
#endif
|
|
|
|
__END_DECLS
|
|
|
|
#endif /* !_SEARCH_H_ */
|