@@ -410,6 +410,9 @@ insert_new_cell(List *list, int pos)
410
410
/*
411
411
* Insert the given datum at position 'pos' (measured from 0) in the list.
412
412
* 'pos' must be valid, ie, 0 <= pos <= list's length.
413
+ *
414
+ * Note that this takes time proportional to the distance to the end of the
415
+ * list, since the following entries must be moved.
413
416
*/
414
417
List *
415
418
list_insert_nth (List * list , int pos , void * datum )
@@ -460,6 +463,9 @@ list_insert_nth_oid(List *list, int pos, Oid datum)
460
463
* value, rather than continuing to use the pointer passed as the
461
464
* second argument.
462
465
*
466
+ * Note that this takes time proportional to the length of the list,
467
+ * since the existing entries must be moved.
468
+ *
463
469
* Caution: before Postgres 8.0, the original List was unmodified and
464
470
* could be considered to retain its separate identity. This is no longer
465
471
* the case.
@@ -525,6 +531,10 @@ lcons_oid(Oid datum, List *list)
525
531
* Callers should be sure to use the return value as the new pointer to the
526
532
* concatenated list: the 'list1' input pointer may or may not be the same
527
533
* as the returned pointer.
534
+ *
535
+ * Note that this takes at least time proportional to the length of list2.
536
+ * It'd typically be the case that we have to enlarge list1's storage,
537
+ * probably adding time proportional to the length of list1.
528
538
*/
529
539
List *
530
540
list_concat (List * list1 , const List * list2 )
@@ -623,6 +633,8 @@ list_truncate(List *list, int new_size)
623
633
* Return true iff 'datum' is a member of the list. Equality is
624
634
* determined via equal(), so callers should ensure that they pass a
625
635
* Node as 'datum'.
636
+ *
637
+ * This does a simple linear search --- avoid using it on long lists.
626
638
*/
627
639
bool
628
640
list_member (const List * list , const void * datum )
@@ -706,6 +718,9 @@ list_member_oid(const List *list, Oid datum)
706
718
* Delete the n'th cell (counting from 0) in list.
707
719
*
708
720
* The List is pfree'd if this was the last member.
721
+ *
722
+ * Note that this takes time proportional to the distance to the end of the
723
+ * list, since the following entries must be moved.
709
724
*/
710
725
List *
711
726
list_delete_nth_cell (List * list , int n )
@@ -777,6 +792,9 @@ list_delete_nth_cell(List *list, int n)
777
792
*
778
793
* The List is pfree'd if this was the last member. However, we do not
779
794
* touch any data the cell might've been pointing to.
795
+ *
796
+ * Note that this takes time proportional to the distance to the end of the
797
+ * list, since the following entries must be moved.
780
798
*/
781
799
List *
782
800
list_delete_cell (List * list , ListCell * cell )
@@ -787,6 +805,8 @@ list_delete_cell(List *list, ListCell *cell)
787
805
/*
788
806
* Delete the first cell in list that matches datum, if any.
789
807
* Equality is determined via equal().
808
+ *
809
+ * This does a simple linear search --- avoid using it on long lists.
790
810
*/
791
811
List *
792
812
list_delete (List * list , void * datum )
@@ -870,6 +890,13 @@ list_delete_oid(List *list, Oid datum)
870
890
* where the intent is to alter the list rather than just traverse it.
871
891
* Beware that the list is modified, whereas the Lisp-y coding leaves
872
892
* the original list head intact in case there's another pointer to it.
893
+ *
894
+ * Note that this takes time proportional to the length of the list,
895
+ * since the remaining entries must be moved. Consider reversing the
896
+ * list order so that you can use list_delete_last() instead. However,
897
+ * if that causes you to replace lappend() with lcons(), you haven't
898
+ * improved matters. (In short, you can make an efficient stack from
899
+ * a List, but not an efficient FIFO queue.)
873
900
*/
874
901
List *
875
902
list_delete_first (List * list )
@@ -884,9 +911,6 @@ list_delete_first(List *list)
884
911
885
912
/*
886
913
* Delete the last element of the list.
887
- *
888
- * This is the opposite of list_delete_first(), but is noticeably cheaper
889
- * with a long list, since no data need be moved.
890
914
*/
891
915
List *
892
916
list_delete_last (List * list )
@@ -910,6 +934,9 @@ list_delete_last(List *list)
910
934
* Delete the first N cells of the list.
911
935
*
912
936
* The List is pfree'd if the request causes all cells to be deleted.
937
+ *
938
+ * Note that this takes time proportional to the distance to the end of the
939
+ * list, since the following entries must be moved.
913
940
*/
914
941
List *
915
942
list_delete_first_n (List * list , int n )
@@ -989,8 +1016,10 @@ list_delete_first_n(List *list, int n)
989
1016
* you probably want to use list_concat_unique() instead to avoid wasting
990
1017
* the storage of the old x list.
991
1018
*
992
- * This function could probably be implemented a lot faster if it is a
993
- * performance bottleneck.
1019
+ * Note that this takes time proportional to the product of the list
1020
+ * lengths, so beware of using it on long lists. (We could probably
1021
+ * improve that, but really you should be using some other data structure
1022
+ * if this'd be a performance bottleneck.)
994
1023
*/
995
1024
List *
996
1025
list_union (const List * list1 , const List * list2 )
@@ -1094,6 +1123,11 @@ list_union_oid(const List *list1, const List *list2)
1094
1123
* This variant works on lists of pointers, and determines list
1095
1124
* membership via equal(). Note that the list1 member will be pointed
1096
1125
* to in the result.
1126
+ *
1127
+ * Note that this takes time proportional to the product of the list
1128
+ * lengths, so beware of using it on long lists. (We could probably
1129
+ * improve that, but really you should be using some other data structure
1130
+ * if this'd be a performance bottleneck.)
1097
1131
*/
1098
1132
List *
1099
1133
list_intersection (const List * list1 , const List * list2 )
@@ -1152,6 +1186,11 @@ list_intersection_int(const List *list1, const List *list2)
1152
1186
*
1153
1187
* This variant works on lists of pointers, and determines list
1154
1188
* membership via equal()
1189
+ *
1190
+ * Note that this takes time proportional to the product of the list
1191
+ * lengths, so beware of using it on long lists. (We could probably
1192
+ * improve that, but really you should be using some other data structure
1193
+ * if this'd be a performance bottleneck.)
1155
1194
*/
1156
1195
List *
1157
1196
list_difference (const List * list1 , const List * list2 )
@@ -1256,6 +1295,8 @@ list_difference_oid(const List *list1, const List *list2)
1256
1295
*
1257
1296
* Whether an element is already a member of the list is determined
1258
1297
* via equal().
1298
+ *
1299
+ * This does a simple linear search --- avoid using it on long lists.
1259
1300
*/
1260
1301
List *
1261
1302
list_append_unique (List * list , void * datum )
@@ -1313,6 +1354,11 @@ list_append_unique_oid(List *list, Oid datum)
1313
1354
* modified in-place rather than being copied. However, callers of this
1314
1355
* function may have strict ordering expectations -- i.e. that the relative
1315
1356
* order of those list2 elements that are not duplicates is preserved.
1357
+ *
1358
+ * Note that this takes time proportional to the product of the list
1359
+ * lengths, so beware of using it on long lists. (We could probably
1360
+ * improve that, but really you should be using some other data structure
1361
+ * if this'd be a performance bottleneck.)
1316
1362
*/
1317
1363
List *
1318
1364
list_concat_unique (List * list1 , const List * list2 )
@@ -1401,6 +1447,8 @@ list_concat_unique_oid(List *list1, const List *list2)
1401
1447
*
1402
1448
* It is caller's responsibility to have sorted the list to bring duplicates
1403
1449
* together, perhaps via list_sort(list, list_oid_cmp).
1450
+ *
1451
+ * Note that this takes time proportional to the length of the list.
1404
1452
*/
1405
1453
void
1406
1454
list_deduplicate_oid (List * list )
@@ -1557,6 +1605,8 @@ list_copy_deep(const List *oldlist)
1557
1605
*
1558
1606
* Like qsort(), this provides no guarantees about sort stability
1559
1607
* for equal keys.
1608
+ *
1609
+ * This is based on qsort(), so it likewise has O(N log N) runtime.
1560
1610
*/
1561
1611
void
1562
1612
list_sort (List * list , list_sort_comparator cmp )
0 commit comments