All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Hou Tao <houtao1@huawei.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
	Martin KaFai Lau <kafai@fb.com>, Yonghong Song <yhs@fb.com>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>
Subject: Re: [PATCH bpf-next 4/5] selftests/bpf: add benchmark for bpf_strncmp() helper
Date: Wed, 8 Dec 2021 12:08:04 -0800	[thread overview]
Message-ID: <CAEf4BzYtKNs8ORZxSgFtWZA49-Rjp5UVww_s9MWKq2DNUMts_w@mail.gmail.com> (raw)
In-Reply-To: <97a9166f-7eec-49e2-1f94-ae2fc21dcf02@huawei.com>

On Wed, Dec 8, 2021 at 5:47 AM Hou Tao <houtao1@huawei.com> wrote:
>
> Hi,
>
> On 12/7/2021 11:01 AM, Andrii Nakryiko wrote:
> > On Tue, Nov 30, 2021 at 6:07 AM Hou Tao <houtao1@huawei.com> wrote:
> >> Add benchmark to compare the performance between home-made strncmp()
> >> in bpf program and bpf_strncmp() helper. In summary, the performance
> >> win of bpf_strncmp() under x86-64 is greater than 18% when the compared
> >> string length is greater than 64, and is 179% when the length is 4095.
> >> Under arm64 the performance win is even bigger: 33% when the length
> >> is greater than 64 and 600% when the length is 4095.
> snip
> >> +
> >> +long hits = 0;
> >> +char str[STRNCMP_STR_SZ];
> >> +
> >> +char _license[] SEC("license") = "GPL";
> >> +
> >> +static __always_inline int local_strncmp(const char *s1, unsigned int sz,
> >> +                                        const char *s2)
> >> +{
> >> +       int ret = 0;
> >> +       unsigned int i;
> >> +
> >> +       for (i = 0; i < sz; i++) {
> >> +               /* E.g. 0xff > 0x31 */
> >> +               ret = (unsigned char)s1[i] - (unsigned char)s2[i];
> > I'm actually not sure if it will perform subtraction in unsigned form
> > (and thus you'll never have a negative result) and then cast to int,
> > or not. Why not cast to int instead of unsigned char to be sure?
> It is used to handle the character which is greater than or equal with 0x80.
> When casting these character into int, the result will be a negative value,
> the compare result will always be negative and it is wrong because
> 0xff should be greater than 0x31.

I see about (unsigned char) cast, but I was worried that subtraction
result won't be negative I've tested with

$ cat test.c
#include <stdio.h>

int main() {
        int x = (unsigned char)190 - (unsigned char)255;
        printf("%d\n", x);
}


Seems like it behaves sanely (at least on this particular compiler),
so I'm fine with it.

  reply	other threads:[~2021-12-08 20:08 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-30 14:22 [PATCH bpf-next 0/5] introduce bpf_strncmp() helper Hou Tao
2021-11-30 14:22 ` [PATCH bpf-next 1/5] bpf: add bpf_strncmp helper Hou Tao
2021-11-30 14:22 ` [PATCH bpf-next 2/5] selftests/bpf: fix checkpatch error on empty function parameter Hou Tao
2021-11-30 14:22 ` [PATCH bpf-next 3/5] selftests/bpf: factor out common helpers for benchmarks Hou Tao
2021-12-07  2:55   ` Andrii Nakryiko
2021-12-08 13:41     ` Hou Tao
2021-11-30 14:22 ` [PATCH bpf-next 4/5] selftests/bpf: add benchmark for bpf_strncmp() helper Hou Tao
2021-12-07  3:01   ` Andrii Nakryiko
2021-12-08 13:47     ` Hou Tao
2021-12-08 20:08       ` Andrii Nakryiko [this message]
2021-11-30 14:22 ` [PATCH bpf-next 5/5] selftests/bpf: add test cases for bpf_strncmp() Hou Tao
2021-12-07  3:09   ` Andrii Nakryiko
2021-12-08 13:50     ` Hou Tao
2021-12-03  2:09 ` [PATCH bpf-next 0/5] introduce bpf_strncmp() helper Alexei Starovoitov

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=CAEf4BzYtKNs8ORZxSgFtWZA49-Rjp5UVww_s9MWKq2DNUMts_w@mail.gmail.com \
    --to=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=houtao1@huawei.com \
    --cc=kafai@fb.com \
    --cc=netdev@vger.kernel.org \
    --cc=yhs@fb.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.