public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ipc/sem: Three function calls less in do_semtimedop()
@ 2019-07-06 12:28 Markus Elfring
  2019-07-06 20:13 ` Colin Ian King
  0 siblings, 1 reply; 4+ messages in thread
From: Markus Elfring @ 2019-07-06 12:28 UTC (permalink / raw)
  To: kernel-janitors, Andrew Morton, Arnd Bergmann, Davidlohr Bueso,
	Gustavo A. R. Silva, Manfred Spraul, Mathieu Malaterre
  Cc: LKML

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Sat, 6 Jul 2019 14:16:24 +0200

Avoid three function calls by using ternary operators instead of
conditional statements.

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 ipc/sem.c | 25 ++++++++-----------------
 1 file changed, 8 insertions(+), 17 deletions(-)

diff --git a/ipc/sem.c b/ipc/sem.c
index 7da4504bcc7c..56ea549ac270 100644
--- a/ipc/sem.c
+++ b/ipc/sem.c
@@ -2122,27 +2122,18 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
 		int idx = array_index_nospec(sops->sem_num, sma->sem_nsems);
 		curr = &sma->sems[idx];

-		if (alter) {
-			if (sma->complex_count) {
-				list_add_tail(&queue.list,
-						&sma->pending_alter);
-			} else {
-
-				list_add_tail(&queue.list,
-						&curr->pending_alter);
-			}
-		} else {
-			list_add_tail(&queue.list, &curr->pending_const);
-		}
+		list_add_tail(&queue.list,
+			      alter
+			      ? (sma->complex_count
+				? &sma->pending_alter
+				: &curr->pending_alter)
+			      : &curr->pending_const);
 	} else {
 		if (!sma->complex_count)
 			merge_queues(sma);

-		if (alter)
-			list_add_tail(&queue.list, &sma->pending_alter);
-		else
-			list_add_tail(&queue.list, &sma->pending_const);
-
+		list_add_tail(&queue.list,
+			      alter ? &sma->pending_alter : &sma->pending_const);
 		sma->complex_count++;
 	}

--
2.22.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] ipc/sem: Three function calls less in do_semtimedop()
  2019-07-06 12:28 [PATCH] ipc/sem: Three function calls less in do_semtimedop() Markus Elfring
@ 2019-07-06 20:13 ` Colin Ian King
  2019-07-06 20:50   ` Al Viro
  2019-07-07  7:09   ` Markus Elfring
  0 siblings, 2 replies; 4+ messages in thread
From: Colin Ian King @ 2019-07-06 20:13 UTC (permalink / raw)
  To: Markus Elfring, kernel-janitors, Andrew Morton, Arnd Bergmann,
	Davidlohr Bueso, Gustavo A. R. Silva, Manfred Spraul,
	Mathieu Malaterre
  Cc: LKML

On 06/07/2019 13:28, Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Sat, 6 Jul 2019 14:16:24 +0200
> 
> Avoid three function calls by using ternary operators instead of
> conditional statements.
> 
> This issue was detected by using the Coccinelle software.
> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---
>  ipc/sem.c | 25 ++++++++-----------------
>  1 file changed, 8 insertions(+), 17 deletions(-)
> 
> diff --git a/ipc/sem.c b/ipc/sem.c
> index 7da4504bcc7c..56ea549ac270 100644
> --- a/ipc/sem.c
> +++ b/ipc/sem.c
> @@ -2122,27 +2122,18 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
>  		int idx = array_index_nospec(sops->sem_num, sma->sem_nsems);
>  		curr = &sma->sems[idx];
> 
> -		if (alter) {
> -			if (sma->complex_count) {
> -				list_add_tail(&queue.list,
> -						&sma->pending_alter);
> -			} else {
> -
> -				list_add_tail(&queue.list,
> -						&curr->pending_alter);
> -			}
> -		} else {
> -			list_add_tail(&queue.list, &curr->pending_const);
> -		}
> +		list_add_tail(&queue.list,
> +			      alter
> +			      ? (sma->complex_count
> +				? &sma->pending_alter
> +				: &curr->pending_alter)
> +			      : &curr->pending_const);

Just no. This is making the code harder to comprehend with no advantage.
Compilers are smart, let the do the optimization work and keep code
simple for us mere mortals.

Colin

>  	} else {
>  		if (!sma->complex_count)
>  			merge_queues(sma);
> 
> -		if (alter)
> -			list_add_tail(&queue.list, &sma->pending_alter);
> -		else
> -			list_add_tail(&queue.list, &sma->pending_const);
> -
> +		list_add_tail(&queue.list,
> +			      alter ? &sma->pending_alter : &sma->pending_const);
>  		sma->complex_count++;
>  	}
> 
> --
> 2.22.0
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] ipc/sem: Three function calls less in do_semtimedop()
  2019-07-06 20:13 ` Colin Ian King
@ 2019-07-06 20:50   ` Al Viro
  2019-07-07  7:09   ` Markus Elfring
  1 sibling, 0 replies; 4+ messages in thread
From: Al Viro @ 2019-07-06 20:50 UTC (permalink / raw)
  To: Colin Ian King
  Cc: Markus Elfring, kernel-janitors, Andrew Morton, Arnd Bergmann,
	Davidlohr Bueso, Gustavo A. R. Silva, Manfred Spraul,
	Mathieu Malaterre, LKML

On Sat, Jul 06, 2019 at 09:13:46PM +0100, Colin Ian King wrote:
> On 06/07/2019 13:28, Markus Elfring wrote:
> > From: Markus Elfring <elfring@users.sourceforge.net>
> > Date: Sat, 6 Jul 2019 14:16:24 +0200
> > 
> > Avoid three function calls by using ternary operators instead of
> > conditional statements.
> > 
> > This issue was detected by using the Coccinelle software.
> > 
> > Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> > ---
> >  ipc/sem.c | 25 ++++++++-----------------
> >  1 file changed, 8 insertions(+), 17 deletions(-)
> > 
> > diff --git a/ipc/sem.c b/ipc/sem.c
> > index 7da4504bcc7c..56ea549ac270 100644
> > --- a/ipc/sem.c
> > +++ b/ipc/sem.c
> > @@ -2122,27 +2122,18 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
> >  		int idx = array_index_nospec(sops->sem_num, sma->sem_nsems);
> >  		curr = &sma->sems[idx];
> > 
> > -		if (alter) {
> > -			if (sma->complex_count) {
> > -				list_add_tail(&queue.list,
> > -						&sma->pending_alter);
> > -			} else {
> > -
> > -				list_add_tail(&queue.list,
> > -						&curr->pending_alter);
> > -			}
> > -		} else {
> > -			list_add_tail(&queue.list, &curr->pending_const);
> > -		}
> > +		list_add_tail(&queue.list,
> > +			      alter
> > +			      ? (sma->complex_count
> > +				? &sma->pending_alter
> > +				: &curr->pending_alter)
> > +			      : &curr->pending_const);
> 
> Just no. This is making the code harder to comprehend with no advantage.
> Compilers are smart, let the do the optimization work and keep code
> simple for us mere mortals.

If anything, that would've been better off as

		int idx = array_index_nospec(sops->sem_num, sma->sem_nsems);
		struct sem *curr = &sma->sems[idx];
		struct list_head *list;	/* which queue to sleep on */

		if (!alter)
			list = &curr->pending_const;
		else if (sma->complex_count)
			list = &sma->pending_alter;
		else
			list = &curr->pending_alter;

		list_add_tail(&queue.list, list);

perhaps with better identifier than 'list'.  This kind of ?: (ab)use makes
for unreadable code and more than makes up for "hey, we are adding to some
list in all those cases" extra information passed to readers...

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: ipc/sem: Three function calls less in do_semtimedop()
  2019-07-06 20:13 ` Colin Ian King
  2019-07-06 20:50   ` Al Viro
@ 2019-07-07  7:09   ` Markus Elfring
  1 sibling, 0 replies; 4+ messages in thread
From: Markus Elfring @ 2019-07-07  7:09 UTC (permalink / raw)
  To: Colin Ian King, kernel-janitors
  Cc: Andrew Morton, Arnd Bergmann, Davidlohr Bueso,
	Gustavo A. R. Silva, Manfred Spraul, Mathieu Malaterre, LKML

>> +		list_add_tail(&queue.list,
>> +			      alter
>> +			      ? (sma->complex_count
>> +				? &sma->pending_alter
>> +				: &curr->pending_alter)
>> +			      : &curr->pending_const);
>
> Just no. This is making the code harder to comprehend

This can be according to your current view.


> with no advantage.

I propose to take additional aspects into account for the interpretation
of such source code.
The shown design direction can provide benefits which might get
a lower value for the software development attention so far.


>> +		list_add_tail(&queue.list,
>> +			      alter ? &sma->pending_alter : &sma->pending_const);

Can this code variant look more succinct?

Regards,
Markus

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-07-07  7:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-07-06 12:28 [PATCH] ipc/sem: Three function calls less in do_semtimedop() Markus Elfring
2019-07-06 20:13 ` Colin Ian King
2019-07-06 20:50   ` Al Viro
2019-07-07  7:09   ` Markus Elfring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox