xfs
[Top] [All Lists]

Re: duplicate code in dir2

To: Eric Sandeen <sandeen@xxxxxxxxxxx>
Subject: Re: duplicate code in dir2
From: Christoph Hellwig <hch@xxxxxxxxxxxxx>
Date: Thu, 23 Jun 2011 06:53:19 -0400
Cc: xfs-oss <xfs@xxxxxxxxxxx>
In-reply-to: <4E020912.9020106@xxxxxxxxxxx>
References: <4E020912.9020106@xxxxxxxxxxx>
User-agent: Mutt/1.5.21 (2010-09-15)
> /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;

                ...
        }

<Prev in Thread] Current Thread [Next in Thread>