v3-0002-Use-Generation-memory-contexts-to-store-tuples-in.patch

application/x-patch

Filename: v3-0002-Use-Generation-memory-contexts-to-store-tuples-in.patch
Type: application/x-patch
Part: 1
Message: Re: Use generation context to speed up tuplesorts

Patch

Format: format-patch
Series: patch v3-0002
Subject: Use Generation memory contexts to store tuples in sorts
File+
src/backend/utils/sort/tuplesort.c 3 3
From 14d03211aee2e58e726169e5022479c86a347db3 Mon Sep 17 00:00:00 2001
From: David Rowley <dgrowley@gmail.com>
Date: Fri, 18 Feb 2022 10:09:14 +1300
Subject: [PATCH v3 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