public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx.manpages@gmail.com>
To: Wilco Dijkstra <Wilco.Dijkstra@arm.com>
Cc: 'GNU C Library' <libc-alpha@sourceware.org>
Subject: stpecpy(3) vs strlcpy(3) benchmark (was: [PATCH 1/1] string: Add stpecpy(3))
Date: Sat, 24 Dec 2022 03:30:39 +0100	[thread overview]
Message-ID: <a847a0ac-552b-dc78-9020-90f90d3955c3@gmail.com> (raw)
In-Reply-To: <91d159d1-d379-af67-6859-bf8e3fa14c72@gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 3331 bytes --]

Hi Wilco,

Here goes benchmark code for comparing performance of stpecpy(3) against 
strlcpy(3).  I used my_strlcpy(3), with your definition, because I suspect 
libbsd's version won't be optimized.  stpecpy(3) was also updated to be as 
optimized as I can write it.

I won't be showing exact numbers, as that's probably very system- and 
version-dependent.  Test yourself for exact numbers.  However, I'll share the 
general results.

At first I was a little bit surprised (and disappointed) because strlcpy(3) 
seemed faster under GCC.  When forming the very short string "Hello, " "worlds" 
"!", in just 3 consecutive calls, my_strlcpy(3) outperforms (~10%) stpecpy() in 
GCC.  In Clang they had both similar times.

When I added a long string of 'x's, the results more or less were the same.

When I added a few calls by repeating "worlds" "!", that changed the balance 
very significantly for stpecpy(3).  When chaining more than just a couple calls, 
strlcpy(3) starts showing a performance decrease.  With the code below, I saw 
stpecpy(3) consistently outperform strlcpy(3) by around 3%.

I guess the advantage of strlcpy(3) over stpecpy(3) may be due to the overhead 
of setting 'end = buf + BUFSIZ;' for stpecpy(3), and to the fact that the first 
strlcpy(3) call doesn't have to do the usual '+=', ' + l', and ' - l', which can 
be significant given how negligible the internal difference between the two is.

Cheers,
Alex

---

alx@debian:~/tmp$ cat foo.c
#include <err.h>
#include <stdio.h>

#include <stp/stpe/stpecpy.h>

void
foo(char *buf, size_t *len)
{
	char    *p, *end;

	end = buf + BUFSIZ;
	p = buf;
	p = stpecpy(p, end, "Heylo, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
	p = stpecpy(p, end, "worlds");
	p = stpecpy(p, end, "!");
	p = stpecpy(p, end, "worlds");
	p = stpecpy(p, end, "!");
	p = stpecpy(p, end, "worlds");
	p = stpecpy(p, end, "!");
	p = stpecpy(p, end, "worlds");
	p = stpecpy(p, end, "!");
	if (p == end) {
		p--;
		warnx("Truncated");
	}
	*len = p - buf;
}

void
bar(char *buf, size_t *len)
{
	size_t l;

	l = my_strlcpy(buf, "Hello, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", BUFSIZ);
	if (l >= BUFSIZ)
		goto toolong;
	l += my_strlcpy(buf + l, "worlds", BUFSIZ - l);
	if (l >= BUFSIZ)
		goto toolong;
	l += my_strlcpy(buf + l, "!", BUFSIZ - l);
	if (l >= BUFSIZ)
		goto toolong;
	l += my_strlcpy(buf + l, "worlds", BUFSIZ - l);
	if (l >= BUFSIZ)
		goto toolong;
	l += my_strlcpy(buf + l, "!", BUFSIZ - l);
	if (l >= BUFSIZ)
		goto toolong;
	l += my_strlcpy(buf + l, "worlds", BUFSIZ - l);
	if (l >= BUFSIZ)
		goto toolong;
	l += my_strlcpy(buf + l, "!", BUFSIZ - l);
	if (l >= BUFSIZ)
		goto toolong;
	l += my_strlcpy(buf + l, "worlds", BUFSIZ - l);
	if (l >= BUFSIZ)
		goto toolong;
	l += my_strlcpy(buf + l, "!", BUFSIZ - l);
	if (l >= BUFSIZ) {
toolong:
		l = BUFSIZ - 1;
		warnx("Truncated");
	}

	*len = l;
}

alx@debian:~/tmp$ cat bench.c
#include <stdio.h>

int foo(char *buf, size_t *len);
int bar(char *buf, size_t *len);

int
main(void)
{
	char    buf[BUFSIZ];
	size_t  len;

	for (size_t i = 0; i < 10000000; i++)
#if 1
		foo(buf, &len);
#else
		bar(buf, &len);
#endif

	printf("%zu: %s\n", len, buf);
}


-- 
<http://www.alejandro-colomar.es/>

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2022-12-24  2:30 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-23 23:24 [PATCH 1/1] string: Add stpecpy(3) Wilco Dijkstra
2022-12-24  0:05 ` Alejandro Colomar
2022-12-24  0:26   ` Alejandro Colomar
2022-12-24  2:30     ` Alejandro Colomar [this message]
2022-12-24 10:28       ` stpecpy(3) vs strlcpy(3) benchmark (was: [PATCH 1/1] string: Add stpecpy(3)) Alejandro Colomar
2022-12-25  1:52     ` [PATCH 1/1] string: Add stpecpy(3) Noah Goldstein
2022-12-25 14:37       ` Alejandro Colomar
2022-12-25 22:31         ` Noah Goldstein
2022-12-26  0:26           ` Alejandro Colomar
2022-12-26  0:32             ` Noah Goldstein
2022-12-26  0:37               ` Alejandro Colomar
2022-12-26  2:43                 ` Noah Goldstein
2022-12-26 22:25                   ` Alejandro Colomar
2022-12-26 23:24                     ` Alejandro Colomar
2022-12-26 23:52                       ` Alejandro Colomar
2022-12-27  0:12                         ` Alejandro Colomar

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=a847a0ac-552b-dc78-9020-90f90d3955c3@gmail.com \
    --to=alx.manpages@gmail.com \
    --cc=Wilco.Dijkstra@arm.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).