Re: [PATCH v4 08/14] libfdt: Speed up fdt_find_string_len_() with memchr()
From: David Gibson
Date: Thu Nov 19 2026 - 00:40:03 EST
On Wed, Nov 18, 2026 at 10:15:38PM +0000, David Laight wrote:
> On Wed, 18 Nov 2026 12:39:59 -0800
> Doug Anderson <dianders@xxxxxxxxxxxx> wrote:
>
> > Hi David,
> >
> > On Wed, Nov 18, 2026 at 11:15 AM David Laight
> > <david.laight.linux@xxxxxxxxx> wrote:
> > >
> > > > 2. Every stored string ends with `'\0'`, whereas `s[0]` only appears in
> > > > a smaller fraction of bytes. On a real upstream Qualcomm base DTB's
> > > > `dt_strings` block (855 strings in 11,259 bytes, average 13.2 bytes
> > > > per string), `'\0'` occurs on 7.6% of bytes while `s[0]` averages
> > > > 3.9%. Replaying those 855 insertions makes 362k `memchr()` calls when
> > > > scanning for `'\0'` vs. 187k `memchr()` calls when scanning for
> > > > `s[0]`.
> > >
> > > You need to count the number of times that a letter that is the first
> > > character of a string you are going to lookup appears in the entire buffer.
> > > There might only be one 'j' in the entire buffer - but you never look for it.
> > > (But I might still be wrong)
> >
> > That 3.9% (`187,315` `memchr()` calls across ~4.77M scanned bytes) was
> > actually measured directly by replaying those 855 real lookups (`s[0]`
> > of each string being inserted), not an unweighted 1/26 average across
> > the alphabet. Even though common prefixes like `'p'`, `'u'`, `'q'`, and
> > `'s'` get looked up a lot, 26.6% of the `dt_strings` block (`2,139`
> > bytes of `'_'`, `'-'`, `'0'-'9'`, and `'#'`, plus `855` NUL bytes) are
> > characters that almost never appear as `s[0]`, which pulls the
> > lookup-weighted hit rate back down to 3.9%.
> >
> > Intuitively, `'\0'` hits once per string (`1 / L`), so `s[0]` wins as
> > long as average string length `L` stays below the ~22-26 byte crossover
> > -- and even on plain non-`-@` DTBs without `/__symbols__`, property
> > names average ~12-14 bytes (`'\0'` still made 1.6x-2.2x more `memchr()`
> > calls than `s[0]` across plain DTB `fdt_sw` builds, `/chosen` fixups,
> > and `.dtbo` overlay application).
> >
> > > Have you tried an open coded scans instead of calling memchr() and memcmp()?
> > > With an average of 13 bytes per string (26 for your memchr()) the code loop
> > > might be faster.
> > > But beware, gcc is likely to convert the code back to a call to memcmp()
> > > - it certainly does for strlen and memcpy.
> > >
> > > Oh - and if this code runs in kernel the timings will be significantly
> > > different to glibc - it uses some magic avx instructions for strlen()
> > > and memchr() is effectively the same.
> > > The kernel functions are likely to be out of line byte loops.
> >
> > Out of curiosity I timed all of those across 200 replays of the same
> > 855-string (11,259-byte) Qualcomm `dt_strings` block with `gcc 15.2` on
> > x86_64 (`gcc -O2` kept the open-coded loops inline and didn't turn them
> > back into `memcmp()`):
>
> Once you are doing timings, adding a few more doesn't take long.
>
> > * glibc `memchr` + `!p[slen]` + glibc `memcmp`:
> > 0.25 s (`-O0`), 0.23 s (`-Os`), 0.22 s (`-O2`)
> > * glibc `memchr` + `!p[slen]` + open-coded inner compare:
> > 0.23 s (`-O0`), 0.21 s (`-Os`), 0.21 s (`-O2`)
>
> So that one is a marginal win.
>
> > * Fully open-coded (`p[0] == s[0] && !p[slen]` + inner byte loop):
> > 0.91 s (`-O0`), 0.62 s (`-Os`), 0.40 s (`-O2`)
> > * Out-of-line byte-loop `memchr` + `!p[slen]` + byte-loop `memcmp`:
> > 0.86 s (`-O0`), 0.90 s (`-Os`), 0.50 s (`-O2`)
> > * Original per-byte out-of-line byte-loop `memcmp`:
> > 3.31 s (`-O0`), 1.87 s (`-Os`), 1.86 s (`-O2`)
> >
> > Even with a ~25-byte average stride, glibc's AVX2 `memchr()` handles
> > almost an entire stride in a single 32-byte vector load + compare, so it
> > beats the open-coded scalar loop by ~1.9x at `-O2` (and ~2.7x at `-Os`).
>
> Using avx2 is 'cheating' ...
> Someone at Intel realised that strlen() does matter and threw some silicon at it.
> They finally did the same for integer divide (but I think AMD did it first)
> and did a divide that generates move than one bit per clock.
>
> > Open-coding just the inner compare barely moves the needle since
> > `!p[slen]` already cut `memcmp()` down to 14.3k calls across the 855
> > insertions.
> >
> > On the kernel / bootloader side, `fdt_find_string_len_()` is only called
> > by `fdt_sw` and `fdt_rw` when adding new property names to `dt_strings`
> > -- normal read-only DTB parsing (`fdt_ro.c`) never calls it. And even
> > with a naive out-of-line byte-loop `memchr()`, `memchr()` + `!p[slen]` +
> > `memcmp()` is still 3.7x faster at `-O2` (`1.86 s -> 0.50 s`) and 2.1x
> > faster at `-Os` (`1.87 s -> 0.90 s`) than the old code, so keeping the
> > simpler `memchr()` + `!p[slen]` + `memcmp()` seems like the right
> > tradeoff.
>
> Seems reasonable.
Uh, calm down folks. I'm happy to take easy performance wins, but
this really isn't worth getting deep into the weeds on.
--
David Gibson (he or they) | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you, not the other way
| around.
http://www.ozlabs.org/~dgibson
Attachment:
signature.asc
Description: PGP signature