List for cgit developers and users
 help / color / mirror / Atom feed
From: Markus Mayer <code@mmayer.net>
To: CGIT List <cgit@lists.zx2c4.com>
Cc: Glenn Strauss <gstrauss@gluelogic.com>
Subject: Re: [PATCH 1/1] global: provide memrchr implementation for macOS
Date: Sun, 29 Jan 2023 13:43:29 -0800	[thread overview]
Message-ID: <CANEuBv60Pvet=dqiTPS1a8r6bCsUaTj380jGgYAzHxH1i8XiSw@mail.gmail.com> (raw)
In-Reply-To: <Y9R3X7bhwUAWofJ/@xps13>

On Fri, 27 Jan 2023 at 17:16, Glenn Strauss <gstrauss@gluelogic.com> wrote:
>
> Fun!  A small exercise for comparison if you like.
> Cheers, Glenn
>
> void *
> memrchr(const void *s, int c, size_t n)
> {
>     const unsigned char *cp = (const unsigned char *)s + n;
>     const unsigned char ch = (unsigned char)c;
>     while (s != cp) {
>         if (*(--cp) == ch)
>             return (void *)cp;
>     }
>     return NULL;
> }

Hi all,

As promised, I played around a bit. I ran a few experiments with
different memrchr() implementations. Everything I did can be found
here:

https://github.com/mmayer/cgit/tree/memrchr-compare

The test-specific code is in the memrchr_test folder[1] within that repo.

The four implementations I tried are:

memrchr: the original implementation (from Apple's sudo command) that
I submitted as v1
memrchr2: Alejandro's suggestion
memrchr3: Glen's suggestion
memrchr4: for added fun, musl-libc's implementation[2]

I also checked the object and assembly files into the repo, so it's
easier to look at them if anybody wants to. They live in the
memrchr_test/output folder.

Here are the results for ARM and x86, both in assembly/object size and runtime.

ARM

# Object size of memrchr and memrchr2 is the same
-rw-r--r--  1 mmayer  staff  552 29 Jan 09:52 memrchr.o
-rw-r--r--  1 mmayer  staff  552 29 Jan 09:52 memrchr2.o
-rw-r--r--  1 mmayer  staff  544 29 Jan 09:52 memrchr3.o
-rw-r--r--  1 mmayer  staff  544 29 Jan 09:52 memrchr4.o

# Assembly source of memrchr2 is larger than memrchr
-rw-r--r--  1 mmayer  staff  694 29 Jan 09:52 memrchr2.s
-rw-r--r--  1 mmayer  staff  691 29 Jan 09:52 memrchr.s
-rw-r--r--  1 mmayer  staff  655 29 Jan 09:52 memrchr3.s
-rw-r--r--  1 mmayer  staff  655 29 Jan 09:52 memrchr4.s

execution time: 18.61453 seconds
execution time: 15.39163 seconds
execution time: 13.56957 seconds
execution time: 13.55493 seconds

x86

-rw-r--r--  1 mmayer  staff  656 29 Jan 10:02 memrchr.o
-rw-r--r--  1 mmayer  staff  656 29 Jan 10:02 memrchr2.o
-rw-r--r--  1 mmayer  staff  656 29 Jan 10:02 memrchr3.o
-rw-r--r--  1 mmayer  staff  648 29 Jan 10:02 memrchr4.o

-rw-r--r--  1 mmayer  staff  835 29 Jan 10:02 memrchr.s
-rw-r--r--  1 mmayer  staff  835 29 Jan 10:02 memrchr2.s
-rw-r--r--  1 mmayer  staff  825 29 Jan 10:02 memrchr3.s
-rw-r--r--  1 mmayer  staff  818 29 Jan 10:02 memrchr4.s

execution time: 20.29937 seconds
execution time: 23.67755 seconds
execution time: 12.59514 seconds
execution time: 11.38668 seconds

As you can see, musl-libc provides the smallest implementation that is
also the fastest. This is true for ARM and x86. So, I guess it makes
the most sense to pick that (memrchr4.c in my experiments). The code
is under a MIT license, which I assume is fine for CGIT.

What does everybody think?

Regards,
-Markus

[1] https://github.com/mmayer/cgit/tree/memrchr-compare/memrchr_test
[2] https://git.musl-libc.org/cgit/musl/tree/src/string/memrchr.c

      parent reply	other threads:[~2023-01-29 21:44 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-27 23:15 [PATCH 0/1] cgit " Markus Mayer
2023-01-27 23:15 ` [PATCH 1/1] global: provide memrchr implementation " Markus Mayer
     [not found]   ` <fe46d404-8659-7260-63c1-38bbc9c1e5ad@gmail.com>
2023-01-28  0:35     ` Markus Mayer
     [not found]       ` <Y9R3X7bhwUAWofJ/@xps13>
2023-01-29 21:43         ` Markus Mayer [this message]

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='CANEuBv60Pvet=dqiTPS1a8r6bCsUaTj380jGgYAzHxH1i8XiSw@mail.gmail.com' \
    --to=code@mmayer.net \
    --cc=cgit@lists.zx2c4.com \
    --cc=gstrauss@gluelogic.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).