Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp2138250img; Sun, 24 Mar 2019 00:19:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqxdZH2PDGzv3JDXztv3xd0ogo6wpvVyO8iGIDLwKVRfrWFzJFsKv7z1BtWlMRRNX8r1Gm8j X-Received: by 2002:a62:12c9:: with SMTP id 70mr14378679pfs.156.1553411971068; Sun, 24 Mar 2019 00:19:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553411971; cv=none; d=google.com; s=arc-20160816; b=w/8p1yhOSlLS/WCP4atIjLmA/xTWVOWF0IzuS9QYeFh/IrAdh9yjLLo6FIWHqWU1Uy BPiOCJVMv9SQZj9HV6l/4+DXqFHKB7g0nYxGbQk5fAuyTTzUUJuTF6qtEadER2EE8kKz 04xTrlc3xvPoUlk2zhyJWwdSuaHb0M6ujNUyEoXzMcde76wjNxr1hBfTSkrTZORbnGwV W+FCDVi/hEZKQ7bI1HMhsYhk7Se22fr4lZXYVcQm4u4N19Zz+b0iakk6SwBFSnhfJEZn QBTIOCw/g5VL22iKwCz4erw8u7SlhFaf0mDmLk8f8lMZ5r1OMo6jlHPSZrsmKFBf3Vet rzwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=p6YU+zDYiefJ3LfU1CTV9V30b9NyJOktuTy7ANCETss=; b=dj89IClD71Ernszg7CtrtO+BedMoAU31Xo0VSEczfetmWRZs464TPTwtMBS8b8TaLq 0Mh7Jg1RePUbdZpNCt3lVW3RymfBITBKJoV4VDiPs7LCHy6TXkBYtf3i0as8OBdpQ2o8 oWZgoECNYmeEDK495UY9hNm3nJZiLlmVTUDtA/kJNVKgNJq0pFTd1qC6OJ5fVlBfR4rB kE56NZj3c6nsFf+x5SGcmnyJ4u8f7hmt+EWsSrLp7wjG4E5Hng9MtGOmr05qW6wVdtaL 3e3/Sstx5RhuFCENrLP6kfftPKdY+5cwIi2NKPvIDq5zk0FNNmROR+JJ0/0gsnhlySeb m/wg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f66si11458339pfb.261.2019.03.24.00.19.16; Sun, 24 Mar 2019 00:19:31 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728490AbfCXHSP (ORCPT + 99 others); Sun, 24 Mar 2019 03:18:15 -0400 Received: from mail-oi1-f195.google.com ([209.85.167.195]:42662 "EHLO mail-oi1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728176AbfCXHSP (ORCPT ); Sun, 24 Mar 2019 03:18:15 -0400 Received: by mail-oi1-f195.google.com with SMTP id w139so4681610oie.9 for ; Sun, 24 Mar 2019 00:18:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=p6YU+zDYiefJ3LfU1CTV9V30b9NyJOktuTy7ANCETss=; b=bGfvkJEunD5GebC8uSzuy/8C/csSUfy4RvB+ajbfyAIAb56YnuKUhz0CrcD6OzpRZM 5snnJSuQYMYw/KEPZR01khVTZI0c+br7VcK7duCnUet7+jvVVhns6nveMauCindvnHeu HGn9EhDEL+IixbZNfGj7OmRWVORAjWc2CntBTzfZoPWGS5eFUl8/0LATz6tIeKwARjFm lzbch8hjywuh2BwP/r5RFuMzqipgp5TEX+uA/hquBJS45rwWQQfb/sWubB3t45sNTu62 epJLrs+VWL1b5Nj589bV6WTh9E6CTpzVRWK61UibOPn7vnsVuj3cg1+yEMUZWF7KPSI5 jTXQ== X-Gm-Message-State: APjAAAVvkNfC8qpjSDCAJkFU46YLQA7kE5clN5tVp9XgjXRdHImvNfQr z1FmYR73lkXtgicEls4BTN4= X-Received: by 2002:aca:4e50:: with SMTP id c77mr7636817oib.109.1553411894460; Sun, 24 Mar 2019 00:18:14 -0700 (PDT) Received: from sultan-box.localdomain ([107.193.118.89]) by smtp.gmail.com with ESMTPSA id w141sm2379363oie.1.2019.03.24.00.18.12 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 24 Mar 2019 00:18:13 -0700 (PDT) Date: Sun, 24 Mar 2019 00:18:07 -0700 From: Sultan Alsawaf To: Nathan Chancellor Cc: akpm@linux-foundation.org, linux-kernel@vger.kernel.org, arnd@arndb.de, keescook@chromium.org, linux@rasmusvillemoes.dk, rostedt@goodmis.org, torvalds@linux-foundation.org, viro@zeniv.linux.org.uk Subject: Re: [RFC v3] string: Use faster alternatives when constant arguments are used Message-ID: <20190324071807.GA29837@sultan-box.localdomain> References: <20190324014445.28688-1-sultan@kerneltoast.com> <20190324022406.GA18988@sultan-box.localdomain> <20190324033153.GA15815@archlinux-ryzen> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190324033153.GA15815@archlinux-ryzen> User-Agent: Mutt/1.11.4 (2019-03-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Mar 23, 2019 at 08:31:53PM -0700, Nathan Chancellor wrote: > Explicitly cc'ing some folks who have touched include/linux/string.h in > the past and might want to take a look at this. > > Nathan Thanks. One last revision with some nitpicks fixed is attached, though I doubt it'll influence any comments on the concept of this anyway. --- include/linux/string.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index 7927b875f..40a8a9436 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -476,4 +476,34 @@ static __always_inline size_t str_has_prefix(const char *str, const char *prefix return strncmp(str, prefix, len) == 0 ? len : 0; } +/* + * Replace some common string helpers with faster alternatives when one of the + * arguments is a constant (i.e., literal string). This uses strlen instead of + * sizeof for calculating the string length in order to silence compiler + * warnings that may arise due to what the compiler thinks is incorrect sizeof + * usage. The strlen calls on constants are folded into scalar values at compile + * time, so performance is not reduced by using strlen. + */ +#define strcpy(dest, src) \ + __builtin_choose_expr(__builtin_constant_p(src), \ + memcpy((dest), (src), strlen(src) + 1), \ + (strcpy)((dest), (src))) + +#define strcat(dest, src) \ + __builtin_choose_expr(__builtin_constant_p(src), \ + ({ \ + memcpy((dest) + strlen(dest), (src), strlen(src) + 1); \ + (dest); \ + }), \ + (strcat)((dest), (src))) + +#define strcmp(left, right) \ + __builtin_choose_expr(__builtin_constant_p(left), \ + __builtin_choose_expr(__builtin_constant_p(right), \ + (strcmp)((left), (right)), \ + memcmp((left), (right), strlen(left) + 1)), \ + __builtin_choose_expr(__builtin_constant_p(right), \ + memcmp((left), (right), strlen(right) + 1), \ + (strcmp)((left), (right)))) + #endif /* _LINUX_STRING_H_ */ -- 2.21.0