public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Christoph Hellwig <hch@infradead.org>
To: Eric Sandeen <sandeen@sandeen.net>
Cc: xfs-oss <xfs@oss.sgi.com>
Subject: Re: duplicate code in dir2
Date: Thu, 23 Jun 2011 06:53:19 -0400	[thread overview]
Message-ID: <20110623105318.GA11195@infradead.org> (raw)
In-Reply-To: <4E020912.9020106@sandeen.net>

> /src/git/linux-2.6/fs/xfs/xfs_dir2_node.c(251)
> /src/git/linux-2.6/fs/xfs/xfs_dir2_leaf.c(440)
>                 if (index < be16_to_cpu(leaf->hdr.count))
>                         memmove(lep + 1, lep,
>                                 (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep));
>                 lfloglow = index;
>                 lfloghigh = be16_to_cpu(leaf->hdr.count);
>                 be16_add_cpu(&leaf->hdr.count, 1);
>         else {

This one and the the chunks following it are easily factorable, and I've
created a patch. It's 100 lines of common code, just with the order of
asssers switched, and one of them reusing the lep pointer on entry, and
the other one recalculating it.  With the function header and lots of
parameters we don't actually remove a whole lot of code, but having this
relatively complex piece of code only once sounds like a good idea.

> /src/git/linux-2.6/fs/xfs/xfs_dir2_node.c(582)
> /src/git/linux-2.6/fs/xfs/xfs_dir2_leaf.c(1349)
>         for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
>                                 be32_to_cpu(lep->hashval) == args->hashval;
>                                 lep++, index++) {
>                 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
>                         continue;
>                 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
>                 if (newdb != curdb) {
> 
> /src/git/linux-2.6/fs/xfs/xfs_dir2_node.c(442)
> /src/git/linux-2.6/fs/xfs/xfs_dir2_leaf.c(1349)
>         for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
>                                 be32_to_cpu(lep->hashval) == args->hashval;
>                                 lep++, index++) {
>                 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
>                         continue;
>                 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
>                 if (newdb != curdb) {

This seems like a relatively common patter, which has even more
occurances with minimal variations, but I'm not sure there's an easy way
to factor it.

I'd prefer to rewrite the loops to something more readable like:

	for (; index < be16_to_cpu(leaf->hdr.count); index++) {
		lep = &leaf->ents[index];

		if (be32_to_cpu(lep->hashval) != args->hashval)
			break;
		if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
			continue;

		...
	}

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

      parent reply	other threads:[~2011-06-23 10:53 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-22 15:24 duplicate code in dir2 Eric Sandeen
2011-06-22 15:25 ` Eric Sandeen
2011-06-23 11:03   ` Christoph Hellwig
2011-06-23 10:53 ` Christoph Hellwig [this message]

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=20110623105318.GA11195@infradead.org \
    --to=hch@infradead.org \
    --cc=sandeen@sandeen.net \
    --cc=xfs@oss.sgi.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox