Skip to content

Commit

Permalink
TEST simplify merge
Browse files Browse the repository at this point in the history
  • Loading branch information
victorjulien committed Jan 27, 2025
1 parent 81ede37 commit 043a1df
Showing 1 changed file with 7 additions and 102 deletions.
109 changes: 7 additions & 102 deletions src/detect.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,113 +267,17 @@ const SigGroupHead *SigMatchSignaturesGetSgh(const DetectEngineCtx *de_ctx,
static inline void DetectPrefilterMergeSort(DetectEngineCtx *de_ctx,
DetectEngineThreadCtx *det_ctx)
{
SigIntId mpm, nonmpm;
// SigIntId mpm, nonmpm;
SigIntId *mpm_ptr = det_ctx->pmq.rule_id_array;
SigIntId *nonmpm_ptr = NULL; // det_ctx->non_pf_id_array;
uint32_t m_cnt = det_ctx->pmq.rule_id_array_cnt;
uint32_t n_cnt = 0; // det_ctx->non_pf_id_cnt;
SigIntId *final_ptr;
uint32_t final_cnt;
SigIntId id;
SigIntId previous_id = (SigIntId)-1;
SigIntId *final_ptr = mpm_ptr;
uint32_t final_cnt = m_cnt;
Signature **sig_array = de_ctx->sig_array;
Signature **match_array = det_ctx->match_array;
Signature *s;

SCLogDebug("PMQ rule id array count %d", det_ctx->pmq.rule_id_array_cnt);

/* Load first values. */
if (likely(m_cnt)) {
mpm = *mpm_ptr;
} else {
/* mpm list is empty */
final_ptr = nonmpm_ptr;
final_cnt = n_cnt;
goto final;
}
if (likely(n_cnt)) {
nonmpm = *nonmpm_ptr;
} else {
/* non-mpm list is empty. */
final_ptr = mpm_ptr;
final_cnt = m_cnt;
goto final;
}
while (1) {
if (mpm < nonmpm) {
/* Take from mpm list */
id = mpm;

s = sig_array[id];
/* As the mpm list can contain duplicates, check for that here. */
if (likely(id != previous_id)) {
*match_array++ = s;
previous_id = id;
}
if (unlikely(--m_cnt == 0)) {
/* mpm list is now empty */
final_ptr = nonmpm_ptr;
final_cnt = n_cnt;
goto final;
}
mpm_ptr++;
mpm = *mpm_ptr;
} else if (mpm > nonmpm) {
id = nonmpm;

s = sig_array[id];
/* As the mpm list can contain duplicates, check for that here. */
if (likely(id != previous_id)) {
*match_array++ = s;
previous_id = id;
}
if (unlikely(--n_cnt == 0)) {
final_ptr = mpm_ptr;
final_cnt = m_cnt;
goto final;
}
nonmpm_ptr++;
nonmpm = *nonmpm_ptr;

} else { /* implied mpm == nonmpm */
/* special case: if on both lists, it's a negated mpm pattern */

/* mpm list may have dups, so skip past them here */
while (--m_cnt != 0) {
mpm_ptr++;
mpm = *mpm_ptr;
if (mpm != nonmpm)
break;
}
/* if mpm is done, update nonmpm_ptrs and jump to final */
if (unlikely(m_cnt == 0)) {
n_cnt--;

/* mpm list is now empty */
final_ptr = ++nonmpm_ptr;
final_cnt = n_cnt;
goto final;
}
/* otherwise, if nonmpm is done jump to final for mpm
* mpm ptrs already updated */
if (unlikely(--n_cnt == 0)) {
final_ptr = mpm_ptr;
final_cnt = m_cnt;
goto final;
}

/* not at end of the lists, update nonmpm. Mpm already
* updated in while loop above. */
nonmpm_ptr++;
nonmpm = *nonmpm_ptr;
}
}

final: /* Only one list remaining. Just walk that list. */

SigIntId previous_id = (SigIntId)-1;
while (final_cnt-- > 0) {
id = *final_ptr++;
s = sig_array[id];
SigIntId id = *final_ptr++;
Signature *s = sig_array[id];

/* As the mpm list can contain duplicates, check for that here. */
if (likely(id != previous_id)) {
Expand Down Expand Up @@ -650,6 +554,7 @@ static inline void DetectRunPrefilterPkt(
}
#endif
// TODO can we bypass this to just copy? PMQ already sorted?
// TODO acts like just list building atm: moves from pmq to match_array, dedups
PACKET_PROFILING_DETECT_START(p, PROF_DETECT_PF_SORT2);
DetectPrefilterMergeSort(de_ctx, det_ctx);
PACKET_PROFILING_DETECT_END(p, PROF_DETECT_PF_SORT2);
Expand Down

0 comments on commit 043a1df

Please sign in to comment.