unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: abush wang <abushwangs@gmail.com>
To: "H.J. Lu" <hjl.tools@gmail.com>,
	 abushwang via Libc-alpha <libc-alpha@sourceware.org>
Subject: x86-64: strlen-evex performance performance degradation compared to strlen-avx2
Date: Fri, 26 Apr 2024 12:03:20 +0800	[thread overview]
Message-ID: <CAMLoAPYs1FgpnTgJ36YxA2R71KAe+qfosRiqUMdrpHyKPCJ45w@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2188 bytes --]

Hi, H.J.
When I test glibc performance between 2.28 and 2.38,
I found there is a performance degradation about strlen.
In fact, this difference comes from __strlen_avx2 and __strlen_evex

```
2.28
__strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:42
42 ENTRY (STRLEN)


2.38
__strlen_evex () at ../sysdeps/x86_64/multiarch/strlen-evex.S:79
79 ENTRY_P2ALIGN (STRLEN, 6)
```

This is my test:
```
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#define MAX_STRINGS 100

uint64_t rdtsc() {
    uint32_t lo, hi;
    __asm__ __volatile__ (
        "rdtsc" : "=a"(lo), "=d"(hi)
    );
    return ((uint64_t)hi << 32) | lo;
}

int main(int argc, char *argv[]) {
    char *input_str[MAX_STRINGS];
    size_t lengths[MAX_STRINGS];
    int num_strings = 0; // Number of input strings
    uint64_t start_cycles, end_cycles;

    // Parse command line arguments and store pointers in input_str array
    for (int i = 1; i < argc && num_strings < MAX_STRINGS; ++i) {
        input_str[num_strings] = argv[i];
        num_strings++;
    }

    // Measure the strlen operation for each string
    start_cycles = rdtsc();
    for (int i = 0; i < num_strings; ++i) {
        lengths[i] = strlen(input_str[i]);
    }
    end_cycles = rdtsc();

    unsigned long long total_cycle = end_cycles - start_cycles;
    unsigned long long av_cycle = total_cycle / num_strings;

    // Print the total cycles taken for the strlen operations
    printf("Total cycles: %llu av cycle: %llu \n", total_cycle, av_cycle);

    // Print the recorded lengths
    printf("Lengths of the input strings:\n");
    for (int i = 0; i < num_strings; ++i) {
        printf("String %d length: %zu\n", i, lengths[i]);
    }

    return 0;
}
```

This is result
```
2.28
./strlen_test str1 str2 str3 str4 str5
Total cycles: 1468 av cycle: 293
Lengths of the input strings:
String 0 length: 4
String 1 length: 4
String 2 length: 4
String 3 length: 4
String 4 length: 4

2.38
./strlen_test str1 str2 str3 str4 str5
Total cycles: 1814 av cycle: 362
Lengths of the input strings:
String 0 length: 4
String 1 length: 4
String 2 length: 4
String 3 length: 4
String 4 length: 4
```

Thanks,
abush

[-- Attachment #2: Type: text/html, Size: 2768 bytes --]

             reply	other threads:[~2024-04-26  4:04 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-26  4:03 abush wang [this message]
2024-04-26 13:30 ` x86-64: strlen-evex performance performance degradation compared to strlen-avx2 H.J. Lu
2024-04-26 16:53   ` Sunil Pandey
2024-04-28  2:13     ` abush wang
2024-04-28 16:12       ` Sunil Pandey
2024-04-28 16:16         ` H.J. Lu
2024-04-29 17:41           ` Sunil Pandey
2024-04-29 20:19             ` H.J. Lu
2024-04-30  0:54               ` Sunil Pandey
2024-04-30  2:51                 ` H.J. Lu
2024-04-30 20:16                   ` Sunil Pandey
2024-04-28  2:06   ` abush wang

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=CAMLoAPYs1FgpnTgJ36YxA2R71KAe+qfosRiqUMdrpHyKPCJ45w@mail.gmail.com \
    --to=abushwangs@gmail.com \
    --cc=hjl.tools@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).