mailing list of musl libc
 help / color / mirror / code / Atom feed
From: NRK <nrk@disroot.org>
To: musl@lists.openwall.com
Subject: Re: [musl] Question: Why vfprintf call twice printf_core?
Date: Sat, 6 May 2023 23:55:27 +0600	[thread overview]
Message-ID: <20230506175527.ic4ibgxo6e7mquq4@gen2.localdomain> (raw)
In-Reply-To: <ZFXy1aD+T1fGzbov@voyager>

On Sat, May 06, 2023 at 08:25:25AM +0200, Markus Wichmann wrote:
> If you're looking for performance, however, I suggest steering clear of
> the printf() family of functions. They contain complex logic that is
> typically way overpowered for common needs, and just straight string
> manipulation will always be faster.

Agreed. However...

> E.g. the above call could be turned into
> 
> strlcpy(buf, "this is a more typical error message with detail: ", sizeof buf);
> strlcat(buf, "No such file or directory", sizeof buf);

strcat (and friends) are the opposite of performance:
https://en.wikipedia.org/wiki/Joel_Spolsky#Schlemiel_the_Painter.27s_algorithm

Better alternative: have your string copy function return a pointer to the
nul-byte. This pointer can be both used for efficient concat as well as
determining the string length.

Example using POSIX stpcpy(3) (minus bounds checking):

	char *p = stpcpy(buf, "this is a more typical error message with detail: ");
	p = stpcpy(p, "No such file or directory");
	write(2, buf, p - buf);

Additionally, consider getting rid of nul-strings altogether and only
use them in interface boundaries that require them.

- NRK

  reply	other threads:[~2023-05-06 17:55 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-06  3:29 847567161
2023-05-06  3:53 ` Markus Wichmann
2023-05-06  5:24   ` =?gb18030?B?ODQ3NTY3MTYx?=
2023-05-06  6:25     ` Markus Wichmann
2023-05-06 17:55       ` NRK [this message]
2023-05-07  1:17       ` Rich Felker
2023-05-07  1:44         ` [musl] =?gb18030?B?u9i4tKO6IFttdXNsXSBRdWVzdGlvbjogV2h5IHZmcHJpbnRmIGNhbGwgdHdpY2UgcHJpbnRmX2NvcmU/?= =?gb18030?B?ODQ3NTY3MTYx?=

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=20230506175527.ic4ibgxo6e7mquq4@gen2.localdomain \
    --to=nrk@disroot.org \
    --cc=musl@lists.openwall.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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).