Skip to content

Commit

Permalink
detect-engine-iponly: remove insertion sort
Browse files Browse the repository at this point in the history
The runtime complexity of insertion sort is approx. O(h*n)^2 where
h is the size of the HOME_NET and n is the number of ip only rules
that use the HOME_NET.

Removing this sort significantly improves rule load time when a large
HOME_NET is used in combination with a moderate amount of ip only
rules.

Brief but cursory testing shows no difference in the structure of the
radix trees that are created from this temporary linked list.
  • Loading branch information
cccs-sadugas committed Jan 17, 2024
1 parent 6896a93 commit b7423a3
Showing 1 changed file with 4 additions and 29 deletions.
33 changes: 4 additions & 29 deletions src/detect-engine-iponly.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,9 @@ static int IPOnlyCIDRItemSetup(IPOnlyCIDRItem **gh, char *s)
return -1;
}


/**
* \brief This function insert a IPOnlyCIDRItem
* to a list of IPOnlyCIDRItems sorted by netmask
* ascending
* to a list of IPOnlyCIDRItems
* \param head Pointer to the head of IPOnlyCIDRItems list
* \param item Pointer to the item to insert in the list
*
Expand All @@ -366,32 +364,9 @@ static IPOnlyCIDRItem *IPOnlyCIDRItemInsertReal(IPOnlyCIDRItem *head,
if (item == NULL)
return head;

/* Compare with the head */
if (item->netmask < head->netmask || (item->netmask == head->netmask && IPOnlyCIDRItemCompare(head, item))) {
item->next = head;
return item;
}

if (item->netmask == head->netmask && !IPOnlyCIDRItemCompare(head, item)) {
item->next = head->next;
head->next = item;
return head;
}

for (prev = it = head;
it != NULL && it->netmask < item->netmask;
it = it->next)
prev = it;

if (it == NULL) {
prev->next = item;
item->next = NULL;
} else {
item->next = it;
prev->next = item;
}

return head;
/* Always insert item as head */
item->next = head;
return item;
}

/**
Expand Down

0 comments on commit b7423a3

Please sign in to comment.