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.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED 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 6BAC42157C for ; Tue, 26 Mar 2024 03:19:20 +0100 (CET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1711419560; b=jvTTV5tozNIBkki/UVlaBcv8UGlcuQcT8CdWmYAaxmAzFQhhMdJPhZLL9Qmm9DTRkCVhzcj3X0 aOCSWbaqky3qaQIJN8ZnMAtXJsP25h67Ag2G3iRlY9Azjl3kiUYbOEq4eHzGIk4tgQ6zmvkyvg 1faE2u73j7Z9CMiNPwiV6Q41yUqyLIU55z2jcIoUCM6q7PjrWMLP4XAQAE6YfgQpFv3yxjk160 KS+DGdfY9YprVVpShRuiA/nTg0b9taLkRZowJmdLGY0tfIWglYYhJa8E3kfdWK0tW5lA3BBGKt QWVRfgjprFbYrhS5AvMr1xSsAVjicPK7N5Z/CpgSgh/FFw==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-lf1-f44.google.com) smtp.remote-ip=209.85.167.44; dkim=pass header.d=gmail.com header.s=20230601 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1711419560; bh=nbJRMHxSsledx8vG/IuJGvdqxduzdLR4i2rkaceujGQ=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To: References:MIME-Version:DKIM-Signature:DKIM-Signature; b=Fwj1TLep+hS104DX6iN7xJEM+N6VSQyBqA78Qc/RN7kgXxgDus8ltBNf3BJBF2cAX1XjbEkE9q 3J6flZRHgiYtK69oqNIpZ4PFA4Sx8D0ub1R+ff6OJb4/X73pkbkL/iaMkewwWZduFpNX1VG3yh 3NeE399KRgk/8Efia8JRRU80olpA0BAMnZ/tiYUmOhuAGPfzdAGn2K6XKgKcIP+GEyCsSb5gtr +FHlsJ4Rqptt8BRrDC4ll7k370CHFr2qFkyneQPo2clKeQaoOOnI3GCZdVOhceKYonQ/mc88TF +29JhSDC7+mNzAft4aYaBSP8qi/1ISzuhJSb1QQNRrmX3w==; 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:Content-Type:Cc:To:Subject:Message-ID :Date:From:In-Reply-To:References:MIME-Version:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=nbJRMHxSsledx8vG/IuJGvdqxduzdLR4i2rkaceujGQ=; b=eCTwfSY7Wxh8plXbHgYTxnJL5F Fl/HKf6M7OeXmctPimgL3YYL8XNpvxaID/6EqhH0AD9pvyHV1M67Hb2qiZ6EaYOOP1jCE7pPzXKol GqONQVEpJ6dDGztQ1lJa0FSjIYnsdA1TeAUjgE68QsSnWd4HiOVNxZ4VhIxlLIwy1D8TjqU18NdyZ IKjo6O11CtB3kRLeGeOqH8hqJGAqQortHJhI5PQdKqx4/ALn0HhDlr1JIiIaAjrjx6RXq7b1zp1i0 hOsYsnyuMcMAUclkVe8+uRRhuBSJLzJsAr1TKW5XhbrhUtn5xa2vUVcapSAMfsxooKoSfosvqpi82 Op+r6haQ==; Received: by zero.zsh.org with local id 1rowPH-000HlT-W5; Tue, 26 Mar 2024 02:19:20 +0000 Authentication-Results: zsh.org; iprev=pass (mail-lf1-f44.google.com) smtp.remote-ip=209.85.167.44; dkim=pass header.d=gmail.com header.s=20230601 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-lf1-f44.google.com ([209.85.167.44]:60927) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1rowOe-000HR0-3N; Tue, 26 Mar 2024 02:18:43 +0000 Received: by mail-lf1-f44.google.com with SMTP id 2adb3069b0e04-515b69e8f38so227522e87.1; Mon, 25 Mar 2024 19:18:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711419518; x=1712024318; darn=zsh.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=nbJRMHxSsledx8vG/IuJGvdqxduzdLR4i2rkaceujGQ=; b=kUdhDTDtjQ7vkHQmUG02iKZCkTjlpjmGpLe+VIOalByrFLdyXQzpZq1/VhpveaBqO2 cCl1ebv/MBL4nedRmgxcqXMPMa53/8YweqFLtuNtbP8XxFPAPgVwVmAFcHDMZ8iCYsAH GKkOuHOftbPHr9JIUim5JyQ8L1fk1dsg2dxfNkDnlmyeI3Aw40czCAqjrL7R7VB07pat yIWtuYpXQulJW7feyaeZLnlKbOzoJv+ojbxEwdjosvQv1LhrWSgjvyBPSVdvuSZgAB69 MKXY+DV/bCfGvk0pIpAD4aa1wV8bXaPcHcKZsbXcZzX5ImEyW+oahDXaXsVCtZTuqfJ6 GhDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711419518; x=1712024318; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nbJRMHxSsledx8vG/IuJGvdqxduzdLR4i2rkaceujGQ=; b=Da9NyVSMlrWkcNTAH07wFsG+BZGOPwDrYxB835Hm24mCXPorJkaxQ7J85h+DRn50T3 M+Caq/8/6qDWvPBvYd+AjefQkb1ZFwgDHz5K0yzqOWSUJ5rV7HTzGOmn0EJvM89S/Cc6 fu8uCD4Pa5P3u2tf+zkkwggRhqcxZd5Gr/LKGJ4no3XBxoJgj9GSFmvtQViJNlJnKNJc 7A7Xla9kdjYG+eK3bMKHh86RsGQneq8CDG3FzPiWecCXtRcYpbx+F6D1YN6qVGJP4/Er enoAW8cIRHiaYtFYEMsqUlVal3td8etIgI8fKOYjCIcpm6alj6dGDWOpny4sajQwoaRP lgUQ== X-Gm-Message-State: AOJu0YzsdfxRcVgOilRBJ4RiN5gkWsDhOSSIaUniQnxdu4QR2eojvG+8 K9+kN6J+/+e6l88pwFqak74tyESxpZ41YQJBOubWUX/haGbohsjjiAUfaHGuYJ6KyoD9Fsa9i85 TfcKc9x2RFFjl/2y9mnnsI7DCm8rr+o7z X-Google-Smtp-Source: AGHT+IENj35rRyaz9uUm4bJLuwKXbox1xNs2MynB2TrHv1TJAYDml7kechP1LDzLeLPKmIEI/xAI02rsvU1v0BwKz9Q= X-Received: by 2002:a05:6512:4023:b0:515:b06f:a89e with SMTP id br35-20020a056512402300b00515b06fa89emr2339108lfb.22.1711419517994; Mon, 25 Mar 2024 19:18:37 -0700 (PDT) MIME-Version: 1.0 References: <20240318071148.135833-1-tirtajames45@gmail.com> <36117-1711413736.217542@p2pe.Gxts.15z4> In-Reply-To: <36117-1711413736.217542@p2pe.Gxts.15z4> From: James Date: Tue, 26 Mar 2024 09:18:26 +0700 Message-ID: Subject: Re: [PATCH v2] prefer memcpy() over strcpy() To: Oliver Kiddle Cc: zsh-workers@zsh.org Content-Type: multipart/alternative; boundary="0000000000006e8edd061486e80e" X-Seq: 52823 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: --0000000000006e8edd061486e80e Content-Type: text/plain; charset="UTF-8" On Tue, Mar 26, 2024, 7:42 AM Oliver Kiddle wrote: > On 18 Mar, James Tirta Halim wrote: > > Add zmemcpyz(), a memcpy() that nul-terminates the destination string. > > This is meant to be used when we have the strlen() of the string. > > I was hoping someone else would review this. It touches on areas I'm > unsure of and I needed to check on how static inline differs between C > and C++. > > > We should prefer memcpy() when we know the length because it 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. > > Have you verified that this does actually have a positive effect on > performance? Musl doesn't provide an assembly implementation for strcpy, so it should be as fast as asm memcpy is to C memcpy. Glibc does, so performance difference is theoretical. > If we're bothering to do this, I'd also assume that mempcpy() might also > be a teany tiny bit faster on systems that support it. Autoconf should > make that easy to check for. > Sure. > > --- a/Src/zsh.h > > > +#include > > +#include > > Currently there are no #includes in zsh.h so I'd be uneasy about adding > some without being fully sure I understood the reasoning behind the > current setup for prototypes and headers. For now, it is only used in > string.c. It'd be easier to add there only for now and address our > use of inline functions separately. The makepro.awk mechanism largely > predates inline functions so I'm not quite sure how we can arrange for > inline functions to appear in full in string.epro or whatever zsh.mdh > includes. Anyone know? > I think it would be better to make it a macro for now. The function call overhead may become a pessimization for small strings. > Oliver > --0000000000006e8edd061486e80e Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Tue, Mar 26, 2024, 7:42 AM Oliver Kiddle <opk@zsh.org> wrote:
On 18 Mar, James Tirta Halim wrote:
> Add zmemcpyz(), a memcpy() that nul-terminates the destination string.=
> This is meant to be used when we have the strlen() of the string.

I was hoping someone else would review this. It touches on areas I'm unsure of and I needed to check on how static inline differs between C
and C++.

> We should prefer memcpy() when we know the length because it most libc=
> implementations provide an assembly implementation of memcpy but maybe=
> not strcpy(). Even if it is implemented in assembly, memcpy() is likel= y
> to be faster than strcpy() since as the loop condition strcpy() needs<= br> > to check for zeros in SRC, whereas memcpy() can just decrement the siz= e.

Have you verified that this does actually have a positive effect on
performance?
Musl doesn't pro= vide an assembly implementation for strcpy, so it should be as fast as asm = memcpy is to C memcpy. Glibc does, so performance difference is theoretical= .
If we're bothering to do this, I'd also assume that mempcpy() might= also
be a teany tiny bit faster on systems that support it. Autoconf should
make that easy to check for.
= Sure.
> --- a/Src/zsh.h

> +#include <stddef.h>
> +#include <string.h>

Currently there are no #includes in zsh.h so I'd be uneasy about adding=
some without being fully sure I understood the reasoning behind the
current setup for prototypes and headers. For now, it is only used in
string.c. It'd be easier to add there only for now and address our
use of inline functions separately. The makepro.awk mechanism largely
predates inline functions so I'm not quite sure how we can arrange for<= br> inline functions to appear in full in string.epro or whatever zsh.mdh
includes. Anyone know?
I thin= k it would be better to make it a macro for now. The function call overhead= may become a pessimization for small strings.
Oliver
--0000000000006e8edd061486e80e--