Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp2024732ybg; Thu, 24 Oct 2019 03:45:11 -0700 (PDT) X-Google-Smtp-Source: APXvYqyh00HVbNz4tKxcLIkCAfjEMXWHOJjc98ydbAMN4Uhy47bwu2s2XGBLJbHUSblppRzZerJb X-Received: by 2002:a17:906:e243:: with SMTP id gq3mr19420015ejb.302.1571913911790; Thu, 24 Oct 2019 03:45:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571913911; cv=none; d=google.com; s=arc-20160816; b=FxsmJgK+lTfEwFGAzhPATGlqe+Oo+WgMqdvEcYYu5UYEmaVOuMU7rzdDROcbqAKTLU MlJ0JVa4Gx+/l/qAgheY2N8i/8LitfII6MiIO6nTqQzoeAbRxY4bg6+gvuh8GIY0VHha 3TMaB9Wt+I30HjOOhyMfotHFNLlo5ENEbSWOu20yJ8GuqfyjqJyF2qoYU6iEPexK+yVV 3FFcbXy5jJKKQcCVbHuP+rnI2kQ9rCFkLN+pvD6n98NEDVS7H8MGubqY/xSspl+tTrtL rf4Hm/9IKsznYSz47hozpfeg03RK1PI6ZkXierejwavrSSWyhGG2x5bJ9f0dUU3Tz3fN 7ZWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=bMGZ+eOJEsrJHAeTEuNpnexmZMp63024FW7bqRrfeTU=; b=xcIAudoNX96aC0/0GhNyzWggaosqFcpBmO7mqAAsGWJ59loWp1YzJPSxJ9yQl9Gak4 GblbvgsQZ5s80Te3byDY4lz2ADxQiCa9oNvqHY8+FMl67Yki+UEhjk3Je/Os9RKzQb7k MTmW20DQZdRcs79pyXlNU+FWwBr0LV772DLT+IBk6eWiTzBiDpncvVCDQp4iCnEVd2Gd 5INsfNdoorhnTzqOa6w7wSr6knUByZWx1zGTi4qVeJ1lr/JUP6emdK11U8dzAObzTRp6 n50QRnVbKTxlx//im7YaC0AdY3aPi68nehdShfDkenu2LpwKBFAnI7GwFmDSFgPrvfHt Hb3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=NZLyOT+Z; 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 pw25si8429092ejb.168.2019.10.24.03.44.46; Thu, 24 Oct 2019 03:45:11 -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; dkim=pass header.i=@kernel.org header.s=default header.b=NZLyOT+Z; 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 S2392694AbfJWW4V (ORCPT + 99 others); Wed, 23 Oct 2019 18:56:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:60208 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727831AbfJWW4U (ORCPT ); Wed, 23 Oct 2019 18:56:20 -0400 Received: from akpm3.svl.corp.google.com (unknown [104.133.8.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 77AFA21929; Wed, 23 Oct 2019 22:56:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1571871379; bh=pH0FnxHuQ4uryY+5yag7Co6S+Z3nSAfsh/gAbdxNnLk=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=NZLyOT+ZqApYNjFfkvN4E7nfBNCZAjOdC/wzoETDChxjDRKMAXrf0iSDA2NpO1dAl n3jrJoabu0ncBYm24CEnLneZZFelnIqDG07Oa2FG2xVneQxq6qjLqnh4rNvt1Xk8HB diUUCetS09WEi0qR/QGP/DU2B2gikuUL57m32dSk= Date: Wed, 23 Oct 2019 15:56:19 -0700 From: Andrew Morton To: Jani Nikula Cc: linux-kernel@vger.kernel.org, Joonas Lahtinen , Rodrigo Vivi , intel-gfx@lists.freedesktop.org, Vishal Kulkarni , netdev@vger.kernel.org, Greg Kroah-Hartman , linux-usb@vger.kernel.org, Julia Lawall , Rasmus Villemoes Subject: Re: [PATCH v4] string-choice: add yesno(), onoff(), enableddisabled(), plural() helpers Message-Id: <20191023155619.43e0013f0c8c673a5c508c1e@linux-foundation.org> In-Reply-To: <20191023131308.9420-1-jani.nikula@intel.com> References: <20191023131308.9420-1-jani.nikula@intel.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 23 Oct 2019 16:13:08 +0300 Jani Nikula wrot= e: > The kernel has plenty of ternary operators to choose between constant > strings, such as condition ? "yes" : "no", as well as value =3D=3D 1 ? ""= : > "s": >=20 > $ 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 >=20 > Additionally, there are some occurences of the same in reverse order, > split to multiple lines, or otherwise not caught by the simple grep. >=20 > 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. >=20 > 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 =A9 2019 Intel Corporation > + */ > + > +#ifndef __STRING_CHOICE_H__ > +#define __STRING_CHOICE_H__ > + > +#include > + > +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 =3D=3D 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.=20 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:=20 --- a/include/linux/string-choice.h~string-choice-add-yesno-onoff-enableddi= sabled-plural-helpers-fix +++ a/include/linux/string-choice.h @@ -8,10 +8,7 @@ =20 #include =20 -static inline const char *yesno(bool v) -{ - return v ? "yes" : "no"; -} +const char *yesno(bool v); =20 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.