dlhist.c: calculate initial size when read table from file.
Now that records are fixed size, it's easy to calculate the number of entries in the file. use that to calculate how large the hash table should be.
This commit is contained in:
parent
9517d28f72
commit
bbefd9daf5
1 changed files with 18 additions and 13 deletions
31
dlhist.c
31
dlhist.c
|
|
@ -50,6 +50,10 @@ union hash {
|
||||||
unsigned char sha1[20];
|
unsigned char sha1[20];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* NOTE: be sure to change this constant if the struct's size changes.
|
||||||
|
*/
|
||||||
|
#define HE_SZ (sizeof(union hash) + sizeof(unsigned))
|
||||||
struct hash_entry {
|
struct hash_entry {
|
||||||
union hash hash;
|
union hash hash;
|
||||||
unsigned int time;
|
unsigned int time;
|
||||||
|
|
@ -126,6 +130,18 @@ static void he_remove(struct hash_entry *he) {
|
||||||
table_count--;
|
table_count--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static unsigned calculate_size(unsigned count) {
|
||||||
|
|
||||||
|
/*
|
||||||
|
* set size to a load factor that is in the
|
||||||
|
* middle in the valid range.
|
||||||
|
*/
|
||||||
|
unsigned size = count / 0.625;
|
||||||
|
if (size < TABLE_MIN_SIZE)
|
||||||
|
size = TABLE_MIN_SIZE;
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
static void resize_table() {
|
static void resize_table() {
|
||||||
|
|
||||||
double load;
|
double load;
|
||||||
|
|
@ -139,13 +155,7 @@ static void resize_table() {
|
||||||
(load >= 0.5 && load <= 0.75))
|
(load >= 0.5 && load <= 0.75))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/*
|
table_size = calculate_size(table_count);
|
||||||
* set size to a load factor that is in the
|
|
||||||
* middle in the valid range.
|
|
||||||
*/
|
|
||||||
table_size = table_count / 0.625;
|
|
||||||
if (table_size < TABLE_MIN_SIZE)
|
|
||||||
table_size = TABLE_MIN_SIZE;
|
|
||||||
|
|
||||||
table_count = 0;
|
table_count = 0;
|
||||||
table = calloc(sizeof(*table), table_size);
|
table = calloc(sizeof(*table), table_size);
|
||||||
|
|
@ -162,6 +172,7 @@ static void build_table(const char *buf, size_t len) {
|
||||||
|
|
||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
|
|
||||||
|
table_size = calculate_size(len / HE_SZ);
|
||||||
table = calloc(sizeof(*table), table_size);
|
table = calloc(sizeof(*table), table_size);
|
||||||
|
|
||||||
while(offset < len) {
|
while(offset < len) {
|
||||||
|
|
@ -217,15 +228,9 @@ int dlhist_open() {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get current table size */
|
|
||||||
table_size = htonl(hdr->size);
|
|
||||||
|
|
||||||
offset = sizeof(*hdr);
|
offset = sizeof(*hdr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (table_size < 1)
|
|
||||||
table_size = TABLE_MIN_SIZE;
|
|
||||||
|
|
||||||
build_table(buf + offset, st.st_size - offset);
|
build_table(buf + offset, st.st_size - offset);
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
|
||||||
Reference in a new issue