Fix misleading comment in tuplesort.c
authorDavid Rowley <drowley@postgresql.org>
Tue, 9 Dec 2025 23:01:14 +0000 (12:01 +1300)
committerDavid Rowley <drowley@postgresql.org>
Tue, 9 Dec 2025 23:01:14 +0000 (12:01 +1300)
A comment in tuplesort.c was claiming that the code was defining
INITIAL_MEMTUPSIZE so that it *does not* exceed
ALLOCSET_SEPARATE_THRESHOLD, but the code actually ensures that we
purposefully *do* exceed ALLOCSET_SEPARATE_THRESHOLD for the initial
allocation of the tuples array, as per reasons detailed in the
commentary of grow_memtuples().

Also, there's not much need to repeat the mention about
ALLOCSET_SEPARATE_THRESHOLD in each location where INITIAL_MEMTUPSIZE is
used, so remove those comments.

Author: ChangAo Chen <cca5507@qq.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: David G. Johnston <david.g.johnston@gmail.com>
Discussion: https://postgr.es/m/tencent_6FA14F85D6B5B5291532D6789E07F4765C08%40qq.com

src/backend/utils/sort/tuplesort.c

index c1fa7a97509866d8dd706a2d2fc67331a6088bcc..88ae529e84313f5863c2feb812955cd7828cbfdb 100644 (file)
 #include "utils/tuplesort.h"
 
 /*
- * Initial size of memtuples array.  We're trying to select this size so that
- * array doesn't exceed ALLOCSET_SEPARATE_THRESHOLD and so that the overhead of
- * allocation might possibly be lowered.  However, we don't consider array sizes
- * less than 1024.
- *
+ * Initial size of memtuples array.  This must be more than
+ * ALLOCSET_SEPARATE_THRESHOLD; see comments in grow_memtuples().  Clamp at
+ * 1024 elements to avoid excessive reallocs.
  */
 #define INITIAL_MEMTUPSIZE Max(1024, \
    ALLOCSET_SEPARATE_THRESHOLD / sizeof(SortTuple) + 1)
@@ -692,10 +690,6 @@ tuplesort_begin_common(int workMem, SortCoordinate coordinate, int sortopt)
    state->base.sortcontext = sortcontext;
    state->base.maincontext = maincontext;
 
-   /*
-    * Initial size of array must be more than ALLOCSET_SEPARATE_THRESHOLD;
-    * see comments in grow_memtuples().
-    */
    state->memtupsize = INITIAL_MEMTUPSIZE;
    state->memtuples = NULL;
 
@@ -784,10 +778,6 @@ tuplesort_begin_batch(Tuplesortstate *state)
 
    state->memtupcount = 0;
 
-   /*
-    * Initial size of array must be more than ALLOCSET_SEPARATE_THRESHOLD;
-    * see comments in grow_memtuples().
-    */
    state->growmemtuples = true;
    state->slabAllocatorUsed = false;
    if (state->memtuples != NULL && state->memtupsize != INITIAL_MEMTUPSIZE)