Improve list manipulation in several places

Started by Richard Guoalmost 3 years ago17 messageshackers
Jump to latest
#1Richard Guo
guofenglinux@gmail.com

There was discussion in [1]/messages/by-id/CAMbWs49aakL=PP7NcTajCtDyaVUE-NMVMGpaLEKreYbQknkQWA@mail.gmail.com about improvements to list manipulation in
several places. But since the discussion is not related to the topic in
that thread, fork a new thread here and attach a patch to show my
thoughts.

Some are just cosmetic changes by using macros. The others should have
performance gain from the avoidance of moving list entries. But I doubt
the performance gain can be noticed or measured, as currently there are
only a few places affected by the change. I still think the changes are
worthwhile though, because it is very likely that future usage of the
same scenario can benefit from these changes.

(Copying in David and Ranier. Ranier provided a patch about the changes
in list.c, but I'm not using that one.)

[1]: /messages/by-id/CAMbWs49aakL=PP7NcTajCtDyaVUE-NMVMGpaLEKreYbQknkQWA@mail.gmail.com
/messages/by-id/CAMbWs49aakL=PP7NcTajCtDyaVUE-NMVMGpaLEKreYbQknkQWA@mail.gmail.com

Thanks
Richard

Attachments:

v1-0001-Improve-list-manipulation-in-several-places.patchapplication/octet-stream; name=v1-0001-Improve-list-manipulation-in-several-places.patchDownload+106-9
#2Ranier Vilela
ranier.vf@gmail.com
In reply to: Richard Guo (#1)
Re: Improve list manipulation in several places

Em sex., 21 de abr. de 2023 às 04:34, Richard Guo <guofenglinux@gmail.com>
escreveu:

There was discussion in [1] about improvements to list manipulation in
several places. But since the discussion is not related to the topic in
that thread, fork a new thread here and attach a patch to show my
thoughts.

Some are just cosmetic changes by using macros. The others should have
performance gain from the avoidance of moving list entries. But I doubt
the performance gain can be noticed or measured, as currently there are
only a few places affected by the change. I still think the changes are
worthwhile though, because it is very likely that future usage of the
same scenario can benefit from these changes.

+1

Perhaps list_delete_nth_cell needs to check NIL too?
+ if (list == NIL)
+ return NIL;
+lcons_copy(void *datum, const List *list)
+lappend_copy(const List *list, void *datum)
list param pointer can be const here not?

regards,
Ranier Vilela

#3David Rowley
dgrowleyml@gmail.com
In reply to: Ranier Vilela (#2)
Re: Improve list manipulation in several places

On Fri, 21 Apr 2023 at 23:16, Ranier Vilela <ranier.vf@gmail.com> wrote:

Perhaps list_delete_nth_cell needs to check NIL too?
+ if (list == NIL)
+ return NIL;

Which cell would you be deleting from an empty list?

David

#4Ranier Vilela
ranier.vf@gmail.com
In reply to: David Rowley (#3)
Re: Improve list manipulation in several places

Em sex, 21 de abr de 2023 9:10 AM, David Rowley <dgrowleyml@gmail.com>
escreveu:

On Fri, 21 Apr 2023 at 23:16, Ranier Vilela <ranier.vf@gmail.com> wrote:

Perhaps list_delete_nth_cell needs to check NIL too?
+ if (list == NIL)
+ return NIL;

Which cell would you be deleting from an empty list?

None.
But list_delete_nth_cel can checks a length of NIL list.

Perhaps a assert?

regards,
Ranier Vilela

#5Peter Eisentraut
peter_e@gmx.net
In reply to: Richard Guo (#1)
Re: Improve list manipulation in several places

On 21.04.23 09:34, Richard Guo wrote:

There was discussion in [1] about improvements to list manipulation in
several places.  But since the discussion is not related to the topic in
that thread, fork a new thread here and attach a patch to show my
thoughts.

Some are just cosmetic changes by using macros.  The others should have
performance gain from the avoidance of moving list entries.  But I doubt
the performance gain can be noticed or measured, as currently there are
only a few places affected by the change.  I still think the changes are
worthwhile though, because it is very likely that future usage of the
same scenario can benefit from these changes.

Can you explain the changes?

Maybe this patch should be split up. It seems some of the changes are
trivial simplifications using existing APIs, while others introduce new
functions.

#6Richard Guo
guofenglinux@gmail.com
In reply to: Peter Eisentraut (#5)
Re: Improve list manipulation in several places

On Sat, Apr 22, 2023 at 12:55 AM Peter Eisentraut <
peter.eisentraut@enterprisedb.com> wrote:

On 21.04.23 09:34, Richard Guo wrote:

There was discussion in [1] about improvements to list manipulation in
several places. But since the discussion is not related to the topic in
that thread, fork a new thread here and attach a patch to show my
thoughts.

Some are just cosmetic changes by using macros. The others should have
performance gain from the avoidance of moving list entries. But I doubt
the performance gain can be noticed or measured, as currently there are
only a few places affected by the change. I still think the changes are
worthwhile though, because it is very likely that future usage of the
same scenario can benefit from these changes.

Can you explain the changes?

Maybe this patch should be split up. It seems some of the changes are
trivial simplifications using existing APIs, while others introduce new
functions.

Thanks for the suggestion. I've split the patch into two as attached.
0001 is just a minor simplification by replacing lfirst(list_head(list))
with linitial(list). 0002 introduces new functions to reduce the
movement of list elements in several places so as to gain performance
improvement and benefit future callers.

Thanks
Richard

Attachments:

v2-0002-Improve-list-manipulation-in-several-places.patchapplication/octet-stream; name=v2-0002-Improve-list-manipulation-in-several-places.patchDownload+105-8
v2-0001-A-minor-simplification-for-List-manipulation.patchapplication/octet-stream; name=v2-0001-A-minor-simplification-for-List-manipulation.patchDownload+1-2
#7Richard Guo
guofenglinux@gmail.com
In reply to: Ranier Vilela (#2)
Re: Improve list manipulation in several places

On Fri, Apr 21, 2023 at 7:16 PM Ranier Vilela <ranier.vf@gmail.com> wrote:

+lcons_copy(void *datum, const List *list)
+lappend_copy(const List *list, void *datum)
list param pointer can be const here not?

Correct. Good point. V2 patch does that.

Thanks
Richard

#8Tender Wang
tndrwang@gmail.com
In reply to: Richard Guo (#1)
Re: Improve list manipulation in several places

Richard Guo <guofenglinux@gmail.com> 于2023年4月21日周五 15:35写道:

There was discussion in [1] about improvements to list manipulation in
several places. But since the discussion is not related to the topic in
that thread, fork a new thread here and attach a patch to show my
thoughts.

Some are just cosmetic changes by using macros. The others should have
performance gain from the avoidance of moving list entries. But I doubt
the performance gain can be noticed or measured, as currently there are
only a few places affected by the change. I still think the changes are
worthwhile though, because it is very likely that future usage of the
same scenario can benefit from these changes.

I doubt the performance gain from lappend_copy func. new_tail_cell in
lappend may not enter enlarge_list in most cases, because we
may allocate extra cells in new_list(see the comment in new_list).

Show quoted text

(Copying in David and Ranier. Ranier provided a patch about the changes
in list.c, but I'm not using that one.)

[1]
/messages/by-id/CAMbWs49aakL=PP7NcTajCtDyaVUE-NMVMGpaLEKreYbQknkQWA@mail.gmail.com

Thanks
Richard

#9Peter Eisentraut
peter_e@gmx.net
In reply to: Richard Guo (#6)
Re: Improve list manipulation in several places

On 23.04.23 08:42, Richard Guo wrote:

Thanks for the suggestion.  I've split the patch into two as attached.
0001 is just a minor simplification by replacing lfirst(list_head(list))
with linitial(list).  0002 introduces new functions to reduce the
movement of list elements in several places so as to gain performance
improvement and benefit future callers.

These look sensible to me. If you could show some numbers that support
the claim that there is a performance advantage, it would be even more
convincing.

#10Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Peter Eisentraut (#9)
Re: Improve list manipulation in several places

On 2023-May-08, Peter Eisentraut wrote:

On 23.04.23 08:42, Richard Guo wrote:

Thanks for the suggestion.  I've split the patch into two as attached.
0001 is just a minor simplification by replacing lfirst(list_head(list))
with linitial(list).  0002 introduces new functions to reduce the
movement of list elements in several places so as to gain performance
improvement and benefit future callers.

These look sensible to me. If you could show some numbers that support the
claim that there is a performance advantage, it would be even more
convincing.

0001 looks fine.

The problem I see is that each of these new functions has a single
caller, and the only one that looks like it could have a performance
advantage is list_copy_move_nth_to_head() (which is the weirdest of the
lot). I'm inclined not to have any of these single-use functions unless
a performance case can be made for them.

--
Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/

#11Ranier Vilela
ranier.vf@gmail.com
In reply to: Alvaro Herrera (#10)
Re: Improve list manipulation in several places

Em seg., 8 de mai. de 2023 às 14:26, Alvaro Herrera <alvherre@alvh.no-ip.org>
escreveu:

On 2023-May-08, Peter Eisentraut wrote:

On 23.04.23 08:42, Richard Guo wrote:

Thanks for the suggestion. I've split the patch into two as attached.
0001 is just a minor simplification by replacing

lfirst(list_head(list))

with linitial(list). 0002 introduces new functions to reduce the
movement of list elements in several places so as to gain performance
improvement and benefit future callers.

These look sensible to me. If you could show some numbers that support

the

claim that there is a performance advantage, it would be even more
convincing.

0001 looks fine.

The problem I see is that each of these new functions has a single
caller, and the only one that looks like it could have a performance
advantage is list_copy_move_nth_to_head() (which is the weirdest of the
lot). I'm inclined not to have any of these single-use functions unless
a performance case can be made for them.

I think you missed list_nth_xid, It makes perfect sense to exist.

regards,
Ranier Vilela

#12Richard Guo
guofenglinux@gmail.com
In reply to: Peter Eisentraut (#9)
Re: Improve list manipulation in several places

On Mon, May 8, 2023 at 11:22 PM Peter Eisentraut <
peter.eisentraut@enterprisedb.com> wrote:

On 23.04.23 08:42, Richard Guo wrote:

Thanks for the suggestion. I've split the patch into two as attached.
0001 is just a minor simplification by replacing lfirst(list_head(list))
with linitial(list). 0002 introduces new functions to reduce the
movement of list elements in several places so as to gain performance
improvement and benefit future callers.

These look sensible to me. If you could show some numbers that support
the claim that there is a performance advantage, it would be even more
convincing.

Thanks Peter for looking at those patches. I tried to devise a query to
show performance gain but did not succeed :-(. So I begin to wonder if
0002 is worthwhile to do, as it seems that it does not solve any real
problem.

Thanks
Richard

#13Richard Guo
guofenglinux@gmail.com
In reply to: Alvaro Herrera (#10)
Re: Improve list manipulation in several places

On Tue, May 9, 2023 at 1:26 AM Alvaro Herrera <alvherre@alvh.no-ip.org>
wrote:

The problem I see is that each of these new functions has a single
caller, and the only one that looks like it could have a performance
advantage is list_copy_move_nth_to_head() (which is the weirdest of the
lot). I'm inclined not to have any of these single-use functions unless
a performance case can be made for them.

Yeah, maybe this is the reason I failed to devise a query that shows any
performance gain. I tried with a query which makes the 'all_pathkeys'
in sort_inner_and_outer being length of 500 and still cannot see any
notable performance improvements gained by list_copy_move_nth_to_head.
Maybe the cost of other parts of planning swamps the performance gain
here? Now I agree that maybe 0002 is not worthwhile to do.

Thanks
Richard

#14Richard Guo
guofenglinux@gmail.com
In reply to: Ranier Vilela (#11)
Re: Improve list manipulation in several places

On Tue, May 9, 2023 at 1:48 AM Ranier Vilela <ranier.vf@gmail.com> wrote:

I think you missed list_nth_xid, It makes perfect sense to exist.

It seems that list_nth_xid is more about simplification. So maybe we
should put it in 0001?

Thanks
Richard

#15Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Ranier Vilela (#11)
Re: Improve list manipulation in several places

On 2023-May-08, Ranier Vilela wrote:

Em seg., 8 de mai. de 2023 às 14:26, Alvaro Herrera <alvherre@alvh.no-ip.org>
escreveu:

The problem I see is that each of these new functions has a single
caller, and the only one that looks like it could have a performance
advantage is list_copy_move_nth_to_head() (which is the weirdest of the
lot). I'm inclined not to have any of these single-use functions unless
a performance case can be made for them.

I think you missed list_nth_xid, It makes perfect sense to exist.

I saw that one; it's just syntactic sugar, just like list_nth_int and
list_nth_oid, except it has only one possible callsite instead of a
dozen like those others. I see no harm in that function, but no
practical advantage to it either. Xid lists are a very fringe feature,
there being exactly one place in the whole server that uses them.

--
Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/

#16Peter Eisentraut
peter_e@gmx.net
In reply to: Richard Guo (#13)
Re: Improve list manipulation in several places

On 09.05.23 05:13, Richard Guo wrote:

On Tue, May 9, 2023 at 1:26 AM Alvaro Herrera <alvherre@alvh.no-ip.org
<mailto:alvherre@alvh.no-ip.org>> wrote:

The problem I see is that each of these new functions has a single
caller, and the only one that looks like it could have a performance
advantage is list_copy_move_nth_to_head() (which is the weirdest of the
lot).  I'm inclined not to have any of these single-use functions unless
a performance case can be made for them.

Yeah, maybe this is the reason I failed to devise a query that shows any
performance gain.  I tried with a query which makes the 'all_pathkeys'
in sort_inner_and_outer being length of 500 and still cannot see any
notable performance improvements gained by list_copy_move_nth_to_head.
Maybe the cost of other parts of planning swamps the performance gain
here?  Now I agree that maybe 0002 is not worthwhile to do.

I have committed patch 0001. Since you have withdrawn 0002, this closes
the commit fest item.

#17Richard Guo
guofenglinux@gmail.com
In reply to: Peter Eisentraut (#16)
Re: Improve list manipulation in several places

On Mon, Jul 3, 2023 at 5:41 PM Peter Eisentraut <
peter.eisentraut@enterprisedb.com> wrote:

On 09.05.23 05:13, Richard Guo wrote:

Yeah, maybe this is the reason I failed to devise a query that shows any
performance gain. I tried with a query which makes the 'all_pathkeys'
in sort_inner_and_outer being length of 500 and still cannot see any
notable performance improvements gained by list_copy_move_nth_to_head.
Maybe the cost of other parts of planning swamps the performance gain
here? Now I agree that maybe 0002 is not worthwhile to do.

I have committed patch 0001. Since you have withdrawn 0002, this closes
the commit fest item.

Thanks for pushing it and closing the item!

Thanks
Richard