From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from EUR02-VI1-obe.outbound.protection.outlook.com (mail-vi1eur02on2088.outbound.protection.outlook.com [40.107.241.88]) by sourceware.org (Postfix) with ESMTPS id 6630F3858D1E for ; Fri, 23 Dec 2022 23:24:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6630F3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=nCN2vgcaNr6wyNrwJ2eBypK3NGvzVjvSZin8/TMzkm8=; b=XMyhUf0qBFT7PBqvvyATV/s0q8z8SiT6CJ4Tt8LVXrApjg5FH8GTb6eILzq7L9PkUCkrCZ6S7bGowaCFYuVfojDW1ikjqrilh5sXtCdo3W6uhL+i9Wq4wH1UrHaGdhlweSkSzVqNM+em0OFdU1IXavJ/0KzoZt/cUzpV3v5pO+0= Received: from AS9PR06CA0417.eurprd06.prod.outlook.com (2603:10a6:20b:461::21) by DBAPR08MB5622.eurprd08.prod.outlook.com (2603:10a6:10:1af::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5944.6; Fri, 23 Dec 2022 23:24:35 +0000 Received: from AM7EUR03FT046.eop-EUR03.prod.protection.outlook.com (2603:10a6:20b:461:cafe::62) by AS9PR06CA0417.outlook.office365.com (2603:10a6:20b:461::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5944.13 via Frontend Transport; Fri, 23 Dec 2022 23:24:34 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; pr=C Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by AM7EUR03FT046.mail.protection.outlook.com (100.127.140.78) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5944.14 via Frontend Transport; Fri, 23 Dec 2022 23:24:34 +0000 Received: ("Tessian outbound 8038f0863a52:v132"); Fri, 23 Dec 2022 23:24:34 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 639b6d532e7f8a65 X-CR-MTA-TID: 64aa7808 Received: from 00d42e5a0099.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id F86ECCF6-CF20-4D8C-8D65-B8ED9D64861A.1; Fri, 23 Dec 2022 23:24:27 +0000 Received: from EUR04-DB3-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 00d42e5a0099.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Fri, 23 Dec 2022 23:24:27 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BERHzT7Gg1xZENrSYePRcVrU9yF9dtQwrNvhavNRMww+uyqFWb3OAtU25Z+CY1jEEHVsUMSu6jiHNOq3NKhEcHwW0Cj74d95FFIANs5nxY2vKMOdwGS1nuXxMQiwKM8TzdborgT9N6sETApdT2yVRyF4rw2lRR3ta3ybTvMhFF6U3gM8ynOeKMWjHfGK+c9a/zGj2EFrj4iZkXctvylKcoYypGDXVaQzbtLbse6+GzPIlAAKuiU2Xxf94K/ZiAY3VMH+SqFQSkJcMmraMoVoPuRSfP8zz8DAakQJV63g20vMOHsVeIIamIYsoNQTOmt1pSq/RE5giFBK8fwYgk7cfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=nCN2vgcaNr6wyNrwJ2eBypK3NGvzVjvSZin8/TMzkm8=; b=ml0qay+XZvSuXLiujF0JO4/poHZtn8AyKTyhs/i2i5SZ1i2GrlN8WBKMG0BXh/u/IEvn+njAOXuqo6NVCOlmdDUlRebcjo5KJuS4A0Z8am8TfkB6HJGJcpSyCRxO6YSQdImNOOn8VsxpipPmE8y2Oi4+/4hrKiw+NvztN62NbE4SIHTbbW7NDGH1rHURiY9HheocSGtnbqet0d2Mso1OK7GyXHQsgMuh3D1c7+sclzU3YB6HXzfzMbWtRzqG82VX7RljLWBrOQ1SHGLxp3EzUcN+9BRK/hQoxjSBvynttU8xPw+AKABPHmh7eVoEYCfERuxULdVFUnGEe75mYamGRw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=nCN2vgcaNr6wyNrwJ2eBypK3NGvzVjvSZin8/TMzkm8=; b=XMyhUf0qBFT7PBqvvyATV/s0q8z8SiT6CJ4Tt8LVXrApjg5FH8GTb6eILzq7L9PkUCkrCZ6S7bGowaCFYuVfojDW1ikjqrilh5sXtCdo3W6uhL+i9Wq4wH1UrHaGdhlweSkSzVqNM+em0OFdU1IXavJ/0KzoZt/cUzpV3v5pO+0= Received: from PAWPR08MB8982.eurprd08.prod.outlook.com (2603:10a6:102:33f::20) by VI1PR08MB10145.eurprd08.prod.outlook.com (2603:10a6:800:1ca::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5944.13; Fri, 23 Dec 2022 23:24:25 +0000 Received: from PAWPR08MB8982.eurprd08.prod.outlook.com ([fe80::66e4:4940:d096:4f7]) by PAWPR08MB8982.eurprd08.prod.outlook.com ([fe80::66e4:4940:d096:4f7%9]) with mapi id 15.20.5944.006; Fri, 23 Dec 2022 23:24:25 +0000 From: Wilco Dijkstra To: Alejandro Colomar CC: 'GNU C Library' Subject: Re: [PATCH 1/1] string: Add stpecpy(3) Thread-Topic: [PATCH 1/1] string: Add stpecpy(3) Thread-Index: AQHZFxnO+gmQ84XT+U2biQA4st3vOQ== Date: Fri, 23 Dec 2022 23:24:23 +0000 Message-ID: Accept-Language: en-GB, en-US Content-Language: en-GB X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: Authentication-Results-Original: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com; x-ms-traffictypediagnostic: PAWPR08MB8982:EE_|VI1PR08MB10145:EE_|AM7EUR03FT046:EE_|DBAPR08MB5622:EE_ X-MS-Office365-Filtering-Correlation-Id: adf6fcb3-66cc-40c8-26c5-08dae53cda0c x-checkrecipientrouted: true nodisclaimer: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: sWGSbIFDjL8z1XFcAJiYMhKekCq2g6YEwHrLbVwLyQ/vGk3ssz9FuGL2aSEFCqREFrYfyLaa1ybBl9SA9WvlE/Bj4yt+h1ezYTBZ2uJQ+0wsu2qMnzmDiiDiA6DYpZ/4nbXUD6Ir3zg7YEKrXzBniugwDPJ59T46poDLR4cKNDk/pjPyxbl9Q6bTBl4gby2OVX5Gzqco7LbX7HCFqgi47wD+HRPjmFMAST3ciM3L1qqaveJnHA4+JJ7CfVuusRJWlHFlmrCxislNuNY/Ina7WZsIUI3UIbd3HpK9P+vYfHpSP5reizymWnaWT3EVdxtai0VGh9U5SAeMCqFgGfISu5bhp8vb60/xSqMdQqPEO/5fSEXQwRuF0Bst3gTwm7kthZT9+G1zkVO4/0zHZbzi5I4p5ah3MimefKbJIG8hoompHr/kX8qutF+gcKlj2RN6FwFNDtweRaPwpJ1jxF+rbbE3AmnZT2Yv7+o3qTOz4su4Vh9QLT/4NQBXoEPs+LJn3fFf32VovjaUTfCInw30aWBflQc+gon4qjwT+60BdxxvO4RgW5Z8plLJS2rFqfWBqa+U+6RjNvYEwPLHKWo5rnQzmHswa56RDvzO2CMDIBeSR4sP3ZK/lVHNgkN+4R7Ibw7SS/+ts+oRfuoUjnUDMEWQ+C2Nz3HKahLmtWjgedW6KAovkG7EEf7QL6+lYMdfq9oL38J//qwJ6piHheTDrg== X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PAWPR08MB8982.eurprd08.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(39850400004)(376002)(366004)(396003)(136003)(346002)(451199015)(2906002)(66899015)(5660300002)(8676002)(66476007)(64756008)(66446008)(66556008)(4326008)(66946007)(76116006)(91956017)(41300700001)(316002)(52536014)(33656002)(478600001)(8936002)(71200400001)(86362001)(7696005)(9686003)(6506007)(6916009)(186003)(26005)(83380400001)(38070700005)(55016003)(38100700002)(122000001);DIR:OUT;SFP:1101; Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR08MB10145 Original-Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM7EUR03FT046.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 87b5cc36-2d12-4772-c298-08dae53cd37f X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: T8DH1Cgqr/8oFl3Z1x2vYmiqHvGkeY5YDLKkNlI2bBnWKbFbtsFwAbnDE8mKOdusGrLg7Ry8vi2ilIHxYN8trAzG/MnnuR5Ji8M5HgJd0MdQib8b0abeU2XrtU1N/J14gUC6d0WGKs+6PKhsb8slZo+e9N2DC8TPfiOYwifxltbbNI5w6g7dBwcch6g79k339EheONre9MevH+OIZoDBFKGPvWuUEIK1bokO9//FiJ7UcNQYJWqNB90M/yctVzymxc5tzGry/goh++Jk3kVUg7CsKM3bHeLufCvpX//FIwTKGEmlFLPp0vdhoFPd8+jmqN+tMVcWvzoAv4XE30DXde+5TtvjgWQdNtPqyLd6ORbkYI5Qt8YQiHejqfdBWQ7CYg0X7l13AIH/Kyk6I/YMAe8Ct6MsvCaaRLovmKiHzETEZv64zp2HV/TXLCWpRqokXiTA26h6FLyHJZmXpB+PpulqRBLBcKhZQe+ZX4HFCC9uHAGTAovjxez9hCrdVAZ5PLRe4bF74XhpCV/G8Hn2FjJjr5vYCF1Z6QQgLggvjvy+N++9QcyrTd8b7K4e1wD2p8g1c3+QhzYF0ZCmkU1Tf4UNKtKAA+9A26JGaf5WlsFRdOJYltPlu5up2KoHEsFLwtJy3gBIeX7to7308TodKPGxzUbnOBbZYJYymla5+6Q= X-Forefront-Antispam-Report: CIP:63.35.35.123;CTRY:IE;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:64aa7808-outbound-1.mta.getcheckrecipient.com;PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com;CAT:NONE;SFS:(13230022)(4636009)(376002)(346002)(39850400004)(136003)(396003)(451199015)(46966006)(36840700001)(82740400003)(66899015)(6862004)(52536014)(33656002)(2906002)(5660300002)(86362001)(8936002)(4326008)(41300700001)(70586007)(70206006)(8676002)(40480700001)(81166007)(356005)(82310400005)(478600001)(55016003)(316002)(9686003)(36860700001)(186003)(7696005)(47076005)(83380400001)(336012)(6506007)(26005);DIR:OUT;SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Dec 2022 23:24:34.4749 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: adf6fcb3-66cc-40c8-26c5-08dae53cda0c X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d;Ip=[63.35.35.123];Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: AM7EUR03FT046.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBAPR08MB5622 X-Spam-Status: No, score=-5.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,FORGED_SPF_HELO,KAM_DMARC_NONE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE,TXREP,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Hi Alex,=0A= =0A= > For that, we'd first need to discuss what is a typical scenario.=0A= =0A= Like copying/concatenating strings that fit within the buffer.=0A= =0A= > And also, it depends a lot on what the compiler can optimize.=A0 If I cal= l =0A= > strlcat(3) in a loop, I know that stpecpy(3) is going to be orders of mag= nitude =0A= > faster.=0A= =0A= If you're trying to say that the 'strcat' variant is bad then yes absolutel= y -=0A= it's better to inline in the compiler or avoid the 'strcat' versions altoge= ther=0A= (that's also why I would strongly suggest never to add more 'cat' variants)= .=0A= But that doesn't say anything about whether stpecpy is better than strlcpy.= =0A= =0A= > If I call strlcpy(3) in a loop, doing what an ideal compiler might do, th= at =0A= > might be something to benchmark, but we'd also need to discuss what is a = good =0A= > input for the benchmark.=0A= =0A= The typical case would be copying or concatenating smallish strings to a bu= ffer.=0A= =0A= > In the OpenBSD definition of strlcpy(), I count 4 branches, and one of th= em is =0A= > inside a while loop.=A0 So, I'd find it very surprising if strlcpy(3) out= performed =0A= > stpecpy(3).=0A= =0A= If that really is the OpenBSD implementation then this proves my point that= =0A= non-standard string functions are often totally unoptimized.=0A= =0A= A basic implementation of strlcpy would use strlen and memcpy so it is fast= =0A= on every system without requiring any optimization:=0A= =0A= size_t=0A= strlcpy (char *dst, const char *src, size_t size)=0A= {=0A= size_t len =3D strlen (src);=0A= =0A= if (size =3D=3D 0)=0A= return len;=0A= size =3D len >=3D size ? size - 1 : len;=0A= dst[size] =3D 0;=0A= memcpy (dst, src, size);=0A= return len;=0A= }=0A= =0A= > Well, with the current memccpy(3) I already suspect it's going to be fast= er than =0A= > strlcpy(3).=A0 If you optimize it, it would increase the chances that it'= s faster :)=0A= =0A= I don't see why it would be any faster given memccpy might also not be=0A= optimized.=0A= =0A= > I find it _way_ more readable than the strlcpy(3)/cat(3) code.=A0 Oh, and= did I =0A= > say it has less branches? :)=0A= =0A= I'm not so sure about that - you've got 3 call/returns plus at least 4 bran= ches=0A= for each stpecpy (besides whatever memcpy/memchr do). strlcpy has 2 calls/= =0A= returns plus one branch. So needing an extra branch in case you need to do= =0A= something special for the buffer full case doesn't seem like a major proble= m.=0A= =0A= >> In contrast we can be pretty sure that the standard strlen, memcpy etc a= re both=0A= >> correct and efficient on all targets/libc's.=0A= >=0A= > Sure, but memcpy(3) is not usable in code that needs to truncate.=A0 We n= eed to =0A= > compare against stpncpy(3) (ughhh) and strlcpy(3).=0A= =0A= The idea is that if we add new string functions, their implementation shoul= d use=0A= other string functions that are known to be well optimized for most targets= .=0A= =0A= Cheers,=0A= Wilco=