From patchwork Thu Aug 11 19:14:30 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Perches X-Patchwork-Id: 9275811 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 21F536022E for ; Thu, 11 Aug 2016 19:14:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1323A2876E for ; Thu, 11 Aug 2016 19:14:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 070C22877D; Thu, 11 Aug 2016 19:14:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AE9252876E for ; Thu, 11 Aug 2016 19:14:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752054AbcHKTOf (ORCPT ); Thu, 11 Aug 2016 15:14:35 -0400 Received: from smtprelay0026.hostedemail.com ([216.40.44.26]:54853 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751059AbcHKTOf (ORCPT ); Thu, 11 Aug 2016 15:14:35 -0400 Received: from filter.hostedemail.com (unknown [216.40.38.60]) by smtprelay05.hostedemail.com (Postfix) with ESMTP id B1ED2268E12; Thu, 11 Aug 2016 19:14:33 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-HE-Tag: cent70_7ef80a7d00952 X-Filterd-Recvd-Size: 2585 Received: from joe-laptop.perches.com (unknown [96.251.125.34]) (Authenticated sender: joe@perches.com) by omf12.hostedemail.com (Postfix) with ESMTPA; Thu, 11 Aug 2016 19:14:32 +0000 (UTC) From: Joe Perches To: Andrew Morton , Alexander Viro Cc: Alexey Dobriyan , Andi Kleen , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH] seq_puts: Optimize seq_puts for compile-time known constant string lengths Date: Thu, 11 Aug 2016 12:14:30 -0700 Message-Id: <954edd3038ed27a4ff1f525691bf29fba7d9379e.1470942477.git.joe@perches.com> X-Mailer: git-send-email 2.8.0.rc4.16.g56331f8 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Use a macro to call seq_write for constant strings and seq_puts for possible variable length char * uses. Parenthesize seq_puts to avoid recursive macro expansions. Code size is slightly larger per call, but a runtime calculation of strlen(string) is avoided. Signed-off-by: Joe Perches --- fs/seq_file.c | 2 +- include/linux/seq_file.h | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/fs/seq_file.c b/fs/seq_file.c index 19f532e..3e50e1f 100644 --- a/fs/seq_file.c +++ b/fs/seq_file.c @@ -661,7 +661,7 @@ void seq_putc(struct seq_file *m, char c) } EXPORT_SYMBOL(seq_putc); -void seq_puts(struct seq_file *m, const char *s) +void (seq_puts)(struct seq_file *m, const char *s) { int len = strlen(s); diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h index f3d45dd..672f4c0 100644 --- a/include/linux/seq_file.h +++ b/include/linux/seq_file.h @@ -116,7 +116,18 @@ void seq_vprintf(struct seq_file *m, const char *fmt, va_list args); __printf(2, 3) void seq_printf(struct seq_file *m, const char *fmt, ...); void seq_putc(struct seq_file *m, char c); -void seq_puts(struct seq_file *m, const char *s); + +void (seq_puts)(struct seq_file *m, const char *s); +/* Hack to allow constant strings to use compile-time calculated lengths */ +# define seq_puts(seq, string) \ +do { \ + if (__builtin_constant_p(string) && \ + (sizeof(string) != sizeof(const char *))) \ + seq_write(seq, string, sizeof(string) - 1); \ + else \ + (seq_puts)(seq, string); \ +} while (0) + void seq_put_decimal_ull(struct seq_file *m, char delimiter, unsigned long long num); void seq_put_decimal_ll(struct seq_file *m, char delimiter, long long num);