Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fc5eb3f

Browse files
committed
Tweak accumArrayResult() to double the size of its working arrays when
more space is needed, instead of incrementing by a fixed amount; the old method wastes lots of space and time when the ultimate size is large. Per gripe from Tatsuo.
1 parent a5cf12e commit fc5eb3f

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

src/backend/utils/adt/arrayfuncs.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.134 2006/10/06 17:13:59 petere Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.135 2006/11/08 19:24:38 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -4337,10 +4337,9 @@ accumArrayResult(ArrayBuildState *astate,
43374337
oldcontext = MemoryContextSwitchTo(arr_context);
43384338
astate = (ArrayBuildState *) palloc(sizeof(ArrayBuildState));
43394339
astate->mcontext = arr_context;
4340-
astate->dvalues = (Datum *)
4341-
palloc(ARRAY_ELEMS_CHUNKSIZE * sizeof(Datum));
4342-
astate->dnulls = (bool *)
4343-
palloc(ARRAY_ELEMS_CHUNKSIZE * sizeof(bool));
4340+
astate->alen = 64; /* arbitrary starting array size */
4341+
astate->dvalues = (Datum *) palloc(astate->alen * sizeof(Datum));
4342+
astate->dnulls = (bool *) palloc(astate->alen * sizeof(bool));
43444343
astate->nelems = 0;
43454344
astate->element_type = element_type;
43464345
get_typlenbyvalalign(element_type,
@@ -4353,14 +4352,13 @@ accumArrayResult(ArrayBuildState *astate,
43534352
oldcontext = MemoryContextSwitchTo(astate->mcontext);
43544353
Assert(astate->element_type == element_type);
43554354
/* enlarge dvalues[]/dnulls[] if needed */
4356-
if ((astate->nelems % ARRAY_ELEMS_CHUNKSIZE) == 0)
4355+
if (astate->nelems >= astate->alen)
43574356
{
4357+
astate->alen *= 2;
43584358
astate->dvalues = (Datum *)
4359-
repalloc(astate->dvalues,
4360-
(astate->nelems + ARRAY_ELEMS_CHUNKSIZE) * sizeof(Datum));
4359+
repalloc(astate->dvalues, astate->alen * sizeof(Datum));
43614360
astate->dnulls = (bool *)
4362-
repalloc(astate->dnulls,
4363-
(astate->nelems + ARRAY_ELEMS_CHUNKSIZE) * sizeof(bool));
4361+
repalloc(astate->dnulls, astate->alen * sizeof(bool));
43644362
}
43654363
}
43664364

src/include/utils/array.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
5050
* Portions Copyright (c) 1994, Regents of the University of California
5151
*
52-
* $PostgreSQL: pgsql/src/include/utils/array.h,v 1.59 2006/09/10 20:14:20 tgl Exp $
52+
* $PostgreSQL: pgsql/src/include/utils/array.h,v 1.60 2006/11/08 19:24:38 tgl Exp $
5353
*
5454
*-------------------------------------------------------------------------
5555
*/
@@ -78,13 +78,8 @@ typedef struct ArrayBuildState
7878
MemoryContext mcontext; /* where all the temp stuff is kept */
7979
Datum *dvalues; /* array of accumulated Datums */
8080
bool *dnulls; /* array of is-null flags for Datums */
81-
82-
/*
83-
* The allocated size of dvalues[] and dnulls[] is always a multiple of
84-
* ARRAY_ELEMS_CHUNKSIZE
85-
*/
86-
#define ARRAY_ELEMS_CHUNKSIZE 64
87-
int nelems; /* number of valid Datums in dvalues[] */
81+
int alen; /* allocated length of above arrays */
82+
int nelems; /* number of valid entries in above arrays */
8883
Oid element_type; /* data type of the Datums */
8984
int16 typlen; /* needed info about datatype */
9085
bool typbyval;

0 commit comments

Comments
 (0)