optimize list_reverse for pg < 13 since we might backport the fix to older versions

pull/5692/head
Onur Tirtir 2022-02-15 00:04:54 +03:00
parent 9f27c202e8
commit af529d3b55
1 changed files with 27 additions and 1 deletions

View File

@ -17,10 +17,10 @@
#include "lib/stringinfo.h"
#include "distributed/citus_safe_lib.h"
#include "distributed/listutils.h"
#include "distributed/pg_version_constants.h"
#include "nodes/pg_list.h"
#include "utils/memutils.h"
/*
* SortList takes in a list of void pointers, and sorts these pointers (and the
* values they point to) by applying the given comparison function. The function
@ -273,6 +273,7 @@ GenerateListFromElement(void *listElement, int listLength)
List *
list_reverse(const List *list)
{
#if PG_VERSION_NUM >= PG_VERSION_13
List *newList = NIL;
for (int i = list_length(list) - 1; i >= 0; i--)
{
@ -280,4 +281,29 @@ list_reverse(const List *list)
}
return newList;
#else
int nelements = list_length(list);
if (nelements == 0)
{
return NIL;
}
void **listElements = palloc0(sizeof(void *) * nelements);
int i = 0;
void *listElement = NULL;
foreach_ptr(listElement, list)
{
listElements[i++] = listElement;
}
List *newList = NIL;
for (i = nelements - 1; i >= 0; i--)
{
newList = lappend(newList, listElements[i]);
}
return newList;
#endif
}