From: Matthew Wilcox <willy@infradead.org>
To: Denis Efremov <efremov@ispras.ru>
Cc: akpm@linux-foundation.org, Akinobu Mita <akinobu.mita@gmail.com>,
Jan Kara <jack@suse.cz>,
linux-kernel@vger.kernel.org, Matthew Wilcox <matthew@wil.cx>,
dm-devel@redhat.com, linux-fsdevel@vger.kernel.org,
linux-media@vger.kernel.org, Erdem Tumurov <erdemus@gmail.com>,
Vladimir Shelekhov <vshel@iis.nsk.su>
Subject: Re: [PATCH v2] lib/memweight.c: open codes bitmap_weight()
Date: Sat, 24 Aug 2019 23:11:58 -0700 [thread overview]
Message-ID: <20190825061158.GC28002@bombadil.infradead.org> (raw)
In-Reply-To: <20190824100102.1167-1-efremov@ispras.ru>
On Sat, Aug 24, 2019 at 01:01:02PM +0300, Denis Efremov wrote:
> This patch open codes the bitmap_weight() call. The direct
> invocation of hweight_long() allows to remove the BUG_ON and
> excessive "longs to bits, bits to longs" conversion.
Honestly, that's not the problem with this function. Take a look
at https://danluu.com/assembly-intrinsics/ for a _benchmarked_
set of problems with popcnt.
> BUG_ON was required to check that bitmap_weight() will return
> a correct value, i.e. the computed weight will fit the int type
> of the return value.
What? No. Look at the _arguments_ of bitmap_weight():
static __always_inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
> With this patch memweight() controls the
> computation directly with size_t type everywhere. Thus, the BUG_ON
> becomes unnecessary.
Why are you bothering? How are you allocating half a gigabyte of memory?
Why are you calling memweight() on half a gigabyte of memory?
> if (longs) {
> - BUG_ON(longs >= INT_MAX / BITS_PER_LONG);
> - ret += bitmap_weight((unsigned long *)bitmap,
> - longs * BITS_PER_LONG);
> + const unsigned long *bitmap_long =
> + (const unsigned long *)bitmap;
> +
> bytes -= longs * sizeof(long);
> - bitmap += longs * sizeof(long);
> + for (; longs > 0; longs--, bitmap_long++)
> + ret += hweight_long(*bitmap_long);
> + bitmap = (const unsigned char *)bitmap_long;
> }
If you really must change anything, I'd rather see this turned into a
loop:
while (longs) {
unsigned int nbits;
if (longs >= INT_MAX / BITS_PER_LONG)
nbits = INT_MAX + 1;
else
nbits = longs * BITS_PER_LONG;
ret += bitmap_weight((unsigned long *)bitmap, sz);
bytes -= nbits / 8;
bitmap += nbits / 8;
longs -= nbits / BITS_PER_LONG;
}
then we only have to use Dan Luu's optimisation in bitmap_weight()
and not in memweight() as well.
Also, why does the trailer do this:
for (; bytes > 0; bytes--, bitmap++)
ret += hweight8(*bitmap);
instead of calling hweight_long on *bitmap & mask?
next prev parent reply other threads:[~2019-08-25 6:12 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-08-21 7:42 [PATCH] lib/memweight.c: optimize by inlining bitmap_weight() Denis Efremov
2019-08-22 1:25 ` Andrew Morton
2019-08-22 7:30 ` Denis Efremov
2019-08-24 10:01 ` [PATCH v2] lib/memweight.c: open codes bitmap_weight() Denis Efremov
2019-08-25 6:11 ` Matthew Wilcox [this message]
2019-08-25 11:39 ` Denis Efremov
2019-08-26 18:39 ` Matthew Wilcox
2019-09-13 11:48 ` Denis Efremov
2019-09-13 13:41 ` efremov
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=20190825061158.GC28002@bombadil.infradead.org \
--to=willy@infradead.org \
--cc=akinobu.mita@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=dm-devel@redhat.com \
--cc=efremov@ispras.ru \
--cc=erdemus@gmail.com \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=matthew@wil.cx \
--cc=vshel@iis.nsk.su \
/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