Commit a847d48c authored by rswindell's avatar rswindell
Browse files

MsgBase.get_all_msg_headers() performance improvement:

read the entire index file in a single read operation
parent 167b1d82
...@@ -1618,6 +1618,7 @@ js_get_all_msg_headers(JSContext *cx, uintN argc, jsval *arglist) ...@@ -1618,6 +1618,7 @@ js_get_all_msg_headers(JSContext *cx, uintN argc, jsval *arglist)
char numstr[16]; char numstr[16];
JSBool include_votes=JS_FALSE; JSBool include_votes=JS_FALSE;
post_t* post; post_t* post;
idxrec_t* idx;
JS_SET_RVAL(cx, arglist, JSVAL_NULL); JS_SET_RVAL(cx, arglist, JSVAL_NULL);
...@@ -1629,11 +1630,15 @@ js_get_all_msg_headers(JSContext *cx, uintN argc, jsval *arglist) ...@@ -1629,11 +1630,15 @@ js_get_all_msg_headers(JSContext *cx, uintN argc, jsval *arglist)
if(!SMB_IS_OPEN(&(priv->smb))) if(!SMB_IS_OPEN(&(priv->smb)))
return JS_TRUE; return JS_TRUE;
if((post = malloc(priv->smb.status.total_msgs * sizeof(post_t))) == NULL) { if((post = calloc(priv->smb.status.total_msgs, sizeof(*post))) == NULL) {
JS_ReportError(cx, "malloc error", WHERE); JS_ReportError(cx, "malloc error", WHERE);
return JS_FALSE; return JS_FALSE;
} }
memset(post, 0, priv->smb.status.total_msgs * sizeof(post_t)); if((idx = calloc(priv->smb.status.total_msgs, sizeof(*idx))) == NULL) {
JS_ReportError(cx, "malloc error", WHERE);
free(post);
return JS_FALSE;
}
if(argc && JSVAL_IS_BOOLEAN(argv[0])) if(argc && JSVAL_IS_BOOLEAN(argv[0]))
include_votes = JSVAL_TO_BOOLEAN(argv[0]); include_votes = JSVAL_TO_BOOLEAN(argv[0]);
...@@ -1645,6 +1650,7 @@ js_get_all_msg_headers(JSContext *cx, uintN argc, jsval *arglist) ...@@ -1645,6 +1650,7 @@ js_get_all_msg_headers(JSContext *cx, uintN argc, jsval *arglist)
if((priv->smb_result=smb_locksmbhdr(&(priv->smb)))!=SMB_SUCCESS) { if((priv->smb_result=smb_locksmbhdr(&(priv->smb)))!=SMB_SUCCESS) {
JS_RESUMEREQUEST(cx, rc); JS_RESUMEREQUEST(cx, rc);
free(post); free(post);
free(idx);
return JS_TRUE; return JS_TRUE;
} }
JS_RESUMEREQUEST(cx, rc); JS_RESUMEREQUEST(cx, rc);
...@@ -1659,29 +1665,23 @@ js_get_all_msg_headers(JSContext *cx, uintN argc, jsval *arglist) ...@@ -1659,29 +1665,23 @@ js_get_all_msg_headers(JSContext *cx, uintN argc, jsval *arglist)
else else
proto=NULL; proto=NULL;
if(fread(idx, sizeof(*idx), priv->smb.status.total_msgs, priv->smb.sid_fp) != priv->smb.status.total_msgs) {
smb_unlocksmbhdr(&(priv->smb));
JS_ReportError(cx,"index read (%lu) failed", priv->smb.status.total_msgs);
free(post);
free(idx);
return JS_FALSE;
}
for(off=0; off < priv->smb.status.total_msgs; off++) { for(off=0; off < priv->smb.status.total_msgs; off++) {
smbmsg_t msg; post[off].idx = idx[off];
if(idx[off].attr&MSG_VOTE && !(idx[off].attr&MSG_POLL)) {
ZERO_VAR(msg);
msg.offset = off;
rc=JS_SUSPENDREQUEST(cx);
priv->smb_result = smb_getmsgidx(&(priv->smb), &msg);
JS_RESUMEREQUEST(cx, rc);
if(priv->smb_result != SMB_SUCCESS) {
smb_unlocksmbhdr(&(priv->smb));
free(post);
return JS_TRUE;
}
post[off].idx = msg.idx;
if(msg.idx.attr&MSG_VOTE && !(msg.idx.attr&MSG_POLL)) {
ulong u; ulong u;
for(u = 0; u < off; u++) for(u = 0; u < off; u++)
if(post[u].idx.number == msg.idx.remsg) if(post[u].idx.number == idx[off].remsg)
break; break;
if(u < off) { if(u < off) {
post[u].total_votes++; post[u].total_votes++;
switch(msg.idx.attr&MSG_VOTE) { switch(idx[off].attr&MSG_VOTE) {
case MSG_UPVOTE: case MSG_UPVOTE:
post[u].upvotes++; post[u].upvotes++;
break; break;
...@@ -1690,13 +1690,14 @@ js_get_all_msg_headers(JSContext *cx, uintN argc, jsval *arglist) ...@@ -1690,13 +1690,14 @@ js_get_all_msg_headers(JSContext *cx, uintN argc, jsval *arglist)
break; break;
default: default:
for(int b=0; b < MSG_POLL_MAX_ANSWERS; b++) { for(int b=0; b < MSG_POLL_MAX_ANSWERS; b++) {
if(msg.idx.votes&(1<<b)) if(idx[off].votes&(1<<b))
post[u].votes[b]++; post[u].votes[b]++;
} }
} }
} }
} }
} }
free(idx);
for(off=0; off < priv->smb.status.total_msgs; off++) { for(off=0; off < priv->smb.status.total_msgs; off++) {
if((!include_votes) && (post[off].idx.attr&MSG_VOTE)) if((!include_votes) && (post[off].idx.attr&MSG_VOTE))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment