From fb668f615c40b84da4e431c41c4138c9524741c2 Mon Sep 17 00:00:00 2001 From: Otavio Pontes Date: Wed, 27 Jun 2018 23:17:30 +0000 Subject: [PATCH] download: Better use of the hashmap area The hashmap used for fullfile downloads is created with size 256, but only 16 positions are used. file->hash is a string composed of characters from 'a' to 'z' and '0' to '1'. So 2 characters are needed to get a 256 different combinations. Use the first 2 bytes in the string to index the hashmap. --- src/download.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/download.c b/src/download.c index 2ed0195e..e0f5dc27 100644 --- a/src/download.c +++ b/src/download.c @@ -58,9 +58,21 @@ static struct list *failed = NULL; * traversing a manifest, doing hash comparisons, and (re)staging any files * whose hash miscompares. * - * file->hash[0] acts as index into the arrays + * file->hash[0:1] acts as index into the arrays */ #define SWUPD_CURL_HASH_BUCKETS 256 + +/* + * Converts a character to its hexadecimal value (0 to 15 assuming that the + * input was from the set 0123456789abcdef). + */ +#define HASH_VALUE(h) (h >= 'a' ? h - 'a' + 10 : h - '0') + +/* + * converts two characters to a value in the range 0 to 255. + */ +#define HASH_TO_KEY(hash) (HASH_VALUE(hash[0]) << 4 | HASH_VALUE(hash[1])) + static struct list *swupd_curl_hashmap[SWUPD_CURL_HASH_BUCKETS]; /* try to insert the file into the hashmap download queue @@ -74,7 +86,7 @@ static int swupd_curl_hashmap_insert(struct file *file) char *tar_dotfile; char *targetfile; struct stat stat; - int hashmap_index = file->hash[0]; + int hashmap_index = HASH_TO_KEY(file->hash); struct list **bucket; bucket = &swupd_curl_hashmap[hashmap_index];