public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Noah Goldstein <goldstein.w.n@gmail.com>
To: Alexander Monakov <amonakov@ispras.ru>
Cc: GNU C Library <libc-alpha@sourceware.org>
Subject: Re: [PATCH v3 6/6] elf: Optimize _dl_new_hash in dl-new-hash.h
Date: Thu, 5 May 2022 13:03:45 -0500	[thread overview]
Message-ID: <CAFUsyfJqs3seMX0VVHKxoCaP68eobBP1XV6YhKG4rMRWjUPh3g@mail.gmail.com> (raw)
In-Reply-To: <21d761a-a6ef-e9a2-1a38-a592503c81db@ispras.ru>

On Thu, May 5, 2022 at 10:26 AM Alexander Monakov <amonakov@ispras.ru> wrote:
>
> On Thu, 5 May 2022, Noah Goldstein wrote:
>
> > > The following variant of the original loop avoids those issues and
> > > should run close to 2 cycles per iteration on most CPUs:
> > >
> > > static uint32_t
> > > _dl_new_hash (const char *s)
> > > {
> > >   uint32_t h = 5381, c;
> > >   const unsigned char *us = (const void *)s;
> > >   while ((c = *us++))
> > >     {
> > >       c += h;
> > >       asm("" : "+r"(h) : "r"(c));
> > >       h = h * 32 + c;
> > >     }
> > >   return h;
> > > }
> > >
> >
> > I'm not sure what you are getting at with the asm(). It seems the
> > produce the exact
> > same assembly:
> > https://godbolt.org/z/93qGMaTTE
>
> They are definitely not the same even via your link. Loop body of dl_new_hash0:

Oh wow. Must have 'looked at it' before it reloaded.
I'm sorry!
>
> .L3:
> (a)     addl    %eax, %edx
>         addq    $1, %rcx
> (b)     sall    $5, %eax
> (c)     addl    %edx, %eax
>         movzbl  -1(%rcx), %edx
>         testl   %edx, %edx
>         jne     .L3
>
> and of dl_new_hash1:
>
> .L9:
> (A)     movl    %r8d, %eax
>         addq    $1, %rcx
> (B)     sall    $5, %eax
> (C)     addl    %edx, %eax
>         movzbl  -1(%rcx), %edx
> (D)     addl    %eax, %r8d
>         testl   %edx, %edx
>         jne     .L9
>
> (in fact even the instruction count is not the same, 7 vs 8)
>
> In the first loop, (a) and (b) are independent, (c) depends on them both, and
> on the next iteration (a) and (b) take the result of (c) from the previous. Thus
> the dependencies are 2 cycles for one iteration.
>
> In the second loop, (B) depends on (A), (C) depends on (B), (D) depends on (C),
> and on the next iteration (A) depends on (D) from the previous. Thus four
> instructions form a dependency chain of 3 or 4 cycles depending if move
> elimination happens or not.
>
> In any case, if you benchmark them both you should see the difference.
>
Okay that makes sense and indeed results in a substantial improvement.

Totally happy with going with your version.

Think there is still some benefit to the unrolled version because

1) It's less eager about hitting the LSD on newer processors
(but that's really only an issue for strings > ~24 characters).

2) It bottlenecks less hard on `p6` because the `imul` goes `p0`
and the branches are distributed between `p0` and `p6` instead of
always on `p6`.

3) It still saves a few uops (although imul vs `add + shl` isn't really
a meaningful save).

Either way it will be an improvement.

Little benchmark: https://godbolt.org/z/G6PvW4eTr

Generally see hash2 winning the most.

> Alexander

  reply	other threads:[~2022-05-05 18:04 UTC|newest]

Thread overview: 167+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-14  4:12 [PATCH v1 1/6] elf: Refactor dl_new_hash so it can be tested / benchmarked Noah Goldstein
2022-04-14  4:12 ` [PATCH v1 2/6] elf: Add tests for the hash functions in dl-hash.h Noah Goldstein
2022-04-14  4:12 ` [PATCH v1 3/6] nss: Add tests for the nss_hash in nss_hash.h Noah Goldstein
2022-04-14  4:12 ` [PATCH v1 4/6] benchtests: Add benchtests for dl_elf_hash, dl_new_hash and nss_hash Noah Goldstein
2022-04-14  4:12 ` [PATCH v1 5/6] nss: Optimize nss_hash in nss_hash.c Noah Goldstein
2022-04-14  4:12 ` [PATCH v1 6/6] elf: Optimize __dl_new_hash in dl-hash.h Noah Goldstein
2022-04-14  4:32 ` [PATCH v1 1/6] elf: Refactor dl_new_hash so it can be tested / benchmarked H.J. Lu
2022-04-14 14:56   ` Noah Goldstein
2022-04-14 14:55 ` [PATCH v2 " Noah Goldstein
2022-04-14 14:55   ` [PATCH v2 2/6] elf: Add tests for the dl hash funcs (_dl_new_hash and _dl_elf_hash) Noah Goldstein
2022-04-25 15:39     ` Florian Weimer
2022-04-25 15:59       ` Noah Goldstein
2022-04-14 14:55   ` [PATCH v2 3/6] nss: Add tests for the nss_hash in nss_hash.h Noah Goldstein
2022-04-25 15:38     ` Florian Weimer
2022-04-25 15:58       ` Noah Goldstein
2022-04-26  8:35         ` Florian Weimer
2022-04-26 21:39           ` Noah Goldstein
2022-04-27 10:48             ` Florian Weimer
2022-04-27 15:02               ` Noah Goldstein
2022-04-14 14:55   ` [PATCH v2 4/6] benchtests: Add benchtests for dl_elf_hash, dl_new_hash and nss_hash Noah Goldstein
2022-04-14 14:55   ` [PATCH v2 5/6] nss: Optimize nss_hash in nss_hash.c Noah Goldstein
2022-04-14 14:55   ` [PATCH v2 6/6] elf: Optimize _dl_new_hash in dl-new-hash.h Noah Goldstein
2022-04-25 15:58 ` [PATCH v3 1/6] elf: Refactor dl_new_hash so it can be tested / benchmarked Noah Goldstein
2022-04-25 15:58   ` [PATCH v3 2/6] elf: Add tests for the dl hash funcs (_dl_new_hash and _dl_elf_hash) Noah Goldstein
2022-04-25 15:58   ` [PATCH v3 3/6] nss: Add tests for the nss_hash in nss_hash.h Noah Goldstein
2022-04-25 15:58   ` [PATCH v3 4/6] benchtests: Add benchtests for dl_elf_hash, dl_new_hash and nss_hash Noah Goldstein
2022-04-25 15:58   ` [PATCH v3 5/6] nss: Optimize nss_hash in nss_hash.c Noah Goldstein
2022-04-25 15:58   ` [PATCH v3 6/6] elf: Optimize _dl_new_hash in dl-new-hash.h Noah Goldstein
2022-04-25 16:01   ` [PATCH v3 1/6] elf: Refactor dl_new_hash so it can be tested / benchmarked Adhemerval Zanella
2022-04-25 16:18     ` Noah Goldstein
2022-04-25 15:59 ` [PATCH v1 " Adhemerval Zanella
2022-04-25 16:16   ` Noah Goldstein
2022-04-25 16:35 ` [PATCH v3 " Noah Goldstein
2022-04-25 16:35   ` [PATCH v3 2/6] elf: Add tests for the dl hash funcs (_dl_new_hash and _dl_elf_hash) Noah Goldstein
2022-04-25 16:35   ` [PATCH v3 3/6] nss: Add tests for the nss_hash in nss_hash.h Noah Goldstein
2022-04-27 10:39     ` Florian Weimer
2022-04-27 16:24       ` Noah Goldstein
2022-04-25 16:35   ` [PATCH v3 4/6] benchtests: Add benchtests for dl_elf_hash, dl_new_hash and nss_hash Noah Goldstein
2022-04-25 16:36   ` [PATCH v3 5/6] nss: Optimize nss_hash in nss_hash.c Noah Goldstein
2022-04-27 10:47     ` Florian Weimer
2022-04-25 16:36   ` [PATCH v3 6/6] elf: Optimize _dl_new_hash in dl-new-hash.h Noah Goldstein
2022-04-27 10:43     ` Florian Weimer
2022-04-27 16:25       ` Noah Goldstein
2022-04-27 15:02     ` Alexander Monakov
     [not found]       ` <CAFUsyfKeocq4VAusvnggq-NR=tOQTjrD0Z6r3CYCTjGQ=tGGSw@mail.gmail.com>
     [not found]         ` <f54f1ec9-fc31-283f-bce9-59fd8bda98ad@ispras.ru>
2022-04-27 16:23           ` Noah Goldstein
2022-04-28 18:03             ` Alexander Monakov
2022-05-04 18:04               ` Alexander Monakov
2022-05-05 11:07                 ` Alexander Monakov
2022-05-05 15:10                   ` Noah Goldstein
2022-05-05 15:26                     ` Alexander Monakov
2022-05-05 18:03                       ` Noah Goldstein [this message]
2022-05-05 19:37                         ` Alexander Monakov
2022-05-05 22:51                           ` Noah Goldstein
2022-04-27 16:19 ` [PATCH v4 1/6] elf: Refactor dl_new_hash so it can be tested / benchmarked Noah Goldstein
2022-04-27 16:19   ` [PATCH v4 2/6] elf: Add tests for the dl hash funcs (_dl_new_hash and _dl_elf_hash) Noah Goldstein
2022-04-27 16:19   ` [PATCH v4 3/6] nss: Add tests for the nss_hash in nss_hash.h Noah Goldstein
2022-04-27 16:20   ` [PATCH v4 4/6] benchtests: Add benchtests for dl_elf_hash, dl_new_hash and nss_hash Noah Goldstein
2022-04-27 16:20   ` [PATCH v4 5/6] nss: Optimize nss_hash in nss_hash.c Noah Goldstein
2022-04-27 16:20   ` [PATCH v4 6/6] elf: Optimize _dl_new_hash in dl-new-hash.h Noah Goldstein
2022-05-09 17:17 ` [PATCH v5 1/6] elf: Refactor dl_new_hash so it can be tested / benchmarked Noah Goldstein
2022-05-09 17:17   ` [PATCH v5 2/6] elf: Add tests for the dl hash funcs (_dl_new_hash and _dl_elf_hash) Noah Goldstein
2022-05-09 17:17   ` [PATCH v5 3/6] nss: Add tests for the nss_hash in nss_hash.h Noah Goldstein
2022-05-09 17:17   ` [PATCH v5 4/6] benchtests: Add benchtests for dl_elf_hash, dl_new_hash and nss_hash Noah Goldstein
2022-05-09 17:17   ` [PATCH v5 5/6] nss: Optimize nss_hash in nss_hash.c Noah Goldstein
2022-05-09 17:17   ` [PATCH v5 6/6] elf: Optimize _dl_new_hash in dl-new-hash.h Noah Goldstein
2022-05-10 11:58     ` Adhemerval Zanella
2022-05-10 15:04 ` [PATCH v6 1/6] elf: Refactor dl_new_hash so it can be tested / benchmarked Noah Goldstein
2022-05-10 15:04   ` [PATCH v6 2/6] elf: Add tests for the dl hash funcs (_dl_new_hash and _dl_elf_hash) Noah Goldstein
2022-05-10 15:04   ` [PATCH v6 3/6] nss: Add tests for the nss_hash in nss_hash.h Noah Goldstein
2022-05-10 15:04   ` [PATCH v6 4/6] benchtests: Add benchtests for dl_elf_hash, dl_new_hash and nss_hash Noah Goldstein
2022-05-10 15:04   ` [PATCH v6 5/6] nss: Optimize nss_hash in nss_hash.c Noah Goldstein
2022-05-10 15:04   ` [PATCH v6 6/6] elf: Optimize _dl_new_hash in dl-new-hash.h Noah Goldstein
2022-05-10 15:29     ` H.J. Lu
2022-05-10 15:31       ` H.J. Lu
2022-05-10 16:49     ` Alexander Monakov
2022-05-10 17:17       ` Noah Goldstein
2022-05-10 17:40         ` Alexander Monakov
2022-05-10 23:30 ` [PATCH v7 1/6] elf: Refactor dl_new_hash so it can be tested / benchmarked Noah Goldstein
2022-05-10 23:30   ` [PATCH v7 2/6] elf: Add tests for the dl hash funcs (_dl_new_hash and _dl_elf_hash) Noah Goldstein
2022-05-10 23:30   ` [PATCH v7 3/6] nss: Add tests for the nss_hash in nss_hash.h Noah Goldstein
2022-05-10 23:30   ` [PATCH v7 4/6] benchtests: Add benchtests for dl_elf_hash, dl_new_hash and nss_hash Noah Goldstein
2022-05-10 23:30   ` [PATCH v7 5/6] nss: Optimize nss_hash in nss_hash.c Noah Goldstein
2022-05-10 23:30   ` [PATCH v7 6/6] elf: Optimize _dl_new_hash in dl-new-hash.h Noah Goldstein
2022-05-10 23:46     ` H.J. Lu
2022-05-11  3:07       ` Noah Goldstein
2022-05-11  3:06 ` [PATCH v8 1/6] elf: Refactor dl_new_hash so it can be tested / benchmarked Noah Goldstein
2022-05-11  3:06   ` [PATCH v8 2/6] elf: Add tests for the dl hash funcs (_dl_new_hash and _dl_elf_hash) Noah Goldstein
2022-05-11  3:06   ` [PATCH v8 3/6] nss: Add tests for the nss_hash in nss_hash.h Noah Goldstein
2022-05-11  3:06   ` [PATCH v8 4/6] benchtests: Add benchtests for dl_elf_hash, dl_new_hash and nss_hash Noah Goldstein
2022-05-11  3:06   ` [PATCH v8 5/6] nss: Optimize nss_hash in nss_hash.c Noah Goldstein
2022-05-11  3:06   ` [PATCH v8 6/6] elf: Optimize _dl_new_hash in dl-new-hash.h Noah Goldstein
2022-05-16 14:12     ` Siddhesh Poyarekar
2022-05-16 14:31       ` Alexander Monakov
2022-05-16 16:23         ` Siddhesh Poyarekar
2022-05-16 16:38           ` Noah Goldstein
2022-05-16 16:44             ` Siddhesh Poyarekar
2022-05-16 20:32               ` Noah Goldstein
2022-05-16 18:09       ` Alexander Monakov
2022-05-16 18:47         ` Siddhesh Poyarekar
2022-05-16 19:28           ` Alexander Monakov
2022-05-16 19:35             ` Noah Goldstein
2022-05-16 19:41               ` Alexander Monakov
2022-05-16 19:47                 ` Adhemerval Zanella
2022-05-16 20:00                   ` Alexander Monakov
2022-05-16 20:08                     ` Adhemerval Zanella
2022-05-16 20:27                       ` Alexander Monakov
2022-05-16 19:48                 ` Noah Goldstein
2022-05-16 20:33                   ` Alexander Monakov
2022-05-16 21:40                     ` Noah Goldstein
2022-05-17  1:45             ` Siddhesh Poyarekar
2022-05-16 13:56   ` [PATCH v8 1/6] elf: Refactor dl_new_hash so it can be tested / benchmarked Siddhesh Poyarekar
2022-05-16 20:31     ` Noah Goldstein
2022-05-16 20:29 ` [PATCH v9 " Noah Goldstein
2022-05-16 20:30   ` [PATCH v9 2/6] elf: Add tests for the dl hash funcs (_dl_new_hash and _dl_elf_hash) Noah Goldstein
2022-05-17  4:19     ` Siddhesh Poyarekar
2022-05-18 17:29       ` Noah Goldstein
2022-05-16 20:30   ` [PATCH v9 3/6] nss: Add tests for the nss_hash in nss_hash.h Noah Goldstein
2022-05-17  4:32     ` Siddhesh Poyarekar
2022-05-18 17:30       ` Noah Goldstein
2022-05-16 20:30   ` [PATCH v9 4/6] benchtests: Add benchtests for dl_elf_hash, dl_new_hash and nss_hash Noah Goldstein
2022-05-17  4:52     ` Siddhesh Poyarekar
2022-05-18 17:33       ` Noah Goldstein
2022-05-16 20:30   ` [PATCH v9 5/6] nss: Optimize nss_hash in nss_hash.c Noah Goldstein
2022-05-17  5:11     ` Siddhesh Poyarekar
2022-05-18 17:34       ` Noah Goldstein
2022-05-18 17:35         ` Noah Goldstein
2022-05-16 20:30   ` [PATCH v9 6/6] elf: Optimize _dl_new_hash in dl-new-hash.h Noah Goldstein
2022-05-17  5:12     ` Siddhesh Poyarekar
2022-05-18 17:38       ` Noah Goldstein
2022-05-19 15:59         ` Siddhesh Poyarekar
2022-05-19 16:54           ` DJ Delorie
2022-05-17  3:34   ` [PATCH v9 1/6] elf: Refactor dl_new_hash so it can be tested / benchmarked Siddhesh Poyarekar
2022-05-18 17:28     ` Noah Goldstein
2022-05-18 17:26 ` [PATCH v10 " Noah Goldstein
2022-05-18 17:26   ` [PATCH v10 2/6] elf: Add tests for the dl hash funcs (_dl_new_hash and _dl_elf_hash) Noah Goldstein
2022-05-19 14:49     ` Siddhesh Poyarekar
2022-05-18 17:26   ` [PATCH v10 3/6] nss: Add tests for the nss_hash in nss_hash.h Noah Goldstein
2022-05-19 15:09     ` Siddhesh Poyarekar
2022-05-19 15:40       ` Siddhesh Poyarekar
2022-05-19 22:20         ` Noah Goldstein
2022-05-18 17:26   ` [PATCH v10 4/6] benchtests: Add benchtests for dl_elf_hash, dl_new_hash and nss_hash Noah Goldstein
2022-05-19 15:34     ` Siddhesh Poyarekar
2022-05-19 22:20       ` Noah Goldstein
2022-05-18 17:26   ` [PATCH v10 5/6] nss: Optimize nss_hash in nss_hash.c Noah Goldstein
2022-05-19 15:41     ` Siddhesh Poyarekar
2022-05-19 22:21       ` Noah Goldstein
2022-05-18 17:26   ` [PATCH v10 6/6] elf: Optimize _dl_new_hash in dl-new-hash.h Noah Goldstein
2022-05-18 17:32     ` H.J. Lu
2022-05-18 17:39       ` Noah Goldstein
2022-05-19  7:53       ` Siddhesh Poyarekar
2022-05-19 15:55     ` Siddhesh Poyarekar
2022-05-19 22:22       ` Noah Goldstein
2022-05-19 14:47   ` [PATCH v10 1/6] elf: Refactor dl_new_hash so it can be tested / benchmarked Siddhesh Poyarekar
2022-05-19 14:50     ` Noah Goldstein
2022-05-19 14:56       ` Siddhesh Poyarekar
2022-05-19 22:17 ` [PATCH v11 " Noah Goldstein
2022-05-19 22:17   ` [PATCH v11 2/6] elf: Add tests for the dl hash funcs (_dl_new_hash and _dl_elf_hash) Noah Goldstein
2022-05-19 22:19     ` Noah Goldstein
2022-05-19 22:18   ` [PATCH v11 3/6] nss: Add tests for the nss_hash in nss_hash.h Noah Goldstein
2022-05-23  7:42     ` Siddhesh Poyarekar
2022-05-19 22:18   ` [PATCH v11 4/6] benchtests: Add benchtests for dl_elf_hash, dl_new_hash and nss_hash Noah Goldstein
2022-05-23  7:44     ` Siddhesh Poyarekar
2022-05-19 22:18   ` [PATCH v11 5/6] nss: Optimize nss_hash in nss_hash.c Noah Goldstein
2022-05-23  7:44     ` Siddhesh Poyarekar
2022-05-19 22:18   ` [PATCH v11 6/6] elf: Optimize _dl_new_hash in dl-new-hash.h Noah Goldstein
2022-05-23  7:46     ` Siddhesh Poyarekar
2022-05-19 22:18   ` [PATCH v11 1/6] elf: Refactor dl_new_hash so it can be tested / benchmarked Noah Goldstein

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=CAFUsyfJqs3seMX0VVHKxoCaP68eobBP1XV6YhKG4rMRWjUPh3g@mail.gmail.com \
    --to=goldstein.w.n@gmail.com \
    --cc=amonakov@ispras.ru \
    --cc=libc-alpha@sourceware.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 read-only IMAP folder(s) and NNTP newsgroup(s).