linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andy Shevchenko <andy.shevchenko@gmail.com>
To: Leno Hou <lenohou@gmail.com>
Cc: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v1]] lib/btree.c: optimise the code by previously getpos function
Date: Fri, 28 Apr 2017 19:40:34 +0300	[thread overview]
Message-ID: <CAHp75VfX0u11mRieRAH=P_6xzbL2cKjbhKNopv-ia70duH1Cfg@mail.gmail.com> (raw)
In-Reply-To: <1491893636-60759-1-git-send-email-lenohou@gmail.com>

On Tue, Apr 11, 2017 at 9:53 AM, Leno Hou <lenohou@gmail.com> wrote:
> This patch optimized the code by previously getpos function call.
> Therefore, It's takes the convenience to understand logic of code.

Besides what Christoph told you (I agree with him, writing test suites
/ modules is quite good exercise for newbies) my 2 cents for below
code that you may consider in the future as a technique of cleaning
up,

> +static int getpos(struct btree_geo *geo, unsigned long *node,
> +               unsigned long *key)
> +{

> +       int i;

unsigned int i;

> +
> +       for (i = 0; i < geo->no_pairs; i++) {
> +               if (keycmp(geo, node, i, key) <= 0)

> +                       break;

Here you return directly
return i;

> +       }

> +       return i;

And here is the best to return negative error code instead, like

return -ENOENT;

> +}

>         for ( ; height > 1; height--) {
> -               for (i = 0; i < geo->no_pairs; i++)
> -                       if (keycmp(geo, node, i, key) <= 0)
> -                               break;

> +               i = getpos(geo, node, key);
>                 if (i == geo->no_pairs)
>                         return NULL;

Taking above into consideration you may do

        i = getpos(geo, node, key);
        if (i < 0)
          return NULL;

Rationale behind that:
1) getpos() return value may be directly used whenever we need return
code to return;
2) you hide implementation details in your helper function
(geo->no_pairs dereference).

Though here both of them kinda minors you may use such technique in
the future for more complex code.

-- 
With Best Regards,
Andy Shevchenko

      parent reply	other threads:[~2017-04-28 16:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-11  6:53 [PATCH v1]] lib/btree.c: optimise the code by previously getpos function Leno Hou
2017-04-11 16:20 ` Christoph Hellwig
     [not found]   ` <CAGQVrL_2EanSVKkMEP_fBaeWSwu=tZYSq79nzOy-wXQ9m1WOuQ@mail.gmail.com>
2017-04-12 17:32     ` Christoph Hellwig
2017-04-19 14:48       ` Leno Hou
2017-04-28 16:40 ` Andy Shevchenko [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='CAHp75VfX0u11mRieRAH=P_6xzbL2cKjbhKNopv-ia70duH1Cfg@mail.gmail.com' \
    --to=andy.shevchenko@gmail.com \
    --cc=lenohou@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).