From: James Jones Date: Fri, 27 Aug 2021 15:44:41 +0000 (-0500) Subject: Adjust initial LST pivot stack size in accordance with initial size (#4213) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8958ab5c956aba8aa18c4e297def90f5c5b81bc6;p=thirdparty%2Ffreeradius-server.git Adjust initial LST pivot stack size in accordance with initial size (#4213) It's now initially max(1, log2(initial array size)). --- diff --git a/src/lib/util/lst.c b/src/lib/util/lst.c index e36190fe2ab..f5d930b34f1 100644 --- a/src/lib/util/lst.c +++ b/src/lib/util/lst.c @@ -46,7 +46,6 @@ RCSID("$Id$") * the pivot stack may be a rare event. */ #define INITIAL_CAPACITY 2048 -#define INITIAL_STACK_CAPACITY 32 #define is_power_of_2(_n) ((_n) && (((_n) & ((_n) - 1)) == 0)) @@ -224,6 +223,7 @@ fr_lst_t *_fr_lst_alloc(TALLOC_CTX *ctx, fr_lst_cmp_t cmp, char const *type, siz { fr_lst_t *lst; pivot_stack_t *s; + unsigned int initial_stack_capacity; if (!init) { init = INITIAL_CAPACITY; @@ -231,6 +231,8 @@ fr_lst_t *_fr_lst_alloc(TALLOC_CTX *ctx, fr_lst_cmp_t cmp, char const *type, siz init = 1 << fr_high_bit_pos(init); } + for (initial_stack_capacity = 1; (1U << initial_stack_capacity) < init; initial_stack_capacity++) ; + /* * Pre-allocate stack memory as it is * unlikely to need to grow in practice. @@ -243,7 +245,7 @@ fr_lst_t *_fr_lst_alloc(TALLOC_CTX *ctx, fr_lst_cmp_t cmp, char const *type, siz * Pre-allocating three chunks appears to be * the optimum. */ - lst = talloc_zero_pooled_object(ctx, fr_lst_t, 3, (INITIAL_STACK_CAPACITY * sizeof(fr_lst_index_t))); + lst = talloc_zero_pooled_object(ctx, fr_lst_t, 3, (initial_stack_capacity * sizeof(fr_lst_index_t))); if (unlikely(!lst)) return NULL; lst->capacity = init; @@ -258,10 +260,10 @@ fr_lst_t *_fr_lst_alloc(TALLOC_CTX *ctx, fr_lst_cmp_t cmp, char const *type, siz * Allocate the initial stack */ s = &lst->s; - s->data = talloc_array(lst, fr_lst_index_t, INITIAL_STACK_CAPACITY); + s->data = talloc_array(lst, fr_lst_index_t, initial_stack_capacity); if (unlikely(!s->data)) goto cleanup; s->depth = 0; - s->size = INITIAL_STACK_CAPACITY; + s->size = initial_stack_capacity; /* Initially the LST is empty and we start at the beginning of the array */ stack_push(lst, &lst->s, 0); diff --git a/src/lib/util/lst_tests.c b/src/lib/util/lst_tests.c index 15686aadd48..68830728e60 100644 --- a/src/lib/util/lst_tests.c +++ b/src/lib/util/lst_tests.c @@ -260,7 +260,8 @@ static void lst_burn_in(void) array = calloc(BURN_IN_OPS, sizeof(lst_thing)); for (unsigned int i = 0; i < BURN_IN_OPS; i++) array[i].data = rand() % 65537; - lst = fr_lst_alloc(NULL, lst_cmp, lst_thing, idx, 0); + /* Make init small to exercise growing the pivot stack. */ + lst = fr_lst_alloc(NULL, lst_cmp, lst_thing, idx, 32); for (unsigned int i = 0; i < BURN_IN_OPS; i++) { lst_thing *ret_thing = NULL;