From 1be6148eca803ec326fd60d608bce41a327160b5 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Mon, 27 Jul 2026 21:51:15 -0700 Subject: [PATCH] Switched to palloc_array and palloc0_array where possible --- src/halfvec.c | 8 ++++++-- src/hnsw.h | 1 + src/hnswutils.c | 4 ++-- src/ivfbuild.c | 6 +++--- src/ivfflat.h | 2 ++ src/ivfkmeans.c | 6 +++--- src/ivfscan.c | 4 ++-- src/sparsevec.c | 6 +++++- src/vector.c | 8 ++++++-- 9 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/halfvec.c b/src/halfvec.c index 3771146..b1400dd 100644 --- a/src/halfvec.c +++ b/src/halfvec.c @@ -27,8 +27,12 @@ #include "parser/scansup.h" #endif +#if PG_VERSION_NUM < 160000 +#define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count))) +#endif + #define STATE_DIMS(x) (ARR_DIMS(x)[0] - 1) -#define CreateStateDatums(dim) palloc(sizeof(Datum) * (dim + 1)) +#define CreateStateDatums(dim) palloc_array(Datum, (dim + 1)) /* * Get a half from a message buffer @@ -513,7 +517,7 @@ halfvec_to_float4(PG_FUNCTION_ARGS) Datum *datums; ArrayType *result; - datums = (Datum *) palloc(sizeof(Datum) * vec->dim); + datums = palloc_array(Datum, vec->dim); for (int i = 0; i < vec->dim; i++) datums[i] = Float4GetDatum(HalfToFloat4(vec->x[i])); diff --git a/src/hnsw.h b/src/hnsw.h index f265c7c..5f01b8c 100644 --- a/src/hnsw.h +++ b/src/hnsw.h @@ -108,6 +108,7 @@ typedef Pointer Item; #if PG_VERSION_NUM < 160000 #define palloc_object(type) ((type *) palloc(sizeof(type))) #define palloc0_object(type) ((type *) palloc0(sizeof(type))) +#define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count))) #endif #define HnswIsElementTuple(tup) ((tup)->type == HNSW_ELEMENT_TUPLE_TYPE) diff --git a/src/hnswutils.c b/src/hnswutils.c index ac36d2a..4faf7db 100644 --- a/src/hnswutils.c +++ b/src/hnswutils.c @@ -831,7 +831,7 @@ HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation in HnswNeighborArray *localNeighborhood = NULL; Size neighborhoodSize = 0; int lm = HnswGetLayerM(m, lc); - HnswUnvisited *unvisited = palloc(lm * sizeof(HnswUnvisited)); + HnswUnvisited *unvisited = palloc_array(HnswUnvisited, lm); int unvisitedLength; bool inMemory = index == NULL; @@ -1074,7 +1074,7 @@ SelectNeighbors(char *base, List *c, int lm, HnswSupport * support, bool *closer if (list_length(w) <= lm) return w; - wd = palloc(sizeof(HnswCandidate *) * list_length(w)); + wd = palloc_array(HnswCandidate *, list_length(w)); /* Ensure order of candidates is deterministic for closer caching */ if (sortCandidates) diff --git a/src/ivfbuild.c b/src/ivfbuild.c index dd540b2..8049623 100644 --- a/src/ivfbuild.c +++ b/src/ivfbuild.c @@ -396,7 +396,7 @@ InitBuildState(IvfflatBuildState * buildstate, Relation heap, Relation index, In buildstate->centers = VectorArrayInit(buildstate->lists, buildstate->dimensions, buildstate->itemsize); /* TODO Move allocation to page creation */ - buildstate->listInfo = palloc(sizeof(ListInfo) * buildstate->lists); + buildstate->listInfo = palloc_array(ListInfo, buildstate->lists); buildstate->tmpCtx = AllocSetContextCreate(CurrentMemoryContext, "Ivfflat build temporary context", @@ -404,8 +404,8 @@ InitBuildState(IvfflatBuildState * buildstate, Relation heap, Relation index, In #ifdef IVFFLAT_KMEANS_DEBUG buildstate->inertia = 0; - buildstate->listSums = palloc0(sizeof(double) * buildstate->lists); - buildstate->listCounts = palloc0(sizeof(int) * buildstate->lists); + buildstate->listSums = palloc0_array(double, buildstate->lists); + buildstate->listCounts = palloc0_array(int, buildstate->lists); #endif buildstate->ivfleader = NULL; diff --git a/src/ivfflat.h b/src/ivfflat.h index 6a628f7..c71c4a3 100644 --- a/src/ivfflat.h +++ b/src/ivfflat.h @@ -92,6 +92,8 @@ typedef Pointer Item; #if PG_VERSION_NUM < 160000 #define palloc_object(type) ((type *) palloc(sizeof(type))) #define palloc0_object(type) ((type *) palloc0(sizeof(type))) +#define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count))) +#define palloc0_array(type, count) ((type *) palloc0(sizeof(type) * (count))) #endif /* Variables */ diff --git a/src/ivfkmeans.c b/src/ivfkmeans.c index dc09552..3f7e0a4 100644 --- a/src/ivfkmeans.c +++ b/src/ivfkmeans.c @@ -26,7 +26,7 @@ InitCenters(Relation index, VectorArray samples, VectorArray centers, float *low FmgrInfo *procinfo; Oid collation; int64 j; - float *weight = palloc(samples->length * sizeof(float)); + float *weight = palloc_array(float, samples->length); int numCenters = centers->maxlen; int numSamples = samples->length; @@ -113,7 +113,7 @@ RandomCenters(Relation index, VectorArray centers, const IvfflatTypeInfo * typeI int dimensions = centers->dim; FmgrInfo *normprocinfo = IvfflatOptionalProcInfo(index, IVFFLAT_KMEANS_NORM_PROC); Oid collation = index->rd_indcollation[0]; - float *x = (float *) palloc(sizeof(float) * dimensions); + float *x = palloc_array(float, dimensions); /* Fill with random data */ while (centers->length < centers->maxlen) @@ -480,7 +480,7 @@ ElkanKmeans(Relation index, VectorArray samples, VectorArray centers, const Ivff static void CheckElements(VectorArray centers, const IvfflatTypeInfo * typeInfo) { - float *scratch = palloc(sizeof(float) * centers->dim); + float *scratch = palloc_array(float, centers->dim); for (int i = 0; i < centers->length; i++) { diff --git a/src/ivfscan.c b/src/ivfscan.c index f9cd248..5714e28 100644 --- a/src/ivfscan.c +++ b/src/ivfscan.c @@ -318,9 +318,9 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys) so->bas = GetAccessStrategy(BAS_BULKREAD); so->listQueue = pairingheap_allocate(CompareLists, scan); - so->listPages = palloc(maxProbes * sizeof(BlockNumber)); + so->listPages = palloc_array(BlockNumber, maxProbes); so->listIndex = 0; - so->lists = palloc(maxProbes * sizeof(IvfflatScanList)); + so->lists = palloc_array(IvfflatScanList, maxProbes); MemoryContextSwitchTo(oldCtx); diff --git a/src/sparsevec.c b/src/sparsevec.c index 656a6cc..57a7b73 100644 --- a/src/sparsevec.c +++ b/src/sparsevec.c @@ -26,6 +26,10 @@ #include "parser/scansup.h" #endif +#if PG_VERSION_NUM < 160000 +#define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count))) +#endif + typedef struct SparseInputElement { int32 index; @@ -223,7 +227,7 @@ sparsevec_in(PG_FUNCTION_ARGS) (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), errmsg("sparsevec cannot have more than %d non-zero elements", SPARSEVEC_MAX_NNZ))); - elements = palloc(maxNnz * sizeof(SparseInputElement)); + elements = palloc_array(SparseInputElement, maxNnz); pt = lit; diff --git a/src/vector.c b/src/vector.c index 7f46404..09ecf9c 100644 --- a/src/vector.c +++ b/src/vector.c @@ -30,8 +30,12 @@ #include "parser/scansup.h" #endif +#if PG_VERSION_NUM < 160000 +#define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count))) +#endif + #define STATE_DIMS(x) (ARR_DIMS(x)[0] - 1) -#define CreateStateDatums(dim) palloc(sizeof(Datum) * (dim + 1)) +#define CreateStateDatums(dim) palloc_array(Datum, (dim + 1)) #if defined(USE_TARGET_CLONES) && !defined(__FMA__) #define VECTOR_TARGET_CLONES __attribute__((target_clones("default", "fma"))) @@ -516,7 +520,7 @@ vector_to_float4(PG_FUNCTION_ARGS) Datum *datums; ArrayType *result; - datums = (Datum *) palloc(sizeof(Datum) * vec->dim); + datums = palloc_array(Datum, vec->dim); for (int i = 0; i < vec->dim; i++) datums[i] = Float4GetDatum(vec->x[i]);