Merge pull request #66 from puqing/master

optimize the search of empty slot in the hashid_node array
This commit is contained in:
云风
2014-02-21 14:11:23 +08:00
2 changed files with 9 additions and 10 deletions

View File

@@ -19,12 +19,15 @@ struct hashid {
}; };
static void static void
hashid_init(struct hashid *hi, int max, int hashcap) { hashid_init(struct hashid *hi, int max) {
int i; int i;
assert((hashcap & (hashcap-1))==0); int hashcap;
hi->cap = max; hashcap = 16;
assert(hi->cap <= hashcap); while (hashcap < max) {
hashcap *= 2;
}
hi->hashmod = hashcap - 1; hi->hashmod = hashcap - 1;
hi->cap = max;
hi->count = 0; hi->count = 0;
hi->id = malloc(max * sizeof(struct hashid_node)); hi->id = malloc(max * sizeof(struct hashid_node));
for (i=0;i<max;i++) { for (i=0;i<max;i++) {
@@ -90,7 +93,7 @@ hashid_insert(struct hashid * hi, int id) {
struct hashid_node *c = NULL; struct hashid_node *c = NULL;
int i; int i;
for (i=0;i<hi->cap;i++) { for (i=0;i<hi->cap;i++) {
int index = (i+hi->count) % hi->cap; int index = (i+id) % hi->cap;
if (hi->id[index].id == -1) { if (hi->id[index].id == -1) {
c = &hi->id[index]; c = &hi->id[index];
break; break;

View File

@@ -368,11 +368,7 @@ gate_init(struct gate *g , struct skynet_context * ctx, char * parm) {
g->ctx = ctx; g->ctx = ctx;
int cap = 16; hashid_init(&g->hash, max);
while (cap < max) {
cap *= 2;
}
hashid_init(&g->hash, max, cap);
g->conn = malloc(max * sizeof(struct connection)); g->conn = malloc(max * sizeof(struct connection));
memset(g->conn, 0, max *sizeof(struct connection)); memset(g->conn, 0, max *sizeof(struct connection));
g->max_connection = max; g->max_connection = max;