All of lore.kernel.org
 help / color / mirror / Atom feed
From: Plato Kiorpelidis <kioplato@gmail.com>
To: phillip.wood@dunelm.org.uk
Cc: git@vger.kernel.org, matheus.bernardino@usp.br,
	mhagger@alum.mit.edu,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: Re: [RFC PATCH 3/6] dir-iterator: refactor dir_iterator_advance()
Date: Wed, 27 Apr 2022 17:32:06 +0300	[thread overview]
Message-ID: <20220427143206.6cqksztbk4exift3@compass> (raw)
In-Reply-To: <35160d46-d337-2110-f968-238abb7e9f0e@gmail.com>

On 22/04/11 02:26PM, Phillip Wood wrote:
> Hi Plato
> 
> On 10/04/2022 12:18, Plato Kiorpelidis wrote:
> > Simplify dir_iterator_advance by switch from iterative to recursive
> > implementation. In each recursive step one action is performed.
> > 
> > This makes dir-iterator easier to work with, understand and introduce
> > new functionality.
> 
> I think that this message could explain a bit more about why it is necessary
> to use a recursive approach. As we are implementing an iterator we cannot
> keep state in the stack between iterations so how does using recursive
> approach help us?

It's not necessary from a functioning perspective. This iterator works on tree
like structures (exception cyclic symlinks), performing pre-order and post-order
traversal on some vertices, in our case directories. Traditionally, these
operations, i.e. pre-order, in-order, post-order, in textbooks, lectures and
online sources are implemented recursively and not iteratively. Therefore it
helps reduce mental load for the reader, since it's easier to follow as it
reminds of the same code paradigm, e.g. post-order:
  * Recurse left child
  * Recurse right child
  * Visit subroot

In general, graph algorithms are more readable when implemented recursively.
In each recursive step a set of instructions are executed and recursion lays out
the code in such a way that these instructions stand out symmetrically for each
recursive step.

As a result, recursive dir-iterator is more maintainable, as its code structure
is more similar to other graph algorithms and to what we're already used to,
than its iterative implementation.

A nice side effect of recursion is that it requires one less indentation depth.
This further simplifies the code.

> I reviewed this series as a way to learn the directory iterator code as I'd
> not looked at it before. Having read the existing code I think the it is
> fairly easy to follow. I'm not sure the changes here improve that. I also
> found these changes harder to follow because they renamed existing functions
> and variables in addition to moving the code around.

Michael Haggerty said that dir-iterator code is quite intricate[1], I agree and
tried to simplify it. My conversion from iterative to recursive implementation
tries to deal with this problem. The proposed advantages are specified above.

> In order in implement returning the directory name after its contents I
> think all we need is to remember whether we've returned the name when we
> readdir() returns NULL so we don't pop it too soon. It's not clear that we
> need a lot of refactoring to do that, especially as the patches you linked
> to in the cover letter seem to avoid that.

It's one more thing we need to keep track of, that's not required and can be
avoided. It further tangles the code, introducing more cases. The recursive
implementation that I propose, does not need to keep track of this information.
Probably there's an equal iterative implementation that also does not need to
keep track of this information.


[1]: https://public-inbox.org/git/7a665631-da6a-4b9f-b9e7-750f2504eccd@alum.mit.edu/

Thanks,
Plato

> Best Wishes
> 
> Phillip
> 
> > [...]

  reply	other threads:[~2022-04-27 14:32 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-10 11:18 [RFC PATCH 0/6][GSoC] iterate dirs before or after their contents Plato Kiorpelidis
2022-04-10 11:18 ` [RFC PATCH 1/6] t0066: improve readablity of dir-iterator tests Plato Kiorpelidis
2022-04-11 13:16   ` Phillip Wood
2022-04-24 19:25     ` Plato Kiorpelidis
2022-04-10 11:18 ` [RFC PATCH 2/6] t0066: better test coverage for dir-iterator Plato Kiorpelidis
2022-04-10 11:18 ` [RFC PATCH 3/6] dir-iterator: refactor dir_iterator_advance() Plato Kiorpelidis
2022-04-11 11:11   ` Ævar Arnfjörð Bjarmason
2022-04-11 13:40     ` Phillip Wood
2022-04-27 15:45     ` Plato Kiorpelidis
2022-04-11 13:26   ` Phillip Wood
2022-04-27 14:32     ` Plato Kiorpelidis [this message]
2022-04-10 11:18 ` [RFC PATCH 4/6] dir-iterator: iterate dirs before or after their contents Plato Kiorpelidis
2022-04-11 13:31   ` Phillip Wood
2022-04-27 14:57     ` Plato Kiorpelidis
2022-04-10 11:18 ` [RFC PATCH 5/6] t0066: remove redundant tests Plato Kiorpelidis
2022-04-11 11:10   ` Ævar Arnfjörð Bjarmason
2022-04-27 16:00     ` Plato Kiorpelidis
2022-04-10 11:18 ` [RFC PATCH 6/6] test-dir-iterator: handle EACCES errno by dir-iterator Plato Kiorpelidis
2022-04-11 11:04   ` Ævar Arnfjörð Bjarmason
2022-04-27 17:30     ` Plato Kiorpelidis
2022-04-11 13:37 ` [RFC PATCH 0/6][GSoC] iterate dirs before or after their contents Phillip Wood
2022-04-19 13:06   ` Plato Kiorpelidis

Reply instructions:

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

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

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220427143206.6cqksztbk4exift3@compass \
    --to=kioplato@gmail.com \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=matheus.bernardino@usp.br \
    --cc=mhagger@alum.mit.edu \
    --cc=phillip.wood@dunelm.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.