unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>,
	Noah Goldstein <goldstein.w.n@gmail.com>
Cc: 'GNU C Library' <libc-alpha@sourceware.org>
Subject: [PATCH v4 0/2] Improve wcsstr
Date: Tue, 19 Mar 2024 15:07:37 +0000	[thread overview]
Message-ID: <PAWPR08MB89822B9B39B098787C232D66832C2@PAWPR08MB8982.eurprd08.prod.outlook.com> (raw)

Hi Adhemerval,

> With this fix, and with the removal of the powerpc strcasestr
> optimization [2], it seems that only x86_64 still provides a non
> O(m*n) implementation [3].  Noah already gave a +1, so it would be
> good to have some confirmation that this implementation can really
> show some quadradic behaviour before propose a removal.

Yes it is a simple brute-force algorithm that checks the whole needle
at a matching character pair (and does so 1 byte at a time after the
first 64 bytes of a needle). Also it never skips ahead and thus can match
at every haystack position after trying to match all of the needle.

I added a quick test for this (every different implementation requires
a unique test for its worst-case), and I got:

  "ifuncs": ["basic_strstr", "twoway_strstr", "__strstr_avx512", "__strstr_sse2_unaligned", "__strstr_generic"],

    {
     "len_haystack": 65536,
     "len_needle": 1024,
     "align_haystack": 0,
     "align_needle": 0,
     "fail": 1,
     "desc": "Difficult bruteforce needle",
     "timings": [4.0948e+07, 15094.5, 3.20818e+07, 108558, 10839.2]
    },
    {
     "len_haystack": 1048576,
     "len_needle": 4096,
     "align_haystack": 0,
     "align_needle": 0,
     "fail": 1,
     "desc": "Difficult bruteforce needle",
     "timings": [2.69767e+09, 100797, 2.08535e+09, 495706, 82666.9]
    }

So a 4x larger needle and 16x larger haystack gives a clear 65x slowdown on
both basic_strstr and __strstr_avx512.

Cheers,
Wilco

             reply	other threads:[~2024-03-19 15:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-19 15:07 Wilco Dijkstra [this message]
  -- strict thread matches above, loose matches on Subject: below --
2024-03-19 13:15 [PATCH v4 0/2] Improve wcsstr Adhemerval Zanella

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

  List information: https://www.gnu.org/software/libc/involved.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=PAWPR08MB89822B9B39B098787C232D66832C2@PAWPR08MB8982.eurprd08.prod.outlook.com \
    --to=wilco.dijkstra@arm.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=goldstein.w.n@gmail.com \
    --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).