mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: Kent Overstreet <kent.overstreet@linux.dev>
Cc: linux-kernel@vger.kernel.org, Theodore Ts'o <tytso@mit.edu>,
	"Jason A. Donenfeld" <Jason@zx2c4.com>
Subject: Re: [PATCH] random: get_random_u64_below()
Date: Sat, 15 Mar 2025 20:55:32 +0000	[thread overview]
Message-ID: <20250315205532.6815f2c5@pumpkin> (raw)
In-Reply-To: <olt5s7scbosagj4fpq25xun4kdvi6puwqdocgsvhpzguvpt7jt@dyy37p4uturh>

On Sat, 15 Mar 2025 14:20:46 -0400
Kent Overstreet <kent.overstreet@linux.dev> wrote:

> On Sat, Mar 15, 2025 at 01:52:34PM +0000, David Laight wrote:
> > On Thu, 13 Mar 2025 12:38:10 -0400
> > Kent Overstreet <kent.overstreet@linux.dev> wrote:
> >   
> > > bcachefs needs this, for sampling devices to read from based on squared
> > > device latencies.
> > > 
> > > this uses the same algorithm as get_random_u32_below: since the multiply
> > > uses the top and bottom halves separately, it works out fairly well.  
> > 
> > Adding two separate copies of much the same code is silly.
> > Given what the code is doing, does it ever make any sense to inline it.
> > 
> > Inlining the original get_random_u32_below(ceil) that did
> > 	(random_u32() * ((1ull << 32) / ceil) >> 32
> > (for constant ceil) made sense.
> > While good enough for most purposes it was replaced by the much more
> > expensive function that guarantees that all the output values are
> > equally likely - rather than just evenly distributed.  
> 
> Expensive!? It adds a multiply.

I make it two multiplies and a loop.
Have you looked at what happens on 32bit systems?

> 
> That % gets constant folded, in the inlined case, and in the non-inline
> case it's hit only a small fraction of the, time, for typical ceil.

If the % is only a small fraction on the cost for the non-inline case
(and it is over 100 clocks on many cpu that people still use) then
why inline anything?
A quick look shows divide being 'only moderately slow' on zen3 and coffee lake.

What you might want to do is pass -ceil % ceil through to a real function
(especially if constant).

Oh I guess you haven't actually tested the version you submitted.
Time to play 'spot the silly error'.

	David
 


  reply	other threads:[~2025-03-15 20:55 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-13 16:38 Kent Overstreet
2025-03-15 13:52 ` David Laight
2025-03-15 13:58   ` David Laight
2025-03-15 18:20   ` Kent Overstreet
2025-03-15 20:55     ` David Laight [this message]
2025-03-15 21:32       ` Kent Overstreet
2025-03-16 13:35         ` David Laight

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=20250315205532.6815f2c5@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=Jason@zx2c4.com \
    --cc=kent.overstreet@linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tytso@mit.edu \
    /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

all inboxes | Powered by JetHome®