linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC] string: Use faster alternatives when constant arguments are used
@ 2019-03-24  1:44 Sultan Alsawaf
  2019-03-24  2:24 ` [RFCv2] " Sultan Alsawaf
  0 siblings, 1 reply; 11+ messages in thread
From: Sultan Alsawaf @ 2019-03-24  1:44 UTC (permalink / raw)
  Cc: akpm, Sultan Alsawaf, linux-kernel

From: Sultan Alsawaf <sultan@kerneltoast.com>

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 <sultan@kerneltoast.com>
---
 include/linux/string.h | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 7927b875f..65db58342 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -476,4 +476,31 @@ 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),	\
+		(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


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2019-04-02  8:17 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-24  1:44 [RFC] string: Use faster alternatives when constant arguments are used Sultan Alsawaf
2019-03-24  2:24 ` [RFCv2] " Sultan Alsawaf
2019-03-24  3:31   ` Nathan Chancellor
2019-03-24  7:18     ` [RFC v3] " Sultan Alsawaf
2019-03-24 21:17   ` [RFCv2] " 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).