From d628112d6ac7cc5c6956fa8975c36b9c1e6060d9 Mon Sep 17 00:00:00 2001 From: David Rowley Date: Wed, 23 Mar 2022 02:24:00 +1300 Subject: [PATCH v4 2/2] Use Generation memory contexts to store tuples in sorts The general useage pattern when we store tuples in tuplesort.c is that we store a series of tuples one by one then either perform a sort or spill them to disk. In the common case there is no pfreeing of already stored tuples. For the common case since we do not individually pfree tuples we have very little need for aset.c memory allocation behavior which maintains freelists and always rounds allocation sizes up to the next power of 2 size. Here we unconditionally switch to using the Generation context type, which does not round allocation sizes up to the next power of 2. This results in more dense memory allocations and on average uses around 25% less memory. This can result in some fairly good performance improvements for sorting. Performance improvements are fairly significant in cases where we no longer spill to disk due to the more compact memory allocation. However, performnace improvements are also quite promising in other cases as well. Author: David Rowley Discussion: https://postgr.es/m/CAApHDvoH4ASzsAOyHcxkuY01Qf++8JJ0paw+03dk+W25tQEcNQ@mail.gmail.com --- src/backend/utils/sort/tuplesort.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c index 086e948fca..df4f5ad6c5 100644 --- a/src/backend/utils/sort/tuplesort.c +++ b/src/backend/utils/sort/tuplesort.c @@ -845,9 +845,9 @@ tuplesort_begin_batch(Tuplesortstate *state) * in the parent context, not this context, because there is no need to * free memtuples early. */ - state->tuplecontext = AllocSetContextCreate(state->sortcontext, - "Caller tuples", - ALLOCSET_DEFAULT_SIZES); + state->tuplecontext = GenerationContextCreate(state->sortcontext, + "Caller tuples", + ALLOCSET_DEFAULT_SIZES); state->status = TSS_INITIAL; state->bounded = false; -- 2.32.0