Re: [PATCH] futex: Optimise the size check get_futex_key()

Peter Zijlstra <[email protected]>
Newsgroups org.kernel.vger.linux-arch,org.infradead.lists.linux-arm-kernel,org.infradead.lists.linux-riscv,org.kernel.vger.linux-kernel,org.kernel.vger.linux-s390
Message-ID <[email protected]>
On Wed, Jul 01, 2026 at 06:17:36PM +0200, Sebastian Andrzej Siewior wrote:
> The futex address must be naturally aligned and this is checked via
> "address % size" where `address' is the supplied address and `size' is
> the expected size of futex. It is guaranteed that `size' is power of two
> but the compiler does not see it and creates here a `div' operation
> (x86, arm, gcc-15).

That's pretty stupid, since size = futex_size() := 1 << (blah); so it
has every opportunity to actually see this. Also, see below, clang does!

> We can take advantage of the pow2 property and rewrite it as
> "address & (size-1)".
> 
> As per testing, the command
> |perf bench futex hash -f 1 -b 16384 -t 1 -r 30
> 
> improved from
> | [thread  0] futex: 0x5619f931f740 [ 7001583 ops/sec ]
> to
> | [thread  0] futex: 0x55da173e5740 [ 7376137 ops/sec ]
> 
> or by 5.3%
> 
> Signed-off-by: Sebastian Andrzej Siewior <[email protected]>
> ---
> 
> Could someone verify this, please? The 5% look a bit high. This is on
> top of the series (but not worsen by the series).

Bah, I tried to reproduce and couldn't. Then I noticed I did a clang
build and that is in fact clever enough to do this optimization itself.

/me tries again with a GCC build.

pre:  [thread  0] futex: 0x561f14430680 [ 9021408 ops/sec ]
post: [thread  0] futex: 0x55feadbbb680 [ 8977527 ops/sec ]

(and this seems to be well inside the error threshold of this test).

So I see the GCC build do the DIV, and no longer with his patch applied,
but for some reason I cannot get the runtime performance to actually
improve much of anything on my system.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.