zsh-workers
 help / color / mirror / code / Atom feed
From: Oliver Kiddle <opk@zsh.org>
To: James <tirtajames45@gmail.com>
Cc: Bart Schaefer <schaefer@brasslantern.com>, zsh-workers@zsh.org
Subject: Re: [PATCH v2] string.c: remove use of strcat() after strlen()
Date: Sun, 28 Jan 2024 01:57:13 +0100	[thread overview]
Message-ID: <7484-1706403433.584667@iZw-.5au7.GRbt> (raw)
In-Reply-To: <CANDqPp1yQ_r_6QSGp27mw7_5Nhc-XHowbe-TyOqnU8jb1KLDzg@mail.gmail.com>

On 31 Dec, James wrote:
> Since we are already calling strlen() beforehand to know the size of
> allocation, we can just memcpy() from BASE + BASE_LEN and avoid another strlen
> () call in strcat(). We should prefer memcpy() because we always know the
> length and most libc implementations provide an assembly implementation of
> memcpy but maybe not strcpy(). Even if it is implemented in assembly, memcpy()
> is likely to be faster than strcpy() since as the loop condition strcpy() needs
> to check for zeros in SRC, whereas memcpy() can just decrement the size.
>
> I'm using a MEMPCPY macro because I don't know how zsh handles using GNU/POSIX
> extensions.

Many OS-specific extensions are used. We'd normally add a test in
configure.ac and then check for that so the macro could be wrapped in
#ifndef HAVE_MEMPCPY
And such macros may preserve the original lowercase which can be less
ugly. Calling the actual system mempcpy may be more efficient than the
macro because it probably already has a pointer to the end. FreeBSD
also has mempcpy().

If it is meant as an optimisation it could be worth verifying that it
has an effect. I don't especially find the modified code any easier to
read. I find it quicker and easier to scan when simple nested calls like
strlen() are not first taken out and assigned to a variable. If it does
make things faster, there may be other places in the code where appstr()
could have been used but strcat/realloc were used directly.

Oliver


      parent reply	other threads:[~2024-01-28  0:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-26  6:01 James Tirta Halim
2023-12-31  4:22 ` Bart Schaefer
2023-12-31  5:10   ` James
2023-12-31  5:20     ` Bart Schaefer
2024-01-28  0:57     ` Oliver Kiddle [this message]

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=7484-1706403433.584667@iZw-.5au7.GRbt \
    --to=opk@zsh.org \
    --cc=schaefer@brasslantern.com \
    --cc=tirtajames45@gmail.com \
    --cc=zsh-workers@zsh.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.
Code repositories for project(s) associated with this public inbox

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

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