From: "Morten Brørup" <mb@smartsharesystems.com>
To: "Mattias Rönnblom" <hofors@lysator.liu.se>,
"Stephen Hemminger" <stephen@networkplumber.org>,
dev@dpdk.org
Cc: "Mattias Rönnblom" <mattias.ronnblom@ericsson.com>,
"Yipeng Wang" <yipeng1.wang@intel.com>,
"Sameh Gobriel" <sameh.gobriel@intel.com>,
"Bruce Richardson" <bruce.richardson@intel.com>,
"Vladimir Medvedkin" <vladimir.medvedkin@intel.com>
Subject: RE: [RFC 3/3] hash: add support for common small key sizes
Date: Fri, 22 Aug 2025 20:57:25 +0200 [thread overview]
Message-ID: <98CBD80474FA8B44BF855DF32C47DC35E9FE63@smartserver.smartshare.dk> (raw)
In-Reply-To: <90ad6122-467c-441d-8515-c043146a8a57@lysator.liu.se>
> >>> +static inline int
> >>> +rte_hash_k2_cmp_eq(const void *key1, const void *key2, size_t key_len
> >> __rte_unused)
> >>> +{
> >>> + const uint16_t *k1 = key1;
> >>> + const uint16_t *k2 = key2;
> >>> +
> >>
> >> What we do now is to require the keys are 16-bit aligned (which wasn't
> >> the case before).
> >>
> >> You could
> >>
> >> uint16_t k1;
> >> memcpy(&k1, key1, sizeof(uint16_t));
> >> instead.
> >>
> >> Would generate the same code, but be safe from any future alignment issues.
> >
> > Or use the unaligned types, e.g.:
> > const unaligned_uint16_t *k1 = key1;
> > const unaligned_uint16_t *k2 = key2;
> >
>
> Could you explain why that is safe? Doesn't
> __attribute__((__aligned__(1)))
> just say specify the object doesn't have any alignment requirements,
> without asking the compiler to deal with it?
It is safe because the compiler does deal with it.
Here's a simple example:
https://godbolt.org/z/r39zdeEcx
I don't know how MSVC deals with it, but it doesn't support alignment sensitive architectures, so no real problem.
>
> >>
> >> Anyway, maybe it's safe to assume the keys are aligned, so this is not
> >> an issue.
> >
> > Lots of DPDK code ignores alignment issues.
> >
> >>
> >>> + return k1[0] ^ k2[0];
> >>> +}
> >>
> >> Haven't you implemented "neq" rather than "eq" here? If the keys are
> >> equal, the result is 0. Should be != 0.
> >
> > Not a bug.
>
> Well, the function body doesn't do what the function name tells it. :)
Agree. They really should be renamed to _cmp_neq.
And _cmp_eq wrappers could be kept for backwards API compatibility. Possibly marked deprecated.
>
> > These hash compare functions are in fact "neq", not "eq".
> > Having "_cmp_eq" in the function names is extremely unfortunate and
> misleading.
> >
> >>
> >> Would it be worth adding a comment like "use XOR to make this
> >> branch-free"? It may not be obvious to all readers.
> >>
> >> That said, I’m not sure this trick will actually change the generated
> >> object code - especially if the result of the eq function is still used
> >> in a conditional afterward. Anyway, keeping it seems like a good
> >> conservative approach.
> >
> > I wonder if any compiler is clever enough to use a different memcmp
> implementation if we inform the compiler at build time that we don't care if
> key1 is less than or greater key2, only if they differ or not.
>
> All what is needed is a constant-size length. (Only tested with the most
> recent GCC and clang.)
Yes. But that was not what I was thinking about... I was wondering if "memcmp()!=0" compiles to code that calls some other memcmp implementation that doesn't check which of the two strings is lower, but only tests if they differ.
>
> At least GCC will emit a cmp instruction though (so not branch free), if
> that matters.
>
> > If so, the OTHER_BYTES handler shouldn't call memcmp() directly, but a
> wrapper around it:
> >
> > rte_hash_k_cmp_eq(const void *key1, const void *key2, size_t key_len
> __rte_unused)
> > {
> > return memcmp(key1, key2, key_len) != 0;
> > }
> >
>
> Should work. (Remove __rte_unused.)
I just tested it in Compiler Explorer, and the function calls the same memcmp(), regardless of the !=0.
So no benefit compared to calling memcmp() directly from the jump table.
next prev parent reply other threads:[~2025-08-22 18:57 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-21 20:35 [RFC 0/3] hash: optimize compare logic Stephen Hemminger
2025-08-21 20:35 ` [RFC 1/3] hash: move table of hash compare functions out of header Stephen Hemminger
2025-08-22 9:05 ` Morten Brørup
2025-08-22 16:50 ` Stephen Hemminger
2025-08-21 20:35 ` [RFC 2/3] hash: reduce architecture special cases Stephen Hemminger
2025-08-22 9:20 ` Morten Brørup
2025-08-21 20:35 ` [RFC 3/3] hash: add support for common small key sizes Stephen Hemminger
2025-08-22 7:19 ` Mattias Rönnblom
2025-08-22 9:50 ` Morten Brørup
2025-08-22 15:05 ` Mattias Rönnblom
2025-08-22 18:57 ` Morten Brørup [this message]
2025-08-22 16:12 ` Stephen Hemminger
2025-08-22 18:19 ` [PATCH v2 0/4] Cuckoo hash cleanup and optimizations Stephen Hemminger
2025-08-22 18:19 ` [PATCH v2 1/4] hash: move table of hash compare functions out of header Stephen Hemminger
2025-08-22 18:19 ` [PATCH v2 2/4] hash: use static_assert Stephen Hemminger
2025-08-22 18:19 ` [PATCH v2 3/4] hash: reduce architecture special cases Stephen Hemminger
2025-08-22 18:19 ` [PATCH v2 4/4] hash: add support for common small key sizes Stephen Hemminger
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=98CBD80474FA8B44BF855DF32C47DC35E9FE63@smartserver.smartshare.dk \
--to=mb@smartsharesystems.com \
--cc=bruce.richardson@intel.com \
--cc=dev@dpdk.org \
--cc=hofors@lysator.liu.se \
--cc=mattias.ronnblom@ericsson.com \
--cc=sameh.gobriel@intel.com \
--cc=stephen@networkplumber.org \
--cc=vladimir.medvedkin@intel.com \
--cc=yipeng1.wang@intel.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;
as well as URLs for NNTP newsgroup(s).