From: Alejandro Colomar <alx.manpages@gmail.com>
To: "Martin Liška" <mliska@suse.cz>,
gcc@gcc.gnu.org, "GNU C Library" <libc-alpha@sourceware.org>
Subject: Re: Missing optimization: mempcpy(3) vs memcpy(3)
Date: Mon, 12 Dec 2022 14:44:04 +0100 [thread overview]
Message-ID: <eccdad35-d7c2-0638-d946-8e94654e0e00@gmail.com> (raw)
In-Reply-To: <8f9d61cf-14a5-4099-e2b6-7c8cac47a28b@suse.cz>
[-- Attachment #1.1: Type: text/plain, Size: 3638 bytes --]
Hi Martin,
On 12/12/22 14:37, Martin Liška wrote:
> On 12/9/22 18:11, Alejandro Colomar via Gcc wrote:
>> I expect the compiler to be knowledgeable enough to call whatever is fastest, whatever it is, but be consistent in both cases. However, here are the results:
>
> Hi.
>
> Note the glibc implementation of mempcpy typically uses (calls) memcpy, thus
Thanks for the info. I CCed glibc now, and copied my original email below for
completeness.
> I don't see any problem with the code snippets you provided.
Well, then the optimization may be the other way around (although I question why
it is implemented that way, and not the other way around, but I'm not a hardware
or libc guy, so there may be reasons).
If calling memcpy(3) is better, then the code calling mempcpy(3) could be
expanded inline to call it (but I doubt it).
If calling mempcpy(3) is better, then the hand-made pattern resembling
mempcpy(3) should probably be merged as a call to mempcpy(3).
But acting different on equivalent calls to both of them seems inconsistent to
me, unless you trust the programmer to know better how to optimize, that is...
Cheers,
Alex
-------- Forwarded Message --------
Subject: Missing optimization: mempcpy(3) vs memcpy(3)
Date: Fri, 9 Dec 2022 18:11:17 +0100
From: Alejandro Colomar <alx.manpages@gmail.com>
To: gcc@gcc.gnu.org
Hi!
I expect mempcpy(3) to be at least as fast as memcpy(3), since it performs the
same operations, with the exception that mempcpy(3) returns something useful (as
opposed to memcpy(3), which could perfectly return void), and in fact something
more likely to be in cache, if the copy is performed upwards.
The following two files are alternative implementations of a function, each one
written in terms of one of memcpy(3) and mempcpy(3):
$ cat usts2stp1.c
#include <string.h>
struct ustr_s {
size_t len;
char *ustr;
};
char *
usts2stp(char *restrict dst, const struct ustr_s *restrict src)
{
memcpy(dst, src->ustr, src->len);
dst[src->len] = '\0';
return dst + src->len;
}
$ cat usts2stp3.c
#define _GNU_SOURCE
#include <string.h>
struct ustr_s {
size_t len;
char *ustr;
};
char *
usts2stp(char *restrict dst, const struct ustr_s *restrict src)
{
char *end;
end = mempcpy(dst, src->ustr, src->len);
*end = '\0';
return end;
}
I expect the compiler to be knowledgeable enough to call whatever is fastest,
whatever it is, but be consistent in both cases. However, here are the results:
$ cc -Wall -Wextra -O3 -S usts2stp*.c
$ diff -u usts2stp[13].s
--- usts2stp1.s 2022-12-09 18:06:11.708367061 +0100
+++ usts2stp3.s 2022-12-09 18:06:11.740366451 +0100
@@ -1,4 +1,4 @@
- .file "usts2stp1.c"
+ .file "usts2stp3.c"
.text
.p2align 4
.globl usts2stp
@@ -6,16 +6,13 @@
usts2stp:
.LFB0:
.cfi_startproc
- pushq %rbx
+ subq $8, %rsp
.cfi_def_cfa_offset 16
- .cfi_offset 3, -16
- movq (%rsi), %rbx
+ movq (%rsi), %rdx
movq 8(%rsi), %rsi
- movq %rbx, %rdx
- call memcpy@PLT
- leaq (%rax,%rbx), %rax
+ call mempcpy@PLT
movb $0, (%rax)
- popq %rbx
+ addq $8, %rsp
.cfi_def_cfa_offset 8
ret
.cfi_endproc
The code with memcpy(3) seems to be worse (assuming both calls to be
equivalent). Shouldn't GCC produce the same code for both implementations?
Cheers,
Alex
--
<http://www.alejandro-colomar.es/>
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2022-12-12 13:44 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-09 17:11 Alejandro Colomar
2022-12-12 13:37 ` Martin Liška
2022-12-12 13:44 ` Alejandro Colomar [this message]
2022-12-12 13:56 ` Jakub Jelinek
2022-12-12 14:05 ` Alejandro Colomar
2022-12-12 14:48 ` Jonathan Wakely
2022-12-12 14:53 ` Jakub Jelinek
2022-12-12 15:56 ` Alejandro Colomar
2022-12-12 16:09 ` Jakub Jelinek
2022-12-12 17:15 ` Alejandro Colomar
2022-12-12 17:42 ` Jonathan Wakely
2022-12-12 14:34 Wilco Dijkstra
2022-12-12 14:57 ` Cristian Rodríguez
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=eccdad35-d7c2-0638-d946-8e94654e0e00@gmail.com \
--to=alx.manpages@gmail.com \
--cc=gcc@gcc.gnu.org \
--cc=libc-alpha@sourceware.org \
--cc=mliska@suse.cz \
/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).