diff mbox series

[v1,1/1] drm/i915/selftests: Replace too verbose for-loop with simpler while

Message ID 20220215163213.54917-1-andriy.shevchenko@linux.intel.com (mailing list archive)
State New, archived
Headers show
Series [v1,1/1] drm/i915/selftests: Replace too verbose for-loop with simpler while | expand

Commit Message

Andy Shevchenko Feb. 15, 2022, 4:32 p.m. UTC
It's hard to parse for-loop which has some magic calculations inside.
Much cleaner to use while-loop directly.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/gpu/drm/i915/selftests/i915_syncmap.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Jani Nikula Feb. 15, 2022, 5:14 p.m. UTC | #1
On Tue, 15 Feb 2022, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> It's hard to parse for-loop which has some magic calculations inside.
> Much cleaner to use while-loop directly.

I assume you're trying to prove a point following our recent
for-vs-while loop discussion. I really can't think of any other reason
you'd end up looking at this file or this loop.

With the change, the loop indeed becomes simpler, but it also runs one
iteration further than the original. Whoops.

It's a selftest. The loop's been there for five years. What are we
trying to achieve here? So we disagree on loops, fine. Perhaps this is
not the best use of either of our time? Please just let the for loops in
i915 be.


BR,
Jani.

>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  drivers/gpu/drm/i915/selftests/i915_syncmap.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpu/drm/i915/selftests/i915_syncmap.c b/drivers/gpu/drm/i915/selftests/i915_syncmap.c
> index 47f4ae18a1ef..26981d1c3025 100644
> --- a/drivers/gpu/drm/i915/selftests/i915_syncmap.c
> +++ b/drivers/gpu/drm/i915/selftests/i915_syncmap.c
> @@ -36,10 +36,10 @@ __sync_print(struct i915_syncmap *p,
>  	unsigned int i, X;
>  
>  	if (depth) {
> -		unsigned int d;
> +		unsigned int d = depth;
>  
> -		for (d = 0; d < depth - 1; d++) {
> -			if (last & BIT(depth - d - 1))
> +		while (d--) {
> +			if (last & BIT(d))
>  				len = scnprintf(buf, *sz, "|   ");
>  			else
>  				len = scnprintf(buf, *sz, "    ");
Andy Shevchenko Feb. 15, 2022, 6:58 p.m. UTC | #2
On Tue, Feb 15, 2022 at 07:14:49PM +0200, Jani Nikula wrote:
> On Tue, 15 Feb 2022, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> > It's hard to parse for-loop which has some magic calculations inside.
> > Much cleaner to use while-loop directly.
> 
> I assume you're trying to prove a point following our recent
> for-vs-while loop discussion. I really can't think of any other reason
> you'd end up looking at this file or this loop.
> 
> With the change, the loop indeed becomes simpler, but it also runs one
> iteration further than the original. Whoops.

Yeah, sorry for that, the initial condition should be d = depth - 1,
of course.

> It's a selftest. The loop's been there for five years. What are we
> trying to achieve here? So we disagree on loops, fine. Perhaps this is
> not the best use of either of our time? Please just let the for loops in
> i915 be.

Yes, I'm pretty much was sure that no-one will go and apply this anyway
(so I haven't paid too much attention), but to prove my point in the
certain discussion.

And yes, the point is for the new code, I'm not going to change existing
suboptimal and too hard to read for-loops, it will consume my time later
when I will try to understand the code.
Jani Nikula Feb. 16, 2022, 8:55 a.m. UTC | #3
On Tue, 15 Feb 2022, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> On Tue, Feb 15, 2022 at 07:14:49PM +0200, Jani Nikula wrote:
>> On Tue, 15 Feb 2022, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
>> > It's hard to parse for-loop which has some magic calculations inside.
>> > Much cleaner to use while-loop directly.
>> 
>> I assume you're trying to prove a point following our recent
>> for-vs-while loop discussion. I really can't think of any other reason
>> you'd end up looking at this file or this loop.
>> 
>> With the change, the loop indeed becomes simpler, but it also runs one
>> iteration further than the original. Whoops.
>
> Yeah, sorry for that, the initial condition should be d = depth - 1,
> of course.

Well, no, the condition should be while (--i) instead to also match the
values the original loop takes. ;D

Cheers,
Jani.


>
>> It's a selftest. The loop's been there for five years. What are we
>> trying to achieve here? So we disagree on loops, fine. Perhaps this is
>> not the best use of either of our time? Please just let the for loops in
>> i915 be.
>
> Yes, I'm pretty much was sure that no-one will go and apply this anyway
> (so I haven't paid too much attention), but to prove my point in the
> certain discussion.
>
> And yes, the point is for the new code, I'm not going to change existing
> suboptimal and too hard to read for-loops, it will consume my time later
> when I will try to understand the code.
Geert Uytterhoeven Feb. 16, 2022, 9:02 a.m. UTC | #4
On Wed, Feb 16, 2022 at 9:55 AM Jani Nikula <jani.nikula@linux.intel.com> wrote:
> On Tue, 15 Feb 2022, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> > On Tue, Feb 15, 2022 at 07:14:49PM +0200, Jani Nikula wrote:
> >> On Tue, 15 Feb 2022, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> >> > It's hard to parse for-loop which has some magic calculations inside.
> >> > Much cleaner to use while-loop directly.
> >>
> >> I assume you're trying to prove a point following our recent
> >> for-vs-while loop discussion. I really can't think of any other reason
> >> you'd end up looking at this file or this loop.
> >>
> >> With the change, the loop indeed becomes simpler, but it also runs one
> >> iteration further than the original. Whoops.
> >
> > Yeah, sorry for that, the initial condition should be d = depth - 1,
> > of course.
>
> Well, no, the condition should be while (--i) instead to also match the
> values the original loop takes. ;D

"There are two hard things in computer science: cache invalidation,
 naming things, and off-by-one errors."

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
diff mbox series

Patch

diff --git a/drivers/gpu/drm/i915/selftests/i915_syncmap.c b/drivers/gpu/drm/i915/selftests/i915_syncmap.c
index 47f4ae18a1ef..26981d1c3025 100644
--- a/drivers/gpu/drm/i915/selftests/i915_syncmap.c
+++ b/drivers/gpu/drm/i915/selftests/i915_syncmap.c
@@ -36,10 +36,10 @@  __sync_print(struct i915_syncmap *p,
 	unsigned int i, X;
 
 	if (depth) {
-		unsigned int d;
+		unsigned int d = depth;
 
-		for (d = 0; d < depth - 1; d++) {
-			if (last & BIT(depth - d - 1))
+		while (d--) {
+			if (last & BIT(d))
 				len = scnprintf(buf, *sz, "|   ");
 			else
 				len = scnprintf(buf, *sz, "    ");