Skip to content

Commit

Permalink
Make "add_child" O(1) instead of O(N)
Browse files Browse the repository at this point in the history
  • Loading branch information
alandekok committed Mar 23, 2014
1 parent 723be4b commit 9ef9cc8
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/main/modcall.c
Expand Up @@ -72,6 +72,7 @@ typedef struct {
GROUPTYPE_COUNT
} grouptype; /* after mc */
modcallable *children;
modcallable *tail; /* of the children list */
CONF_SECTION *cs;
value_pair_map_t *map; /* update */
fr_cond_t *cond; /* if/elsif */
Expand Down Expand Up @@ -146,24 +147,20 @@ static modcallable *mod_xlattocallable(modxlat *p)
}

/* modgroups are grown by adding a modcallable to the end */
/* FIXME: This is O(N^2) */
static void add_child(modgroup *g, modcallable *c)
{
modcallable **head = &g->children;
modcallable *node = *head;
modcallable **last = head;

if (!c) return;

(void) talloc_steal(g, c);

while (node) {
last = &node->next;
node = node->next;
if (!g->children) {
g->children = g->tail = c;
} else {
rad_assert(g->tail->next == NULL);
g->tail->next = c;
g->tail = c;
}

rad_assert(c->next == NULL);
*last = c;
c->parent = mod_grouptocallable(g);
}

Expand Down

0 comments on commit 9ef9cc8

Please sign in to comment.