diff options
author | in2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2003-10-05 09:03:12 +0800 |
---|---|---|
committer | in2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2003-10-05 09:03:12 +0800 |
commit | a94fe7b873e50aec4d08cab1867e741998957605 (patch) | |
tree | 1bd658fe1e0fdc62b35ec36467696158c8eb4b2e | |
parent | 2a2fcfccb232aec55d2e388ca93ffaf52810daaa (diff) | |
download | pttbbs-a94fe7b873e50aec4d08cab1867e741998957605.tar pttbbs-a94fe7b873e50aec4d08cab1867e741998957605.tar.gz pttbbs-a94fe7b873e50aec4d08cab1867e741998957605.tar.bz2 pttbbs-a94fe7b873e50aec4d08cab1867e741998957605.tar.lz pttbbs-a94fe7b873e50aec4d08cab1867e741998957605.tar.xz pttbbs-a94fe7b873e50aec4d08cab1867e741998957605.tar.zst pttbbs-a94fe7b873e50aec4d08cab1867e741998957605.zip |
use bsearch in login_friend_online()
save cpu but increase SHM_t (sizeof(short) * MAX_ACTIVE) bytes
WARNING: SHM_t has been changed
git-svn-id: http://opensvn.csie.org/pttbbs/trunk@1212 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r-- | pttbbs/include/proto.h | 2 | ||||
-rw-r--r-- | pttbbs/include/pttstruct.h | 2 | ||||
-rw-r--r-- | pttbbs/mbbsd/friend.c | 2 | ||||
-rw-r--r-- | pttbbs/mbbsd/stuff.c | 25 | ||||
-rw-r--r-- | pttbbs/mbbsd/talk.c | 27 |
5 files changed, 38 insertions, 20 deletions
diff --git a/pttbbs/include/proto.h b/pttbbs/include/proto.h index a072c5d9..41fa2a7f 100644 --- a/pttbbs/include/proto.h +++ b/pttbbs/include/proto.h @@ -445,6 +445,8 @@ int not_alpha(char ch); int valid_ident(char *ident); int userid_is_BM(char *userid, char *list); int is_uBM(char *list, char *id); +inline int *intbsearch(int key, int *base0, int nmemb); +int qsort_intcompar(const void *a, const void *b); #ifndef CRITICAL_MEMORY #define MALLOC(p) malloc(p) #define FREE(p) free(p) diff --git a/pttbbs/include/pttstruct.h b/pttbbs/include/pttstruct.h index f14317e7..5ec26a57 100644 --- a/pttbbs/include/pttstruct.h +++ b/pttbbs/include/pttstruct.h @@ -238,6 +238,8 @@ typedef struct userinfo_t { int from_alias; char birth; /* 是否是生日 Ptt*/ char tty[11]; /* tty port */ + short nFriends; /* 下面 friend[] 只用到前幾個, + 用來 bsearch */ int friend[MAX_FRIEND]; int friend_online[MAX_FRIEND];/* point到線上好友 utmpshm的位置 */ /* 好友比較的cache 前兩個bit是狀態 */ diff --git a/pttbbs/mbbsd/friend.c b/pttbbs/mbbsd/friend.c index 8205686d..ab9f1076 100644 --- a/pttbbs/mbbsd/friend.c +++ b/pttbbs/mbbsd/friend.c @@ -271,7 +271,9 @@ friend_load(int type) myfriends[friendcount++] = unum; fclose(fp); } + qsort(myfriends, friendcount, sizeof(int), qsort_intcompar); memcpy(currutmp->friend, myfriends, sizeof(myfriends)); + currutmp->nFriends = friendcount; } if (!type || type & FRIEND_REJECT) { diff --git a/pttbbs/mbbsd/stuff.c b/pttbbs/mbbsd/stuff.c index d8201330..a6aaec46 100644 --- a/pttbbs/mbbsd/stuff.c +++ b/pttbbs/mbbsd/stuff.c @@ -663,3 +663,28 @@ StringHash(unsigned char *s) } return (v * 2654435769UL) >> (32 - HASH_BITS); } + +inline int *intbsearch(int key, int *base0, int nmemb) +{ + /* 改自 /usr/src/lib/libc/stdlib/bsearch.c , + 專給搜 int array 用的, 不透過 compar function 故較快些 */ + const char *base = (char *)base0; + size_t lim; + int *p; + + for (lim = nmemb; lim != 0; lim >>= 1) { + p = (int *)(base + (lim >> 1) * 4); + if( key == *p ) + return p; + if( key > *p ){/* key > p: move right */ + base = (char *)p + 4; + lim--; + } /* else move left */ + } + return (NULL); +} + +int qsort_intcompar(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} diff --git a/pttbbs/mbbsd/talk.c b/pttbbs/mbbsd/talk.c index a576fbf4..9633570d 100644 --- a/pttbbs/mbbsd/talk.c +++ b/pttbbs/mbbsd/talk.c @@ -155,33 +155,20 @@ modestring(userinfo_t * uentp, int simple) int set_friend_bit(userinfo_t * me, userinfo_t * ui) { - int unum, *myfriends, hit = 0, n; + int unum, *myfriends, hit = 0; /* 判斷對方是否為我的朋友 ? */ - unum = ui->uid; - myfriends = me->friend; - while ((n = *myfriends++)) { - if (unum == n) { - hit = IFH; - break; - } - } + if( intbsearch(ui->uid, me->friend, me->nFriends) ) + hit = IFH; /* 判斷我是否為對方的朋友 ? */ - myfriends = ui->friend; - while ((unum = *myfriends++)) { - if (unum == me->uid) { - hit |= HFM; - break; - } - } + if( intbsearch(me->uid, ui->friend, ui->nFriends) ) + hit |= HFM; /* 判斷對方是否為我的仇人 ? */ - - unum = ui->uid; myfriends = me->reject; - while ((n = *myfriends++)) { - if (unum == n) { + while ((unum = *myfriends++)) { + if (unum == ui->uid) { hit |= IRH; break; } |