linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Dan Williams <dan.j.williams@intel.com>
Cc: "Huang, Ying" <ying.huang@intel.com>,
	David Hildenbrand <david@redhat.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	linux-cxl@vger.kernel.org, Davidlohr Bueso <dave@stgolabs.net>,
	Jonathan Cameron <jonathan.cameron@huawei.com>,
	Alistair Popple <apopple@nvidia.com>,
	Bjorn Helgaas <bhelgaas@google.com>, Baoquan He <bhe@redhat.com>,
	Dave Jiang <dave.jiang@intel.com>,
	Alison Schofield <alison.schofield@intel.com>
Subject: Re: [RFC] resource: Avoid unnecessary resource tree walking in __region_intersects()
Date: Fri, 25 Oct 2024 16:22:14 +0300	[thread overview]
Message-ID: <ZxubhuEwL5GrhBdu@smile.fi.intel.com> (raw)
In-Reply-To: <671ac2d2b7bea_10e59294f2@dwillia2-xfh.jf.intel.com.notmuch>

On Thu, Oct 24, 2024 at 02:57:38PM -0700, Dan Williams wrote:
> Andy Shevchenko wrote:
> > On Thu, Oct 24, 2024 at 08:30:39PM +0800, Huang, Ying wrote:
> > > Andy Shevchenko <andriy.shevchenko@linux.intel.com> writes:
> > > > On Wed, Oct 23, 2024 at 02:07:52PM -0700, Dan Williams wrote:
> > > >> Andy Shevchenko wrote:
> > > >> > On Fri, Oct 11, 2024 at 09:06:37AM +0800, Huang, Ying wrote:
> > > >> > > David Hildenbrand <david@redhat.com> writes:
> > > >> > > > On 10.10.24 08:55, Huang Ying wrote:

...

> > > >> > > > 	for ((_p) = (_root)->child; (_p); (_p) = next_resource_XXX(_root, _p))
> > > >> > > 
> > > >> > > Yes.  This can improve code readability.
> > > >> > > 
> > > >> > > A possible issue is that "_root" will be evaluated twice in above macro
> > > >> > > definition.  IMO, this should be avoided.
> > > >> > 
> > > >> > Ideally, yes. But how many for_each type of macros you see that really try hard
> > > >> > to achieve that? I believe we shouldn't worry right now about this and rely on
> > > >> > the fact that root is the given variable. Or do you have an example of what you
> > > >> > suggested in the other reply, i.e. where it's an evaluation of the heavy call?
> > > >> > 
> > > >> > > Do you have some idea about
> > > >> > > how to do that?  Something like below?
> > > >> > > 
> > > >> > > #define for_each_resource_XXX(_root, _p)                                \
> > > >> > > 	for (typeof(_root) __root = (_root), __p = (_p) = (__root)->child; \
> > > >> > > 	     __p && (_p); (_p) = next_resource_XXX(__root, _p))
> > > >> > 
> > > >> > This is a bit ugly :-( I would avoid ugliness as long as we have no problem to
> > > >> > solve (see above).
> > > >> 
> > > >> Using a local defined variable to avoid double evaluation is standard
> > > >> practice. I do not understand "avoid ugliness as long as we have no problem to
> > > >> solve", the problem to solve will be if someone accidentally does
> > > >> something like "for_each_resource_descendant(root++, res)". *That* will
> > > >> be a problem when someone finally realizes that the macro is hiding a
> > > >> double evaluation.
> > > >
> > > > Can you explain, why do we need __p and how can we get rid of that?
> > > > I understand the part of the local variable for root.
> > > 
> > > If don't use '__p', the macro becomes
> > > 
> > > #define for_each_resource_XXX(_root, _p)                                \
> > > 	for (typeof(_root) __root = (_root), (_p) = (__root)->child; \
> > > 	     (_p); (_p) = next_resource_XXX(__root, _p))
> > > 
> > > Where, '_p' must be a variable name, and it will be a new variable
> > > inside for loop and mask the variable with same name outside of macro.
> > > IIUC, this breaks the macro convention in kernel and has subtle variable
> > > masking semantics.
> > 
> > Yep.
> 
> Oh, due to the comment expression, good catch.
> 
> > In property.h nobody cares about evaluation which makes the macro as simple as
> > 
> > #define for_each_resource_XXX(_root, _p)		\
> > 	for (_p = next_resource_XXX(__root, NULL); _p;	\
> > 	     _p = next_resource_XXX(__root, _p))
> > 
> > (Dan,
> >  that's what I called to avoid solving issues we don't have and most likely
> >  will never have.)
> 
> Ah, my apologies, I thought the objection was to the macro altogether. 

No, no, I'm supporting the idea!

> > but if you want to stick with your variant some improvements can be done:
> > 
> > #define for_each_resource_XXX(_root, _p)				\
> > 	for (typeof(_root) __root = (_root), __p = _p = __root->child;	\
> > 	     __p && _p; _p = next_resource_XXX(__root, _p))
> > 
> > 
> > 1) no need to have local variable in parentheses;
> > 2) no need to have iterator in parentheses, otherwise it would be crazy code
> > that has put something really wrong there and still expect the thing to work.
> 
> Why not:
> 
> #define for_each_resource_XXX(_root, _p)				\
> 	for (typeof(_root) __root = (_root), __p = _p = __root->child;	\
> 	     _p; _p = next_resource_XXX(__root, _p))
> 
> The __p is only to allow for _p to be initialized in the first statement
> without causing a new "_p" shadow to be declared.

If people think this would be better than the existing patterns, okay. fine.

-- 
With Best Regards,
Andy Shevchenko




  parent reply	other threads:[~2024-10-25 13:22 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-10  6:55 Huang Ying
2024-10-10 12:54 ` David Hildenbrand
2024-10-11  1:06   ` Huang, Ying
2024-10-11  8:02     ` David Hildenbrand
2024-10-11  8:48       ` Huang, Ying
2024-10-11 10:51         ` Andy Shevchenko
2024-10-11 10:49     ` Andy Shevchenko
2024-10-11 10:51       ` David Hildenbrand
2024-10-11 11:15         ` Andy Shevchenko
2024-10-11 11:19           ` Andy Shevchenko
2024-10-11 11:30             ` David Hildenbrand
2024-10-11 13:21               ` Huang, Ying
2024-10-23 21:07       ` Dan Williams
2024-10-24  6:57         ` Andy Shevchenko
2024-10-24 12:30           ` Huang, Ying
2024-10-24 13:01             ` Andy Shevchenko
2024-10-24 21:57               ` Dan Williams
2024-10-25  0:31                 ` Huang, Ying
2024-10-25 13:22                 ` Andy Shevchenko [this message]
2024-10-25 15:14                   ` Dan Williams
2024-10-28  2:49                     ` Huang, Ying
2024-10-25  0:34               ` Huang, Ying

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=ZxubhuEwL5GrhBdu@smile.fi.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=alison.schofield@intel.com \
    --cc=apopple@nvidia.com \
    --cc=bhe@redhat.com \
    --cc=bhelgaas@google.com \
    --cc=dan.j.williams@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=dave@stgolabs.net \
    --cc=david@redhat.com \
    --cc=jonathan.cameron@huawei.com \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ying.huang@intel.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: 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