From: Andrew Morton <akpm@linux-foundation.org> To: Jani Nikula <jani.nikula@intel.com> Cc: linux-kernel@vger.kernel.org, Joonas Lahtinen <joonas.lahtinen@linux.intel.com>, Rodrigo Vivi <rodrigo.vivi@intel.com>, intel-gfx@lists.freedesktop.org, Vishal Kulkarni <vishal@chelsio.com>, netdev@vger.kernel.org, Greg Kroah-Hartman <gregkh@linuxfoundation.org>, linux-usb@vger.kernel.org, Julia Lawall <julia.lawall@lip6.fr>, Rasmus Villemoes <linux@rasmusvillemoes.dk> Subject: Re: [PATCH v4] string-choice: add yesno(), onoff(), enableddisabled(), plural() helpers Date: Wed, 23 Oct 2019 15:56:19 -0700 [thread overview] Message-ID: <20191023155619.43e0013f0c8c673a5c508c1e@linux-foundation.org> (raw) In-Reply-To: <20191023131308.9420-1-jani.nikula@intel.com> On Wed, 23 Oct 2019 16:13:08 +0300 Jani Nikula <jani.nikula@intel.com> wrote: > The kernel has plenty of ternary operators to choose between constant > strings, such as condition ? "yes" : "no", as well as value == 1 ? "" : > "s": > > $ git grep '? "yes" : "no"' | wc -l > 258 > $ git grep '? "on" : "off"' | wc -l > 204 > $ git grep '? "enabled" : "disabled"' | wc -l > 196 > $ git grep '? "" : "s"' | wc -l > 25 > > Additionally, there are some occurences of the same in reverse order, > split to multiple lines, or otherwise not caught by the simple grep. > > Add helpers to return the constant strings. Remove existing equivalent > and conflicting functions in i915, cxgb4, and USB core. Further > conversion can be done incrementally. > > The main goal here is to abstract recurring patterns, and slightly clean > up the code base by not open coding the ternary operators. Fair enough. > --- /dev/null > +++ b/include/linux/string-choice.h > @@ -0,0 +1,31 @@ > +/* SPDX-License-Identifier: MIT */ > +/* > + * Copyright © 2019 Intel Corporation > + */ > + > +#ifndef __STRING_CHOICE_H__ > +#define __STRING_CHOICE_H__ > + > +#include <linux/types.h> > + > +static inline const char *yesno(bool v) > +{ > + return v ? "yes" : "no"; > +} > + > +static inline const char *onoff(bool v) > +{ > + return v ? "on" : "off"; > +} > + > +static inline const char *enableddisabled(bool v) > +{ > + return v ? "enabled" : "disabled"; > +} > + > +static inline const char *plural(long v) > +{ > + return v == 1 ? "" : "s"; > +} > + > +#endif /* __STRING_CHOICE_H__ */ These aren't very good function names. Better to create a kernel-style namespace such as "choice_" and then add the expected underscores: choice_yes_no() choice_enabled_disabled() choice_plural() (Example: note that slabinfo.c already has an "onoff()"). Also, I worry that making these functions inline means that each .o file will contain its own copy of the strings ("yes", "no", "enabled", etc) if the .c file calls the relevant helper. I'm not sure if the linker is smart enough (yet) to fix this up. If not, we will end up with a smaller kernel by uninlining these functions. lib/string-choice.c would suit. And doing this will cause additional savings: calling a single-arg out-of-line function generates less .text than calling yesno(). When I did this: --- a/include/linux/string-choice.h~string-choice-add-yesno-onoff-enableddisabled-plural-helpers-fix +++ a/include/linux/string-choice.h @@ -8,10 +8,7 @@ #include <linux/types.h> -static inline const char *yesno(bool v) -{ - return v ? "yes" : "no"; -} +const char *yesno(bool v); static inline const char *onoff(bool v) { The text segment of drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.o (78 callsites) shrunk by 118 bytes.
WARNING: multiple messages have this Message-ID (diff)
From: Andrew Morton <akpm@linux-foundation.org> To: Jani Nikula <jani.nikula@intel.com> Cc: linux-usb@vger.kernel.org, Greg Kroah-Hartman <gregkh@linuxfoundation.org>, netdev@vger.kernel.org, intel-gfx@lists.freedesktop.org, Rasmus Villemoes <linux@rasmusvillemoes.dk>, linux-kernel@vger.kernel.org, Julia Lawall <julia.lawall@lip6.fr>, Vishal Kulkarni <vishal@chelsio.com> Subject: Re: [Intel-gfx] [PATCH v4] string-choice: add yesno(), onoff(), enableddisabled(), plural() helpers Date: Wed, 23 Oct 2019 15:56:19 -0700 [thread overview] Message-ID: <20191023155619.43e0013f0c8c673a5c508c1e@linux-foundation.org> (raw) Message-ID: <20191023225619.GZ4jCJZUZ2wd1Ba9cSX_JgEpfHQvRXo2_O_9Iw_CjZM@z> (raw) In-Reply-To: <20191023131308.9420-1-jani.nikula@intel.com> On Wed, 23 Oct 2019 16:13:08 +0300 Jani Nikula <jani.nikula@intel.com> wrote: > The kernel has plenty of ternary operators to choose between constant > strings, such as condition ? "yes" : "no", as well as value == 1 ? "" : > "s": > > $ git grep '? "yes" : "no"' | wc -l > 258 > $ git grep '? "on" : "off"' | wc -l > 204 > $ git grep '? "enabled" : "disabled"' | wc -l > 196 > $ git grep '? "" : "s"' | wc -l > 25 > > Additionally, there are some occurences of the same in reverse order, > split to multiple lines, or otherwise not caught by the simple grep. > > Add helpers to return the constant strings. Remove existing equivalent > and conflicting functions in i915, cxgb4, and USB core. Further > conversion can be done incrementally. > > The main goal here is to abstract recurring patterns, and slightly clean > up the code base by not open coding the ternary operators. Fair enough. > --- /dev/null > +++ b/include/linux/string-choice.h > @@ -0,0 +1,31 @@ > +/* SPDX-License-Identifier: MIT */ > +/* > + * Copyright © 2019 Intel Corporation > + */ > + > +#ifndef __STRING_CHOICE_H__ > +#define __STRING_CHOICE_H__ > + > +#include <linux/types.h> > + > +static inline const char *yesno(bool v) > +{ > + return v ? "yes" : "no"; > +} > + > +static inline const char *onoff(bool v) > +{ > + return v ? "on" : "off"; > +} > + > +static inline const char *enableddisabled(bool v) > +{ > + return v ? "enabled" : "disabled"; > +} > + > +static inline const char *plural(long v) > +{ > + return v == 1 ? "" : "s"; > +} > + > +#endif /* __STRING_CHOICE_H__ */ These aren't very good function names. Better to create a kernel-style namespace such as "choice_" and then add the expected underscores: choice_yes_no() choice_enabled_disabled() choice_plural() (Example: note that slabinfo.c already has an "onoff()"). Also, I worry that making these functions inline means that each .o file will contain its own copy of the strings ("yes", "no", "enabled", etc) if the .c file calls the relevant helper. I'm not sure if the linker is smart enough (yet) to fix this up. If not, we will end up with a smaller kernel by uninlining these functions. lib/string-choice.c would suit. And doing this will cause additional savings: calling a single-arg out-of-line function generates less .text than calling yesno(). When I did this: --- a/include/linux/string-choice.h~string-choice-add-yesno-onoff-enableddisabled-plural-helpers-fix +++ a/include/linux/string-choice.h @@ -8,10 +8,7 @@ #include <linux/types.h> -static inline const char *yesno(bool v) -{ - return v ? "yes" : "no"; -} +const char *yesno(bool v); static inline const char *onoff(bool v) { The text segment of drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.o (78 callsites) shrunk by 118 bytes. _______________________________________________ Intel-gfx mailing list Intel-gfx@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/intel-gfx
next prev parent reply other threads:[~2019-10-23 22:56 UTC|newest] Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-10-23 13:13 [PATCH v4] string-choice: add yesno(), onoff(), enableddisabled(), plural() helpers Jani Nikula 2019-10-23 13:13 ` [Intel-gfx] " Jani Nikula 2019-10-23 13:21 ` Rasmus Villemoes 2019-10-23 13:21 ` [Intel-gfx] " Rasmus Villemoes 2019-10-23 13:26 ` Jani Nikula 2019-10-23 13:26 ` [Intel-gfx] " Jani Nikula 2019-10-23 13:26 ` Jani Nikula 2019-10-23 22:56 ` Andrew Morton [this message] 2019-10-23 22:56 ` [Intel-gfx] " Andrew Morton 2019-10-23 23:46 ` Joe Perches 2019-10-23 23:46 ` [Intel-gfx] " Joe Perches 2019-10-24 7:32 ` Jani Nikula 2019-10-24 7:32 ` [Intel-gfx] " Jani Nikula 2019-10-24 7:32 ` Jani Nikula 2019-10-24 7:40 ` Rasmus Villemoes 2019-10-24 7:40 ` [Intel-gfx] " Rasmus Villemoes 2019-10-24 7:57 ` Rasmus Villemoes 2019-10-24 7:57 ` [Intel-gfx] " Rasmus Villemoes 2019-10-24 7:57 ` Rasmus Villemoes 2019-10-24 2:21 ` ✗ Fi.CI.BUILD: failure for string-choice: add yesno(), onoff(), enableddisabled(), plural() helpers (rev2) Patchwork 2019-10-24 2:21 ` [Intel-gfx] " Patchwork
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=20191023155619.43e0013f0c8c673a5c508c1e@linux-foundation.org \ --to=akpm@linux-foundation.org \ --cc=gregkh@linuxfoundation.org \ --cc=intel-gfx@lists.freedesktop.org \ --cc=jani.nikula@intel.com \ --cc=joonas.lahtinen@linux.intel.com \ --cc=julia.lawall@lip6.fr \ --cc=linux-kernel@vger.kernel.org \ --cc=linux-usb@vger.kernel.org \ --cc=linux@rasmusvillemoes.dk \ --cc=netdev@vger.kernel.org \ --cc=rodrigo.vivi@intel.com \ --cc=vishal@chelsio.com \ /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: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.