mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sultan Alsawaf <sultan@kerneltoast.com>
To: Nathan Chancellor <natechancellor@gmail.com>
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
Date: Sun, 24 Mar 2019 00:18:07 -0700	[thread overview]
Message-ID: <20190324071807.GA29837@sultan-box.localdomain> (raw)
In-Reply-To: <20190324033153.GA15815@archlinux-ryzen>

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


  reply	other threads:[~2019-03-24  7:18 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-24  1:44 [RFC] " Sultan Alsawaf
2019-03-24  2:24 ` [RFCv2] " Sultan Alsawaf
2019-03-24  3:31   ` Nathan Chancellor
2019-03-24  7:18     ` Sultan Alsawaf [this message]
2019-03-24 21:17   ` Rasmus Villemoes
2019-03-24 22:32     ` Sultan Alsawaf
2019-03-25 21:24       ` Rasmus Villemoes
2019-03-30 22:59         ` Sultan Alsawaf
2019-04-01 20:43           ` Rasmus Villemoes
2019-04-01 23:55             ` Sultan Alsawaf
2019-04-02  8:17               ` Rasmus Villemoes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190324071807.GA29837@sultan-box.localdomain \
    --to=sultan@kerneltoast.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=natechancellor@gmail.com \
    --cc=rostedt@goodmis.org \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®