From: Arran Cudbard-Bell Date: Tue, 17 Aug 2021 17:50:17 +0000 (-0500) Subject: Pre-allocate LST stack memory X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=478a84ce5c281d70545d826d9bc9b7f663842817;p=thirdparty%2Ffreeradius-server.git Pre-allocate LST stack memory This is mostly to ensure good locality --- diff --git a/src/lib/util/lst.c b/src/lib/util/lst.c index c44d7fd5c01..4c39734e5fd 100644 --- a/src/lib/util/lst.c +++ b/src/lib/util/lst.c @@ -144,7 +144,7 @@ bool is_bucket(fr_lst_t *lst, stack_index_t idx) * 2. one can fetch and modify arbitrary stack items; when array elements must be * moved to keep them contiguous, the pivot stack entries must change to match. */ -static pivot_stack_t *stack_alloc(TALLOC_CTX *ctx) +static inline CC_HINT(always_inline, nonnull) pivot_stack_t *stack_alloc(TALLOC_CTX *ctx) { pivot_stack_t *s; @@ -230,8 +230,18 @@ fr_lst_t *_fr_lst_alloc(TALLOC_CTX *ctx, fr_lst_cmp_t cmp, char const *type, siz { fr_lst_t *lst; - lst = talloc_zero(ctx, fr_lst_t); - if (!lst) return NULL; + /* + * Pre-allocate stack memory as it is + * unlikely to need to grow in practice. + * + * We don't pre-allocate the array of elements + * If we pre-allocated the array of elements + * we'd end up wasting that memory as soon as + * we needed to expand the array. + */ + lst = talloc_zero_pooled_object(ctx, fr_lst_t, 2, + sizeof(pivot_stack_t) + (INITIAL_STACK_CAPACITY * sizeof(fr_lst_index_t))); + if (unlikely(!lst)) return NULL; lst->capacity = INITIAL_CAPACITY; lst->p = talloc_array(lst, void *, lst->capacity);