1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-05 02:22:28 +03:00

Clean up some ad-hoc code for sorting and de-duplicating Lists.

heap.c and relcache.c contained nearly identical copies of logic
to insert OIDs into an OID list while preserving the list's OID
ordering (and rejecting duplicates, in one case but not the other).

The comments argue that this is faster than qsort for small numbers
of OIDs, which is at best unproven, and seems even less likely to be
true now that lappend_cell_oid has to move data around.  In any case
it's ugly and hard-to-follow code, and if we do have a lot of OIDs
to consider, it's O(N^2).

Hence, replace with simply lappend'ing OIDs to a List, then list_sort
the completed List, then remove adjacent duplicates if necessary.
This is demonstrably O(N log N) and it's much simpler for the
callers.  It's possible that this would be somewhat inefficient
if there were a very large number of duplicates, but that seems
unlikely in the existing usage.

This adds list_deduplicate_oid and list_oid_cmp infrastructure
to list.c.  I didn't bother with equivalent functionality for
integer or pointer Lists, but such could always be added later
if we find a use for it.

Discussion: https://postgr.es/m/26193.1563228600@sss.pgh.pa.us
This commit is contained in:
Tom Lane
2019-07-16 12:04:06 -04:00
parent 569ed7f483
commit 2f5b8eb5a2
4 changed files with 63 additions and 80 deletions

View File

@@ -1297,6 +1297,34 @@ list_concat_unique_oid(List *list1, const List *list2)
return list1;
}
/*
* Remove adjacent duplicates in a list of OIDs.
*
* It is caller's responsibility to have sorted the list to bring duplicates
* together, perhaps via list_sort(list, list_oid_cmp).
*/
void
list_deduplicate_oid(List *list)
{
int len;
Assert(IsOidList(list));
len = list_length(list);
if (len > 1)
{
ListCell *elements = list->elements;
int i = 0;
for (int j = 1; j < len; j++)
{
if (elements[i].oid_value != elements[j].oid_value)
elements[++i].oid_value = elements[j].oid_value;
}
list->length = i + 1;
}
check_list_invariants(list);
}
/*
* Free all storage in a list, and optionally the pointed-to elements
*/
@@ -1444,3 +1472,19 @@ list_sort(List *list, list_sort_comparator cmp)
if (len > 1)
qsort(list->elements, len, sizeof(ListCell), (qsort_comparator) cmp);
}
/*
* list_sort comparator for sorting a list into ascending OID order.
*/
int
list_oid_cmp(const ListCell *p1, const ListCell *p2)
{
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
if (v1 < v2)
return -1;
if (v1 > v2)
return 1;
return 0;
}