public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Qu Wenruo <quwenruo.btrfs@gmx.com>
To: Nikolay Borisov <nborisov@suse.com>, Qu Wenruo <wqu@suse.com>,
	linux-btrfs@vger.kernel.org
Subject: Re: [PATCH v2 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
Date: Wed, 27 Oct 2021 15:41:23 +0800	[thread overview]
Message-ID: <06ccca88-72f7-be2e-4ba4-87ca947aa9be@gmx.com> (raw)
In-Reply-To: <62d6f59e-b4f0-fb47-54b2-bb6e5c5b744b@suse.com>



On 2021/10/27 14:37, Nikolay Borisov wrote:
>
>
> On 27.10.21 г. 8:28, Qu Wenruo wrote:
>> In function btrfs_bg_flags_to_raid_index(), we use quite some if () to
>> convert the BTRFS_BLOCK_GROUP_* bits to a index number.
>>
>> But the truth is, there is really no such need for so many branches at
>> all.
>> Since all BTRFS_BLOCK_GROUP_* flags are just one single bit set inside
>> BTRFS_BLOCK_GROUP_PROFILES_MASK, we can easily use ilog2() to calculate
>> their values.
>>
>> Only one fixed offset is needed to make the index sequential (the
>> lowest profile bit starts at ilog2(1 << 3) while we have 0 reserved for
>> SINGLE).
>>
>> Even with that calculation involved (one if(), one ilog2(), one minus),
>> it should still be way faster than the if () branches, and now it is
>> definitely small enough to be inlined.
>>
>
> Is this used in a performance critical path,

Not really in a hot path.

Most of them are called in a per block group/chunk base.

The only hotter path is in __btrfs_map_block() where if we need full
stripe, we will call btrfs_chunk_max_errors() which in turn call the
function.

That's the hottest path I can find, and even for that case it's just
per-bio base.

> are there any numbers which prove that it's indeed faster?

No real world bench for it.

But from x86_75 asm code, it's definitely smaller, with only one branch.

New:
btrfs_bg_flags_to_raid_index:
         xorl    %eax, %eax
         andl    $2040, %edi
         je      .L2499
         shrq    $2, %rdi
         movl    $-1, %eax
         bsrq %rdi,%rax
.L2499:
         ret

Old:
btrfs_bg_flags_to_raid_index:
         xorl    %eax, %eax
         testb   $64, %dil
         jne     .L429
         movl    $1, %eax
         testb   $16, %dil
         jne     .L429
         movl    $7, %eax
         testl   $512, %edi
         jne     .L429
         movl    $8, %eax
         testl   $1024, %edi
         jne     .L429
         movl    $2, %eax
         testb   $32, %dil
         jne     .L429
         movl    $3, %eax
         testb   $8, %dil
         jne     .L429
         movl    $5, %eax
         testb   $-128, %dil
         jne     .L429
         andl    $256, %edi
         cmpq    $1, %rdi
         sbbl    %eax, %eax
         andl    $-2, %eax
         addl    $6, %eax
.L429:
         ret

Which I don't really believe the older code can be any faster,
considering so many branches, and pure lines of asm.

Thanks,
Qu

  reply	other threads:[~2021-10-27  7:41 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-27  5:28 [PATCH v2 0/2] btrfs: re-define btrfs_raid_types Qu Wenruo
2021-10-27  5:28 ` [PATCH v2 1/2] btrfs: move definition of btrfs_raid_types to volumes.h Qu Wenruo
2021-10-27  5:28 ` [PATCH v2 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index() Qu Wenruo
2021-10-27  6:37   ` Nikolay Borisov
2021-10-27  7:41     ` Qu Wenruo [this message]
2021-10-27  9:23   ` Anand Jain
2021-10-27 10:41     ` Qu Wenruo
2021-10-28  1:04       ` Anand Jain
2021-10-28  7:10         ` Qu Wenruo
2021-10-28 21:53           ` Anand Jain
2021-10-29 14:11   ` David Sterba
2021-10-29 23:38     ` Qu Wenruo
2021-11-02 17:16       ` David Sterba

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=06ccca88-72f7-be2e-4ba4-87ca947aa9be@gmx.com \
    --to=quwenruo.btrfs@gmx.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=nborisov@suse.com \
    --cc=wqu@suse.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