Skip to content

Commit

Permalink
Hashmap size should be a power of 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ddoroshev committed Jul 21, 2024
1 parent 84ac9a5 commit ae0ce83
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
37 changes: 16 additions & 21 deletions benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

struct rusage r;

char *keys;

void benchmark_init(hashmap **map) {
clock_t start, end;
double cpu_time_used;
Expand All @@ -31,18 +33,14 @@ void benchmark_insert(hashmap *map) {
start = clock();

for (int i = 0; i < SIZE; i++) {
char *key = malloc(20 * sizeof(char));
sprintf(key, "key%d", i);
hashmap_set(map, key, i);
hashmap_set(map, &keys[i * 20], i);
if (i % REPORT_FREQUENCY == 0) {
printf("Inserted %d items\n", i);
}
free(key);
}

end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;

printf("Insertion benchmark completed. Time taken: %.5f seconds\n", cpu_time_used);
}

Expand All @@ -51,11 +49,8 @@ void benchmark_search(hashmap *map) {
double cpu_time_used;

start = clock();

for (int i = 0; i < SIZE; i++) {
char key[20];
sprintf(key, "key%d", i);
hashmap_item *item = hashmap_get(map, key);
hashmap_item *item = hashmap_get(map, &keys[i * 20]);
if (item->value != i) {
printf("Error! %d != %d\n", item->value, i);
}
Expand All @@ -66,7 +61,6 @@ void benchmark_search(hashmap *map) {

end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;

printf("Search benchmark completed. Time taken: %.5f seconds\n", cpu_time_used);
}

Expand All @@ -76,12 +70,10 @@ void benchmark_delete(hashmap *map) {

start = clock();

for (int i = 0; i < SIZE; i++) {
char key[20];
sprintf(key, "key%d", i);
int res = hashmap_delete(map, key);
for (int i = 0; i < SIZE; i++) {;
int res = hashmap_delete(map, &keys[20 * i]);
if (res != 0) {
printf("Error deleting %s\n", key);
printf("Error deleting %s\n", &keys[20 * i]);
}
if (i % REPORT_FREQUENCY == 0) {
printf("Deleted %d items\n", i);
Expand All @@ -90,7 +82,6 @@ void benchmark_delete(hashmap *map) {

end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;

printf("Deletion benchmark completed. Time taken: %.5f seconds\n", cpu_time_used);
}

Expand All @@ -115,9 +106,7 @@ void benchmark_mixed_workload(hashmap *map) {
start = clock();

for (int i = 0; i < SIZE; i++) {
char *key = malloc(20 * sizeof(char));
sprintf(key, "key%d", i);

char *key = &keys[i * 20];
// Insertion
hashmap_set(map, key, i);

Expand All @@ -140,7 +129,6 @@ void benchmark_mixed_workload(hashmap *map) {
if (i % REPORT_FREQUENCY == 0) {
printf("Processed %d items\n", i);
}
free(key);
}

end = clock();
Expand All @@ -156,6 +144,12 @@ long get_peak_memory() {

int main() {
long mem_before, mem_after;

keys = malloc(20 * sizeof(char) * SIZE);
for (int i = 0; i < SIZE; i++) {
sprintf(&keys[i * 20], "key%d", i);
}

mem_before = get_peak_memory();

hashmap *map;
Expand All @@ -167,5 +161,6 @@ int main() {
benchmark_free(map);
mem_after = get_peak_memory();
printf("Memory usage before: %ld, after: %ld, growth: %ld\n", mem_before, mem_after, mem_after - mem_before);
free(keys);
return 0;
}
}
6 changes: 5 additions & 1 deletion hashmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ int hashmap_resize(hashmap *hm)
hashmap_item *item;
array *old_values = hm->values;
uint32_t old_len = hm->values->length;
int new_length = ESTIMATE_SIZE(old_len);
int est_size = ESTIMATE_SIZE(old_len);
int new_length = HASHMAP_BASE_SIZE;
while (new_length <= est_size && new_length > 0) {
new_length <<= 1;
}
hm->count = 0;
hm->values = array_init(new_length, sizeof(hashmap_item));
if (hm->values == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion test_hashmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void test_hashmap_set(void)
}
ASSERT(hashmap_set(hm, "foo", val) == 0);
ASSERT(hashmap_len(hm) == HASHMAP_BASE_SIZE + 1);
ASSERT(hashmap_values(hm)->length == 18);
ASSERT(hashmap_values(hm)->length == 16);

hashmap_free(hm);
}
Expand Down

0 comments on commit ae0ce83

Please sign in to comment.