From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.5 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: from zero.zsh.org (zero.zsh.org [IPv6:2a02:898:31:0:48:4558:7a:7368]) by inbox.vuxu.org (Postfix) with ESMTP id C64152471F for ; Sun, 28 Jan 2024 01:57:48 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Message-ID:Date: Content-Transfer-Encoding:Content-ID:Content-Type:MIME-Version:Subject:To: References:From:In-reply-to:cc:Reply-To:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=Upn6/76K0rGTTVgdTBg+zDL8GDs1FPWqYEHFVg/EpYg=; b=OVBzzJ+XsPcQUwHBKEbDGUk+Bf lEnrlxEByQNpx5sBOvXHP52+fKFHPc1kwSzc8dmcdf0xZsAwdxKpb0u/lJzAZN5NYMqnpV5wJbvJz zFNcwCkxTGa+oEJZHEQA0Pl+4NB8O5We6nmPlJe9GGSbRioSez//IpTOV0ac7AsXVmh5bXzwXOdTr TgnRE5SPfNSmll1pWqQYWu858CDyfMcZ8XOF0SqkBhg6a6WnlUekycRj3PKvWjU+o4PSTiBk0kjZQ F/l1i0LkYQa4DuJzt8ynt+vbVKaXdF21w3eUBJeHDZAoos492aUdFYGrZ6JFw4uou+BToDZCD9U4l 5PKTBopQ==; Received: by zero.zsh.org with local id 1rTtUa-000KIe-9l; Sun, 28 Jan 2024 00:57:48 +0000 Received: by zero.zsh.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) id 1rTtU2-000JyA-G4; Sun, 28 Jan 2024 00:57:14 +0000 Received: from [192.168.178.21] (helo=hydra) by mail.kiddle.eu with esmtp(Exim 4.97.1) (envelope-from ) id 1rTtU1-000000001wj-2T2Q; Sun, 28 Jan 2024 01:57:14 +0100 cc: Bart Schaefer , zsh-workers@zsh.org In-reply-to: From: Oliver Kiddle References: <20231226060159.182340-1-tirtajames45@gmail.com> To: James Subject: Re: [PATCH v2] string.c: remove use of strcat() after strlen() MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <7483.1706403433.1@hydra> Content-Transfer-Encoding: 8bit Date: Sun, 28 Jan 2024 01:57:13 +0100 Message-ID: <7484-1706403433.584667@iZw-.5au7.GRbt> X-Seq: 52508 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: 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