Skip to content

Commit c23e3e6

Browse files
committed
Use list_copy_head() instead of list_truncate(list_copy(...), ...)
Truncating off the end of a freshly copied List is not a very efficient way of copying the first N elements of a List. In many of the cases that are updated here, the pattern was only being used to remove the final element of a List. That's about the best case for it, but there were many instances where the truncate trimming the List down much further. 4cc832f added list_copy_head(), so let's use it in cases where it's useful. Author: David Rowley Discussion: https://postgr.es/m/1986787.1657666922%40sss.pgh.pa.us
1 parent 50e4c28 commit c23e3e6

File tree

7 files changed

+25
-30
lines changed

7 files changed

+25
-30
lines changed

src/backend/catalog/objectaddress.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,7 @@ get_object_address_relobject(ObjectType objtype, List *object,
14531453
errmsg("must specify relation and object name")));
14541454

14551455
/* Extract relation name and open relation. */
1456-
relname = list_truncate(list_copy(object), nnames - 1);
1456+
relname = list_copy_head(object, nnames - 1);
14571457
relation = table_openrv_extended(makeRangeVarFromNameList(relname),
14581458
AccessShareLock,
14591459
missing_ok);
@@ -1528,7 +1528,7 @@ get_object_address_attribute(ObjectType objtype, List *object,
15281528
(errcode(ERRCODE_SYNTAX_ERROR),
15291529
errmsg("column name must be qualified")));
15301530
attname = strVal(llast(object));
1531-
relname = list_truncate(list_copy(object), list_length(object) - 1);
1531+
relname = list_copy_head(object, list_length(object) - 1);
15321532
/* XXX no missing_ok support here */
15331533
relation = relation_openrv(makeRangeVarFromNameList(relname), lockmode);
15341534
reloid = RelationGetRelid(relation);
@@ -1581,7 +1581,7 @@ get_object_address_attrdef(ObjectType objtype, List *object,
15811581
(errcode(ERRCODE_SYNTAX_ERROR),
15821582
errmsg("column name must be qualified")));
15831583
attname = strVal(llast(object));
1584-
relname = list_truncate(list_copy(object), list_length(object) - 1);
1584+
relname = list_copy_head(object, list_length(object) - 1);
15851585
/* XXX no missing_ok support here */
15861586
relation = relation_openrv(makeRangeVarFromNameList(relname), lockmode);
15871587
reloid = RelationGetRelid(relation);
@@ -1715,7 +1715,7 @@ get_object_address_opf_member(ObjectType objtype,
17151715
* address. The rest can be used directly by get_object_address_opcf().
17161716
*/
17171717
membernum = atoi(strVal(llast(linitial(object))));
1718-
copy = list_truncate(list_copy(linitial(object)), list_length(linitial(object)) - 1);
1718+
copy = list_copy_head(linitial(object), list_length(linitial(object)) - 1);
17191719

17201720
/* no missing_ok support here */
17211721
famaddr = get_object_address_opcf(OBJECT_OPFAMILY, copy, false);

src/backend/commands/dropcmds.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ owningrel_does_not_exist_skipping(List *object, const char **msg, char **name)
145145
List *parent_object;
146146
RangeVar *parent_rel;
147147

148-
parent_object = list_truncate(list_copy(object),
149-
list_length(object) - 1);
148+
parent_object = list_copy_head(object, list_length(object) - 1);
150149

151150
if (schema_does_not_exist_skipping(parent_object, msg, name))
152151
return true;
@@ -419,17 +418,17 @@ does_not_exist_skipping(ObjectType objtype, Node *object)
419418
{
420419
msg = gettext_noop("trigger \"%s\" for relation \"%s\" does not exist, skipping");
421420
name = strVal(llast(castNode(List, object)));
422-
args = NameListToString(list_truncate(list_copy(castNode(List, object)),
423-
list_length(castNode(List, object)) - 1));
421+
args = NameListToString(list_copy_head(castNode(List, object),
422+
list_length(castNode(List, object)) - 1));
424423
}
425424
break;
426425
case OBJECT_POLICY:
427426
if (!owningrel_does_not_exist_skipping(castNode(List, object), &msg, &name))
428427
{
429428
msg = gettext_noop("policy \"%s\" for relation \"%s\" does not exist, skipping");
430429
name = strVal(llast(castNode(List, object)));
431-
args = NameListToString(list_truncate(list_copy(castNode(List, object)),
432-
list_length(castNode(List, object)) - 1));
430+
args = NameListToString(list_copy_head(castNode(List, object),
431+
list_length(castNode(List, object)) - 1));
433432
}
434433
break;
435434
case OBJECT_EVENT_TRIGGER:
@@ -441,8 +440,8 @@ does_not_exist_skipping(ObjectType objtype, Node *object)
441440
{
442441
msg = gettext_noop("rule \"%s\" for relation \"%s\" does not exist, skipping");
443442
name = strVal(llast(castNode(List, object)));
444-
args = NameListToString(list_truncate(list_copy(castNode(List, object)),
445-
list_length(castNode(List, object)) - 1));
443+
args = NameListToString(list_copy_head(castNode(List, object),
444+
list_length(castNode(List, object)) - 1));
446445
}
447446
break;
448447
case OBJECT_FDW:

src/backend/commands/sequence.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1620,7 +1620,7 @@ process_owned_by(Relation seqrel, List *owned_by, bool for_identity)
16201620
RangeVar *rel;
16211621

16221622
/* Separate relname and attr name */
1623-
relname = list_truncate(list_copy(owned_by), nnames - 1);
1623+
relname = list_copy_head(owned_by, nnames - 1);
16241624
attrname = strVal(llast(owned_by));
16251625

16261626
/* Open and lock rel to ensure it won't go away meanwhile */

src/backend/optimizer/path/allpaths.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,9 +2053,8 @@ accumulate_append_subpath(Path *path, List **subpaths, List **special_subpaths)
20532053
*subpaths = list_concat(*subpaths,
20542054
list_copy_tail(apath->subpaths,
20552055
apath->first_partial_path));
2056-
new_special_subpaths =
2057-
list_truncate(list_copy(apath->subpaths),
2058-
apath->first_partial_path);
2056+
new_special_subpaths = list_copy_head(apath->subpaths,
2057+
apath->first_partial_path);
20592058
*special_subpaths = list_concat(*special_subpaths,
20602059
new_special_subpaths);
20612060
return;
@@ -3086,8 +3085,8 @@ get_useful_pathkeys_for_relation(PlannerInfo *root, RelOptInfo *rel,
30863085
root->query_pathkeys);
30873086
else if (npathkeys > 0)
30883087
useful_pathkeys_list = lappend(useful_pathkeys_list,
3089-
list_truncate(list_copy(root->query_pathkeys),
3090-
npathkeys));
3088+
list_copy_head(root->query_pathkeys,
3089+
npathkeys));
30913090
}
30923091

30933092
return useful_pathkeys_list;

src/backend/optimizer/path/indxpath.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3026,14 +3026,12 @@ expand_indexqual_rowcompare(PlannerInfo *root,
30263026

30273027
rc->rctype = (RowCompareType) op_strategy;
30283028
rc->opnos = new_ops;
3029-
rc->opfamilies = list_truncate(list_copy(clause->opfamilies),
3030-
matching_cols);
3031-
rc->inputcollids = list_truncate(list_copy(clause->inputcollids),
3032-
matching_cols);
3033-
rc->largs = list_truncate(copyObject(var_args),
3034-
matching_cols);
3035-
rc->rargs = list_truncate(copyObject(non_var_args),
3036-
matching_cols);
3029+
rc->opfamilies = list_copy_head(clause->opfamilies,
3030+
matching_cols);
3031+
rc->inputcollids = list_copy_head(clause->inputcollids,
3032+
matching_cols);
3033+
rc->largs = list_copy_head(var_args, matching_cols);
3034+
rc->rargs = list_copy_head(non_var_args, matching_cols);
30373035
iclause->indexquals = list_make1(make_simple_restrictinfo(root,
30383036
(Expr *) rc));
30393037
}

src/backend/optimizer/path/pathkeys.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2464,7 +2464,7 @@ truncate_useless_pathkeys(PlannerInfo *root,
24642464
else if (nuseful == list_length(pathkeys))
24652465
return pathkeys;
24662466
else
2467-
return list_truncate(list_copy(pathkeys), nuseful);
2467+
return list_copy_head(pathkeys, nuseful);
24682468
}
24692469

24702470
/*

src/backend/optimizer/plan/createplan.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,8 +1397,7 @@ create_append_plan(PlannerInfo *root, AppendPath *best_path, int flags)
13971397
*/
13981398
if (tlist_was_changed && (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST)))
13991399
{
1400-
tlist = list_truncate(list_copy(plan->plan.targetlist),
1401-
orig_tlist_length);
1400+
tlist = list_copy_head(plan->plan.targetlist, orig_tlist_length);
14021401
return inject_projection_plan((Plan *) plan, tlist,
14031402
plan->plan.parallel_safe);
14041403
}
@@ -1557,7 +1556,7 @@ create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path,
15571556
*/
15581557
if (tlist_was_changed && (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST)))
15591558
{
1560-
tlist = list_truncate(list_copy(plan->targetlist), orig_tlist_length);
1559+
tlist = list_copy_head(plan->targetlist, orig_tlist_length);
15611560
return inject_projection_plan(plan, tlist, plan->parallel_safe);
15621561
}
15631562
else

0 commit comments

Comments
 (0)