From 224a1eebfbfd5edc4ce29121fb0a1a9260ec45db Mon Sep 17 00:00:00 2001 From: Chen Puqing Date: Tue, 18 Feb 2014 23:18:29 +0800 Subject: [PATCH 1/2] optimize the search of empty slot in the hashid_node array --- service-src/hashid.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service-src/hashid.h b/service-src/hashid.h index dc410eb2..1d567122 100644 --- a/service-src/hashid.h +++ b/service-src/hashid.h @@ -90,7 +90,7 @@ hashid_insert(struct hashid * hi, int id) { struct hashid_node *c = NULL; int i; for (i=0;icap;i++) { - int index = (i+hi->count) % hi->cap; + int index = (i+id) % hi->cap; if (hi->id[index].id == -1) { c = &hi->id[index]; break; From 59e60294322ee52734b2f0d1fe14995041acdee3 Mon Sep 17 00:00:00 2001 From: Chen Puqing Date: Wed, 19 Feb 2014 00:07:34 +0800 Subject: [PATCH 2/2] a little improvement of hashid code: move the calculation of hashcap into the hashid_init function and remove the assertions --- service-src/hashid.h | 11 +++++++---- service-src/service_gate.c | 6 +----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/service-src/hashid.h b/service-src/hashid.h index 1d567122..501addcd 100644 --- a/service-src/hashid.h +++ b/service-src/hashid.h @@ -19,12 +19,15 @@ struct hashid { }; static void -hashid_init(struct hashid *hi, int max, int hashcap) { +hashid_init(struct hashid *hi, int max) { int i; - assert((hashcap & (hashcap-1))==0); - hi->cap = max; - assert(hi->cap <= hashcap); + int hashcap; + hashcap = 16; + while (hashcap < max) { + hashcap *= 2; + } hi->hashmod = hashcap - 1; + hi->cap = max; hi->count = 0; hi->id = malloc(max * sizeof(struct hashid_node)); for (i=0;ictx = ctx; - int cap = 16; - while (cap < max) { - cap *= 2; - } - hashid_init(&g->hash, max, cap); + hashid_init(&g->hash, max); g->conn = malloc(max * sizeof(struct connection)); memset(g->conn, 0, max *sizeof(struct connection)); g->max_connection = max;