Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp2001756img; Sat, 23 Mar 2019 19:31:47 -0700 (PDT) X-Google-Smtp-Source: APXvYqwTjWVT1chUY8F0b1HioarfrgR76SReMgcuAjvm0Ezi/H/uJjUe/K7TpT+ogkiVm1g2iGtr X-Received: by 2002:a63:4247:: with SMTP id p68mr16685055pga.30.1553394707759; Sat, 23 Mar 2019 19:31:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553394707; cv=none; d=google.com; s=arc-20160816; b=cKvZixT/qmFJ9QOUd1+PURMI3/e/jUWIUX5dPnKNlm2gV8QB28E6k8jsuzMywD8A18 DXpJk/xWypwZUmup+mckXmOUxUtitPb6KVRebt0wrA4bybB90xPVwjvS8NmaZ3unHK6W HcmRJ+tnCG11vJgt8/fQVc6VNufKqAVahxMF3eH0TZqjDs8uXdhG4YMhSEWpM1LmYhF0 03SM6EV6A7acn4kn429V9mPKDDn2jknUmu6B/3QrGrbzLNddr+QAyQvbp8XmSLwqfy7D 7426RYmEZR8IJA8blqJvhlWs99Qoq6Xs+tYHF5oMjbKWzap6g0mH9olO+G3NrJMcG1v+ jkqw== 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:to :from:date; bh=eYHMHvfk1i1VZt1I+33kwKGsGJOWrp3Ur5mjx/9Dq0g=; b=DcC5iAwyamxG9LiFoNhYYzATDvPU3mQOwb8hWs56P3qnpjoyhXd+QAcTodAAkwOtad M8NbTtx2Ikh8QvPIgMQ3eX9zZNUgfVtRABhbsXCThQdCgNTuW38TRCx6EMkSOct9vLZ3 bw3z5ava+IPcdTMEsJEJHpoOJ7M0iAxFAB3aM15evOUthIgO1zk+zsx8xNYa14SNZKPM HBxTAE04urqAfqIL8eiV+wAzYDTiF/ZlTzCKeg7RlIS9e34rlxn/2hQ6NXoEMFudquwc /F/+82j3lwLH/YpqZfW0P1q+0xps8h4/mtl6c2FCEHrQ3eHh1Us3xGuEpJZ7DSAoJMGl MtnQ== 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 r3si10155917pgp.154.2019.03.23.19.31.32; Sat, 23 Mar 2019 19:31:47 -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 S1728043AbfCXC3g (ORCPT + 99 others); Sat, 23 Mar 2019 22:29:36 -0400 Received: from mail-ot1-f67.google.com ([209.85.210.67]:38146 "EHLO mail-ot1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727628AbfCXC3g (ORCPT ); Sat, 23 Mar 2019 22:29:36 -0400 Received: by mail-ot1-f67.google.com with SMTP id e80so5180241ote.5 for ; Sat, 23 Mar 2019 19:29:35 -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:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=eYHMHvfk1i1VZt1I+33kwKGsGJOWrp3Ur5mjx/9Dq0g=; b=BhRTlu3wod3kO/LOWZisBHA8F7Bi1TIRxdfcdOw72IW8Mxtsw0scdU5eGMl4af7T2o SG79D/fghmPkf06ZRFNpEi9LDezk04xuEbFDoE75R++CQtVDi7Pl44uMI3rPeaxl4cpk jJ2Of7+fQN0K03aldcgAROMoaz4/MXIMgPRdA6O0dXttWmu8+aR9gENXB1olL1hx+zHL gLxjBy0h5FtW5kLOg9GYqr8Drfsa2hZZySu9zUNpatBqJW0XDq7iITiiCRp3dXQBIHhI UemGoGZoI7GKdOe9YxMUp/gq4cmBkTgdFIxhEyPhXRWPed+N5+YxWXW0xYg02TWk5mOe h5qg== X-Gm-Message-State: APjAAAVaqw9OnqwJpFGHPw8bOTkeAylOs2ElLzzNexgiHDk00kDTthWO a1Bf9IyopcZoi0MrB8AcuKo= X-Received: by 2002:a9d:7ac7:: with SMTP id m7mr13197905otn.179.1553394251435; Sat, 23 Mar 2019 19:24:11 -0700 (PDT) Received: from sultan-box.localdomain ([107.193.118.89]) by smtp.gmail.com with ESMTPSA id 92sm4872410oti.60.2019.03.23.19.24.10 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 23 Mar 2019 19:24:10 -0700 (PDT) Date: Sat, 23 Mar 2019 19:24:06 -0700 From: Sultan Alsawaf To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Subject: [RFCv2] string: Use faster alternatives when constant arguments are used Message-ID: <20190324022406.GA18988@sultan-box.localdomain> References: <20190324014445.28688-1-sultan@kerneltoast.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190324014445.28688-1-sultan@kerneltoast.com> 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 I messed up the return value for strcat in the first patch. Here's a fixed version, ready for some scathing reviews. From: Sultan Alsawaf When strcpy, strcat, and strcmp are used with a literal string, they can be optimized to memcpy or memcmp calls. These alternatives are faster since knowing the length of a string argument beforehand allows traversal through the string word at a time without being concerned about looking for the terminating zero character. In some cases, the replaced calls to memcpy or memcmp can even be optimized out completely for a significant speed up. Signed-off-by: Sultan Alsawaf --- include/linux/string.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index 7927b875f..59c301c0e 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(strchr((dest), '\0'), (src), strlen(src) + 1); \ + (dest); \ + }), \ + (strcat)((dest), (src))) + +#define strcmp(dest, src) \ + __builtin_choose_expr(__builtin_constant_p(dest), \ + __builtin_choose_expr(__builtin_constant_p(src), \ + (strcmp)((dest), (src)), \ + memcmp((dest), (src), strlen(dest) + 1)), \ + __builtin_choose_expr(__builtin_constant_p(src), \ + memcmp((dest), (src), strlen(src) + 1), \ + (strcmp)((dest), (src)))) + #endif /* _LINUX_STRING_H_ */ -- 2.21.0