Custom Quick Sort for Signature IDs

Use an in place Quick Sort instead of qsort(), which does merge sort and
calls memcpy().

Improves performance on my tests.
pull/1295/head
Ken Steele 11 years ago committed by Victor Julien
parent 736ac6a459
commit 86f4c6c47b

@ -842,11 +842,29 @@ static inline void DetectPrefilterMergeSort(DetectEngineCtx *de_ctx,
BUG_ON((det_ctx->pmq.rule_id_array_cnt + sgh->non_mpm_id_cnt) < det_ctx->match_array_cnt); BUG_ON((det_ctx->pmq.rule_id_array_cnt + sgh->non_mpm_id_cnt) < det_ctx->match_array_cnt);
} }
static int DoSortSigIntId(const void *a, const void *b) /* Return true is the list is sorted smallest to largest */
static void QuickSortSigIntId(SigIntId *sids, uint32_t n)
{ {
SigIntId x = *(SigIntId *)a; if (n < 2)
SigIntId y = *(SigIntId *)b; return;
return x - y; SigIntId p = sids[n / 2];
SigIntId *l = sids;
SigIntId *r = sids + n - 1;
while (l <= r) {
if (*l < p)
l++;
else if (*r > p)
r--;
else {
SigIntId t = *l;
*l = *r;
*r = t;
l++;
r--;
}
}
QuickSortSigIntId(sids, r - sids + 1);
QuickSortSigIntId(l, sids + n - l);
} }
#define SMS_USE_FLOW_SGH 0x01 #define SMS_USE_FLOW_SGH 0x01
@ -1098,9 +1116,8 @@ static inline void DetectMpmPrefilter(DetectEngineCtx *de_ctx,
/* Sort the rule list to lets look at pmq. /* Sort the rule list to lets look at pmq.
* NOTE due to merging of 'stream' pmqs we *MAY* have duplicate entries */ * NOTE due to merging of 'stream' pmqs we *MAY* have duplicate entries */
if (det_ctx->pmq.rule_id_array_cnt) { if (det_ctx->pmq.rule_id_array_cnt > 1) {
qsort(det_ctx->pmq.rule_id_array, det_ctx->pmq.rule_id_array_cnt, QuickSortSigIntId(det_ctx->pmq.rule_id_array, det_ctx->pmq.rule_id_array_cnt);
sizeof(SigIntId), DoSortSigIntId);
} }
} }

Loading…
Cancel
Save