pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: David Rowley <david.rowley@2ndquadrant.com>
Cc: Andres Freund <andres@anarazel.de>
Cc: Robert Haas <robertmhaas@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Subject: Re: POC: converting Lists into arrays
Date: Fri, 24 May 2019 20:53:47 -0400
Message-ID: <14626.1558745627@sss.pgh.pa.us> (raw)
In-Reply-To: <1131.1551746172@sss.pgh.pa.us>
References: <481.1551390571@sss.pgh.pa.us>
	<24783.1551568303@sss.pgh.pa.us>
	<20190303043424.itzy3ge52xrkpmpr@alap3.anarazel.de>
	<437.1551637744@sss.pgh.pa.us>
	<CA+TgmoZM4c=v0ifR++i09JFtNtnqTq_HkFmbFjwWqbUJffOLmA@mail.gmail.com>
	<12684.1551723095@sss.pgh.pa.us>
	<20190304190612.vgqqsowzkrh22623@alap3.anarazel.de>
	<26464.1551734920@sss.pgh.pa.us>
	<20190304221101.hdg4vj5fo4eewh3b@alap3.anarazel.de>
	<CAKJS1f-pNfoUJrmU8vgD1WrKLqs9WxOVLQ0RpSWu9h6W9RnypA@mail.gmail.com>
	<20190304235402.nod3gbotk2qtd4nh@alap3.anarazel.de>
	<CAKJS1f-yS201hAAmnxcLb2aeTYNZ_A9R1JNXF6hTVXjhHYN7rQ@mail.gmail.com>
	<1131.1551746172@sss.pgh.pa.us>

Here's a new version of the Lists-as-arrays patch.  It's rebased up to
HEAD, and I also realized that I could fix the problem with multiple
evaluation of the List arguments of foreach etc. by using structure
assignment.  So that gets rid of a large chunk of the semantic gotchas
that were in the previous patch.  You still have to be careful about
code that deletes list entries within a foreach() over the list ---
but nearly all such code is using list_delete_cell, which means
you'll have to touch it anyway because of the API change for that
function.

Previously, the typical logic for deletion-within-a-loop involved
either advancing or not advancing a "prev" pointer that was used
with list_delete_cell.  The way I've recoded that here changes those
loops to use an integer list index that gets incremented or not.

Now, it turns out that the new formulation of foreach() is really
strictly equivalent to

	for (int pos = 0; pos < list_length(list); pos++)
	{
		whatever-type item = list_nth(list, pos);
		...
	}

which means that it could cope fine with deletion of the current
list element if we were to provide some supported way of not
incrementing the list index counter.  That is, instead of
code that looks more or less like this:

	for (int pos = 0; pos < list_length(list); pos++)
	{
		whatever-type item = list_nth(list, pos);
		...
		if (delete_cur)
		{
			list = list_delete_nth_cell(list, pos);
			pos--;   /* keep loop in sync with deletion */
		}
	}

we could write, say:

	foreach(lc, list)
	{
		whatever-type item = lfirst(lc);
		...
		if (delete_cur)
		{
			list = list_delete_cell(list, lc);
			foreach_backup(lc); /* keep loop in sync with deletion */
		}
	}

which is the same thing under the hood.  I'm not quite sure if that way
is better or not.  It's more magical than explicitly manipulating a list
index, but it's also shorter and therefore less subject to typos.

			regards, tom lane

Attachments:

  [application/x-gzip] reimplement-List-as-array-4.patch.gz (44.4K, ../14626.1558745627@sss.pgh.pa.us/2-reimplement-List-as-array-4.patch.gz)
  download

view thread (130+ messages)  latest in thread

Message-ID: <14626.1558745627@sss.pgh.pa.us>
Permalink:  ../14626.1558745627@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/14626.1558745627@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, david.rowley@2ndquadrant.com, andres@anarazel.de, robertmhaas@gmail.com, pgsql-hackers@lists.postgresql.org
  Subject: Re: POC: converting Lists into arrays
  In-Reply-To: <14626.1558745627@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox