diff mbox series

[2/2] net: netrom: refactor code in nr_add_node

Message ID 20171019172746.GA15332@embeddedor.com
State Changes Requested, archived
Delegated to: David Miller
Headers show
Series [1/2] net: netrom: mark expected switch fall-throughs | expand

Commit Message

Gustavo A. R. Silva Oct. 19, 2017, 5:27 p.m. UTC
Code refactoring in order to make the code easier to read and maintain.

Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
---
This code was tested by compilation only (GCC 7.2.0 was used).

 net/netrom/nr_route.c | 63 ++++++++++++++++-----------------------------------
 1 file changed, 20 insertions(+), 43 deletions(-)

Comments

Walter Harms Oct. 20, 2017, 8:57 a.m. UTC | #1
Am 19.10.2017 19:27, schrieb Gustavo A. R. Silva:
> Code refactoring in order to make the code easier to read and maintain.
> 
> Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
> ---
> This code was tested by compilation only (GCC 7.2.0 was used).
> 
>  net/netrom/nr_route.c | 63 ++++++++++++++++-----------------------------------
>  1 file changed, 20 insertions(+), 43 deletions(-)
> 
> diff --git a/net/netrom/nr_route.c b/net/netrom/nr_route.c
> index fc9cadc..1e5165f 100644
> --- a/net/netrom/nr_route.c
> +++ b/net/netrom/nr_route.c
> @@ -80,6 +80,23 @@ static struct nr_neigh *nr_neigh_get_dev(ax25_address *callsign,
>  
>  static void nr_remove_neigh(struct nr_neigh *);
>  
> +/*      re-sort the routes in quality order.    */
> +static inline void re_sort_routes(struct nr_node *nr_node, int ix_x, int ix_y)
> +{
> +	struct nr_route nr_route;
> +
> +	if (nr_node->routes[ix_y].quality > nr_node->routes[ix_x].quality) {
> +		if (nr_node->which == ix_x)
> +			nr_node->which = ix_y;
> +		else if (nr_node->which == ix_y)
> +			nr_node->which = ix_x;
> +
> +		nr_route              = nr_node->routes[ix_x];
> +		nr_node->routes[ix_x] = nr_node->routes[ix_y];
> +		nr_node->routes[ix_y] = nr_route;
> +	}
> +}
> +


Good idea, a bit of nit picking ..
does ix_ has a special meaning ? otherwise x,y would be sufficient.
From the code below i can see: y=x+1 Perhaps that can be used.

kernel.h has a swap() macro. so you can swap(nr_node->routes[x],nr_node->routes[y]);

hope that helps,
re,
 wh

>  /*
>   *	Add a new route to a node, and in the process add the node and the
>   *	neighbour if it is new.
> @@ -90,7 +107,6 @@ static int __must_check nr_add_node(ax25_address *nr, const char *mnemonic,
>  {
>  	struct nr_node  *nr_node;
>  	struct nr_neigh *nr_neigh;
> -	struct nr_route nr_route;
>  	int i, found;
>  	struct net_device *odev;
>  
> @@ -251,50 +267,11 @@ static int __must_check nr_add_node(ax25_address *nr, const char *mnemonic,
>  	/* Now re-sort the routes in quality order */
>  	switch (nr_node->count) {
>  	case 3:
> -		if (nr_node->routes[1].quality > nr_node->routes[0].quality) {
> -			switch (nr_node->which) {
> -			case 0:
> -				nr_node->which = 1;
> -				break;
> -			case 1:
> -				nr_node->which = 0;
> -				break;
> -			}
> -			nr_route           = nr_node->routes[0];
> -			nr_node->routes[0] = nr_node->routes[1];
> -			nr_node->routes[1] = nr_route;
> -		}
> -		if (nr_node->routes[2].quality > nr_node->routes[1].quality) {
> -			switch (nr_node->which) {
> -			case 1:  nr_node->which = 2;
> -				break;
> -
> -			case 2:  nr_node->which = 1;
> -				break;
> -
> -			default:
> -				break;
> -			}
> -			nr_route           = nr_node->routes[1];
> -			nr_node->routes[1] = nr_node->routes[2];
> -			nr_node->routes[2] = nr_route;
> -		}
> +		re_sort_routes(nr_node, 0, 1);
> +		re_sort_routes(nr_node, 1, 2);
>  		/* fall through */
>  	case 2:
> -		if (nr_node->routes[1].quality > nr_node->routes[0].quality) {
> -			switch (nr_node->which) {
> -			case 0:  nr_node->which = 1;
> -				break;
> -
> -			case 1:  nr_node->which = 0;
> -				break;
> -
> -			default: break;
> -			}
> -			nr_route           = nr_node->routes[0];
> -			nr_node->routes[0] = nr_node->routes[1];
> -			nr_node->routes[1] = nr_route;
> -			}
> +		re_sort_routes(nr_node, 0, 1);
>  	case 1:
>  		break;
>  	}
Gustavo A. R. Silva Oct. 20, 2017, 4:06 p.m. UTC | #2
Hi Walter,

Quoting walter harms <wharms@bfs.de>:

> Am 19.10.2017 19:27, schrieb Gustavo A. R. Silva:
>> Code refactoring in order to make the code easier to read and maintain.
>>
>> Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
>> ---
>> This code was tested by compilation only (GCC 7.2.0 was used).
>>
>>  net/netrom/nr_route.c | 63  
>> ++++++++++++++++-----------------------------------
>>  1 file changed, 20 insertions(+), 43 deletions(-)
>>
>> diff --git a/net/netrom/nr_route.c b/net/netrom/nr_route.c
>> index fc9cadc..1e5165f 100644
>> --- a/net/netrom/nr_route.c
>> +++ b/net/netrom/nr_route.c
>> @@ -80,6 +80,23 @@ static struct nr_neigh  
>> *nr_neigh_get_dev(ax25_address *callsign,
>>
>>  static void nr_remove_neigh(struct nr_neigh *);
>>
>> +/*      re-sort the routes in quality order.    */
>> +static inline void re_sort_routes(struct nr_node *nr_node, int  
>> ix_x, int ix_y)
>> +{
>> +	struct nr_route nr_route;
>> +
>> +	if (nr_node->routes[ix_y].quality > nr_node->routes[ix_x].quality) {
>> +		if (nr_node->which == ix_x)
>> +			nr_node->which = ix_y;
>> +		else if (nr_node->which == ix_y)
>> +			nr_node->which = ix_x;
>> +
>> +		nr_route              = nr_node->routes[ix_x];
>> +		nr_node->routes[ix_x] = nr_node->routes[ix_y];
>> +		nr_node->routes[ix_y] = nr_route;
>> +	}
>> +}
>> +
>
>
> Good idea, a bit of nit picking ..
> does ix_ has a special meaning ? otherwise x,y would be sufficient.

ix typical stands for index, but I think just x and y are fine too.

> From the code below i can see: y=x+1 Perhaps that can be used.
>

So are you proposing to use two arguments instead of three?

re_sort_routes(nr_node, 0);


> kernel.h has a swap() macro. so you can  
> swap(nr_node->routes[x],nr_node->routes[y]);
>

Nice, I will use that macro.

> hope that helps,

Definitely. I appreciate your comments.

Thanks!
--
Gustavo A. R. Silva
Walter Harms Oct. 20, 2017, 4:54 p.m. UTC | #3
Am 20.10.2017 18:06, schrieb Gustavo A. R. Silva:
> Hi Walter,
> 
> Quoting walter harms <wharms@bfs.de>:
> 
>> Am 19.10.2017 19:27, schrieb Gustavo A. R. Silva:
>>> Code refactoring in order to make the code easier to read and maintain.
>>>
>>> Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
>>> ---
>>> This code was tested by compilation only (GCC 7.2.0 was used).
>>>
>>>  net/netrom/nr_route.c | 63
>>> ++++++++++++++++-----------------------------------
>>>  1 file changed, 20 insertions(+), 43 deletions(-)
>>>
>>> diff --git a/net/netrom/nr_route.c b/net/netrom/nr_route.c
>>> index fc9cadc..1e5165f 100644
>>> --- a/net/netrom/nr_route.c
>>> +++ b/net/netrom/nr_route.c
>>> @@ -80,6 +80,23 @@ static struct nr_neigh
>>> *nr_neigh_get_dev(ax25_address *callsign,
>>>
>>>  static void nr_remove_neigh(struct nr_neigh *);
>>>
>>> +/*      re-sort the routes in quality order.    */
>>> +static inline void re_sort_routes(struct nr_node *nr_node, int ix_x,
>>> int ix_y)
>>> +{
>>> +    struct nr_route nr_route;
>>> +
>>> +    if (nr_node->routes[ix_y].quality >
>>> nr_node->routes[ix_x].quality) {
>>> +        if (nr_node->which == ix_x)
>>> +            nr_node->which = ix_y;
>>> +        else if (nr_node->which == ix_y)
>>> +            nr_node->which = ix_x;
>>> +
>>> +        nr_route              = nr_node->routes[ix_x];
>>> +        nr_node->routes[ix_x] = nr_node->routes[ix_y];
>>> +        nr_node->routes[ix_y] = nr_route;
>>> +    }
>>> +}
>>> +
>>
>>
>> Good idea, a bit of nit picking ..
>> does ix_ has a special meaning ? otherwise x,y would be sufficient.
> 
> ix typical stands for index, but I think just x and y are fine too.
> 
>> From the code below i can see: y=x+1 Perhaps that can be used.
>>
> 
> So are you proposing to use two arguments instead of three?
> 
> re_sort_routes(nr_node, 0);
> 

I am not sure, i would wait a bit and see if what improves readability.
as Kevin Dawson pointed out: this is a sort here.
Maybe there a nice way to do something like that (i do not know):

case 3:
  re_sort_routes(nr_node, 1,2)
case 2:
  re_sort_routes(nr_node, 0,1)
case 1:
  break;

The question is: Is the sorted list needed or simply the maximum ?

NTL is a good thing to chop down the function in smaller digestible
peaces.

re,
 wh 	

> 
>> kernel.h has a swap() macro. so you can
>> swap(nr_node->routes[x],nr_node->routes[y]);
>>
> 
> Nice, I will use that macro.
> 
>> hope that helps,
> 
> Definitely. I appreciate your comments.
> 




> Thanks!
> -- 
> Gustavo A. R. Silva
> 
> 
> 
> 
> 
> 
>
Kevin Dawson Oct. 20, 2017, 11:09 p.m. UTC | #4
Hello all.

My original response just went to Gustavo and Walter because I'm not much of a kernel hacker these days; it was mainly observations that may or may not have been helpful and I'm not a regular list contributor.  Looks like I shouldn't have been so shy!

On Fri, Oct 20, 2017 at 06:54:05PM +0200, walter harms wrote:
> 
> ...
> 
> >> From the code below i can see: y=x+1 Perhaps that can be used.
> > 
> > So are you proposing to use two arguments instead of three?
> > 
> > re_sort_routes(nr_node, 0);
> 
> I am not sure, i would wait a bit and see if what improves readability.
> as Kevin Dawson pointed out: this is a sort here.
> Maybe there a nice way to do something like that (i do not know):
> 
> case 3:
>   re_sort_routes(nr_node, 1,2)
> case 2:
>   re_sort_routes(nr_node, 0,1)
> case 1:
>   break;

Nearly - you left out one of the calls to re_sort_routes.  I include my original mail here, as it explains it better:

> Hi Walter and Gustavo.
> 
> I've just left this response for the two of you, as it's a long time since I've been fiddling in the kernel and I don't know much of the current ideology regarding how people want their coding done.
> 
> I also only have a passing knowledge of NetRom, but I do recall some of the principles like routes and their quality.
> 
> On Fri, Oct 20, 2017 at 10:57:10AM +0200, walter harms wrote:
> > 
> > Am 19.10.2017 19:27, schrieb Gustavo A. R. Silva:
> > > Code refactoring in order to make the code easier to read and maintain.
> 
> ...
> 
> [ Gustavo ]
> > > +/*      re-sort the routes in quality order.    */
> > > +static inline void re_sort_routes(struct nr_node *nr_node, int ix_x, int ix_y)
> 
> I'm curious as to why re_sort_routes() is declared inline.  Its use below hardly gains any efficiency from saving function calls; as well, it's extra code space taken.
> 
> > > +		if (nr_node->which == ix_x)
> > > +			nr_node->which = ix_y;
> > > +		else if (nr_node->which == ix_y)
> > > +			nr_node->which = ix_x;
> 
> I suspect this can be simplified too, but I don't know what the possible values for ->which might be in any particular route case.
> 
> ...
> 
> [ Walter ]
> > From the code below i can see: y=x+1 Perhaps that can be used.
> > ...
> > >  	/* Now re-sort the routes in quality order */
> > >  	switch (nr_node->count) {
> > >  	case 3:
> > >  		re_sort_routes(nr_node, 0, 1);
> > >  		re_sort_routes(nr_node, 1, 2);
> > >  		/* fall through */
> > >  	case 2:
> > >  		re_sort_routes(nr_node, 0, 1);
> > >  	case 1:
> > >  		break;
> > >  	}
> 
> Yes, it can and it makes sense to do so.  The algorithm is an unrolled bubblesort for up to 3 routes.  If 3 is as far as it will ever go (and given that's all the original function allowed for), it's not unjustified in leaving it that way.  Anything more and I'd be suggesting it be made a loop.
> 
> > kernel.h has a swap() macro. so you can swap(nr_node->routes[x],nr_node->routes[y]);
> 
> I presume the swap() macro handles arbitrary types - structs and not just ints.  If the aim is to improve readability of the code, it helps.
> 
> I hope I'm not completely out of line in suggesting this.  As I say, it's been quite a while since I've dabbled in the kernel (although I did start back in the 1970s on Unix...).
> 
> Thanks,
> Kevin
Gustavo A. R. Silva Oct. 23, 2017, 12:41 a.m. UTC | #5
Quoting walter harms <wharms@bfs.de>:

> Am 20.10.2017 18:06, schrieb Gustavo A. R. Silva:
>> Hi Walter,
>>
>> Quoting walter harms <wharms@bfs.de>:
>>
>>> Am 19.10.2017 19:27, schrieb Gustavo A. R. Silva:
>>>> Code refactoring in order to make the code easier to read and maintain.
>>>>
>>>> Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
>>>> ---
>>>> This code was tested by compilation only (GCC 7.2.0 was used).
>>>>
>>>>  net/netrom/nr_route.c | 63
>>>> ++++++++++++++++-----------------------------------
>>>>  1 file changed, 20 insertions(+), 43 deletions(-)
>>>>
>>>> diff --git a/net/netrom/nr_route.c b/net/netrom/nr_route.c
>>>> index fc9cadc..1e5165f 100644
>>>> --- a/net/netrom/nr_route.c
>>>> +++ b/net/netrom/nr_route.c
>>>> @@ -80,6 +80,23 @@ static struct nr_neigh
>>>> *nr_neigh_get_dev(ax25_address *callsign,
>>>>
>>>>  static void nr_remove_neigh(struct nr_neigh *);
>>>>
>>>> +/*      re-sort the routes in quality order.    */
>>>> +static inline void re_sort_routes(struct nr_node *nr_node, int ix_x,
>>>> int ix_y)
>>>> +{
>>>> +    struct nr_route nr_route;
>>>> +
>>>> +    if (nr_node->routes[ix_y].quality >
>>>> nr_node->routes[ix_x].quality) {
>>>> +        if (nr_node->which == ix_x)
>>>> +            nr_node->which = ix_y;
>>>> +        else if (nr_node->which == ix_y)
>>>> +            nr_node->which = ix_x;
>>>> +
>>>> +        nr_route              = nr_node->routes[ix_x];
>>>> +        nr_node->routes[ix_x] = nr_node->routes[ix_y];
>>>> +        nr_node->routes[ix_y] = nr_route;
>>>> +    }
>>>> +}
>>>> +
>>>
>>>
>>> Good idea, a bit of nit picking ..
>>> does ix_ has a special meaning ? otherwise x,y would be sufficient.
>>
>> ix typical stands for index, but I think just x and y are fine too.
>>
>>> From the code below i can see: y=x+1 Perhaps that can be used.
>>>
>>
>> So are you proposing to use two arguments instead of three?
>>
>> re_sort_routes(nr_node, 0);
>>
>
> I am not sure, i would wait a bit and see if what improves readability.
> as Kevin Dawson pointed out: this is a sort here.
> Maybe there a nice way to do something like that (i do not know):
>
> case 3:
>   re_sort_routes(nr_node, 1,2)
> case 2:
>   re_sort_routes(nr_node, 0,1)
> case 1:
>   break;
>
> The question is: Is the sorted list needed or simply the maximum ?
>
> NTL is a good thing to chop down the function in smaller digestible
> peaces.
>

I'll use the swap macro as you suggested and leave the rest of this  
patch as is for now.

Dawson:
Thanks for pointing out the thing about the inline keyword. I'll remove it.

Thanks
--
Gustavo A. R. Silva

> re,
>  wh
>
>>
>>> kernel.h has a swap() macro. so you can
>>> swap(nr_node->routes[x],nr_node->routes[y]);
>>>
>>
>> Nice, I will use that macro.
>>
>>> hope that helps,
>>
>> Definitely. I appreciate your comments.
>>
>
>
>
>
>> Thanks!
>> --
>> Gustavo A. R. Silva
>>
>>
>>
>>
>>
>>
>>
diff mbox series

Patch

diff --git a/net/netrom/nr_route.c b/net/netrom/nr_route.c
index fc9cadc..1e5165f 100644
--- a/net/netrom/nr_route.c
+++ b/net/netrom/nr_route.c
@@ -80,6 +80,23 @@  static struct nr_neigh *nr_neigh_get_dev(ax25_address *callsign,
 
 static void nr_remove_neigh(struct nr_neigh *);
 
+/*      re-sort the routes in quality order.    */
+static inline void re_sort_routes(struct nr_node *nr_node, int ix_x, int ix_y)
+{
+	struct nr_route nr_route;
+
+	if (nr_node->routes[ix_y].quality > nr_node->routes[ix_x].quality) {
+		if (nr_node->which == ix_x)
+			nr_node->which = ix_y;
+		else if (nr_node->which == ix_y)
+			nr_node->which = ix_x;
+
+		nr_route              = nr_node->routes[ix_x];
+		nr_node->routes[ix_x] = nr_node->routes[ix_y];
+		nr_node->routes[ix_y] = nr_route;
+	}
+}
+
 /*
  *	Add a new route to a node, and in the process add the node and the
  *	neighbour if it is new.
@@ -90,7 +107,6 @@  static int __must_check nr_add_node(ax25_address *nr, const char *mnemonic,
 {
 	struct nr_node  *nr_node;
 	struct nr_neigh *nr_neigh;
-	struct nr_route nr_route;
 	int i, found;
 	struct net_device *odev;
 
@@ -251,50 +267,11 @@  static int __must_check nr_add_node(ax25_address *nr, const char *mnemonic,
 	/* Now re-sort the routes in quality order */
 	switch (nr_node->count) {
 	case 3:
-		if (nr_node->routes[1].quality > nr_node->routes[0].quality) {
-			switch (nr_node->which) {
-			case 0:
-				nr_node->which = 1;
-				break;
-			case 1:
-				nr_node->which = 0;
-				break;
-			}
-			nr_route           = nr_node->routes[0];
-			nr_node->routes[0] = nr_node->routes[1];
-			nr_node->routes[1] = nr_route;
-		}
-		if (nr_node->routes[2].quality > nr_node->routes[1].quality) {
-			switch (nr_node->which) {
-			case 1:  nr_node->which = 2;
-				break;
-
-			case 2:  nr_node->which = 1;
-				break;
-
-			default:
-				break;
-			}
-			nr_route           = nr_node->routes[1];
-			nr_node->routes[1] = nr_node->routes[2];
-			nr_node->routes[2] = nr_route;
-		}
+		re_sort_routes(nr_node, 0, 1);
+		re_sort_routes(nr_node, 1, 2);
 		/* fall through */
 	case 2:
-		if (nr_node->routes[1].quality > nr_node->routes[0].quality) {
-			switch (nr_node->which) {
-			case 0:  nr_node->which = 1;
-				break;
-
-			case 1:  nr_node->which = 0;
-				break;
-
-			default: break;
-			}
-			nr_route           = nr_node->routes[0];
-			nr_node->routes[0] = nr_node->routes[1];
-			nr_node->routes[1] = nr_route;
-			}
+		re_sort_routes(nr_node, 0, 1);
 	case 1:
 		break;
 	}