tipc: Use bsearch library function
Subsystems (3)
PATCH 1/1 tipc: Use bsearch library function
Use common library function rather than explicitly coding some variant of it yourself.
--- net/tipc/name_table.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-)
net/tipc/name_table.c +15 -15
@@ -44,6 +44,7 @@#include "addr.h" #include "node.h" #include <net/genetlink.h> +#include <linux/bsearch.h> #define TIPC_NAMETBL_SIZE 1024/* must be a power of 2 */
@@ -168,6 +169,18 @@ static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_heareturn nseq; } +static int nameseq_find_subseq_cmp (const void *key,const void *elt) +{ +u32 instance = *(u32 *)key; +struct sub_seq *sseq = (struct sub_seq *)elt; + +if (instance < sseq->lower) +return -1 ; +else if (instance > sseq->upper) +return 1 ; +return 0 ; +} +/** * nameseq_find_subseq - find sub-sequence (if any) matching a name instance *
@@ -176,21 +189,8 @@ static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_heastatic struct sub_seq *nameseq_find_subseq (struct name_seq *nseq,u32 instance) { -struct sub_seq *sseqs = nseq->sseqs; -int low =0 ; -int high = nseq->first_free -1 ; -int mid; - -while (low <= high) { - mid = (low + high) /2 ; -if (instance < sseqs[mid].lower) - high = mid -1 ; -else if (instance > sseqs[mid].upper) - low = mid +1 ; -else -return &sseqs[mid]; - } -return NULL ; +return bsearch (&instance, nseq->sseqs, nseq->first_free, +sizeof (struct sub_seq ), nameseq_find_subseq_cmp); }/**
Signature · 1 line
2.11.0
David Miller
maintainerFrom: Thomas Meyer <[email protected]>
Date: Sat, 9 Sep 2017 05:18:19 +0200
net/tipc/name_table.c static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_hea · line 175
Quoted 8 lines
net/tipc/name_table.c +4
-0@@ -168,6 +169,18 @@ static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_heareturn nseq; } +static int nameseq_find_subseq_cmp (const void *key,const void *elt) +{ +u32 instance = *(u32 *)key; +struct sub_seq *sseq = (struct sub_seq *)elt;
Please order local variables from longest to shortest (ie. reverse christmas tree).
Thank you.
From: David Miller
Quoted 9 lines
Sent: 11 September 2017 22:30
From: Thomas Meyer <[email protected]>
Date: Sat, 9 Sep 2017 05:18:19 +0200Review static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head
Quoted 1 line
(quoted diff) +0
-0@@ -168,6 +169,18 @@ static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head
*seq_heaQuoted 3 lines
net/tipc/name_table.c +0
-0return nseq; }
Quoted 7 lines
net/tipc/name_table.c static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_hea · line 175
Quoted 4 lines
net/tipc/name_table.c +4
-0+static int nameseq_find_subseq_cmp (const void *key,const void *elt) +{ +u32 instance = *(u32 *)key; +struct sub_seq *sseq = (struct sub_seq *)elt;Please order local variables from longest to shortest (ie. reverse christmas tree).
You probably just need to remove the unnecessary cast of 'void *'. Although adding the 'const' qualifier will make it wrong again.
You probably ought to make the 'key' a structure - even if it only contains a single u32.
Casting pointers to numeric types is often wrong.
David
PATCH v2 1/1 tipc: Use bsearch library function
In reply to David MillerUse common library function rather than explicitly coding some variant of it yourself.
--- net/tipc/name_table.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-)
V2: Coding style
net/tipc/name_table.c +15 -15
@@ -44,6 +44,7 @@#include "addr.h" #include "node.h" #include <net/genetlink.h> +#include <linux/bsearch.h> #define TIPC_NAMETBL_SIZE 1024/* must be a power of 2 */
@@ -168,6 +169,18 @@ static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_heareturn nseq; } +static int nameseq_find_subseq_cmp (const void *key,const void *elt) +{ +struct sub_seq *sseq = (struct sub_seq *)elt; +u32 instance = *(u32 *)key; + +if (instance < sseq->lower) +return -1 ; +else if (instance > sseq->upper) +return 1 ; +return 0 ; +} +/** * nameseq_find_subseq - find sub-sequence (if any) matching a name instance *
@@ -176,21 +189,8 @@ static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_heastatic struct sub_seq *nameseq_find_subseq (struct name_seq *nseq,u32 instance) { -struct sub_seq *sseqs = nseq->sseqs; -int low =0 ; -int high = nseq->first_free -1 ; -int mid; - -while (low <= high) { - mid = (low + high) /2 ; -if (instance < sseqs[mid].lower) - high = mid -1 ; -else if (instance > sseqs[mid].upper) - low = mid +1 ; -else -return &sseqs[mid]; - } -return NULL ; +return bsearch (&instance, nseq->sseqs, nseq->first_free, +sizeof (struct sub_seq ), nameseq_find_subseq_cmp); }/**
Signature · 1 line
2.11.0
Quoted 4 lines · Thomas Meyer
Use common library function rather than explicitly coding some variant of it yourself.
Signed-off-by Thomas Meyer <[email protected]>
Quoted 62 lines
--- net/tipc/name_table.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-)V2: Coding style
net/tipc/name_table.c +15
-15@@ -44,6 +44,7 @@#include "addr.h" #include "node.h" #include <net/genetlink.h> +#include <linux/bsearch.h> #define TIPC_NAMETBL_SIZE 1024/* must be a power of 2 */ @@ -168,6 +169,18 @@ static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_heareturn nseq; } +static int nameseq_find_subseq_cmp (const void *key,const void *elt) +{ +struct sub_seq *sseq = (struct sub_seq *)elt; +u32 instance = *(u32 *)key; + +if (instance < sseq->lower) +return -1 ; +else if (instance > sseq->upper) +return 1 ; +return 0 ; +} +/** * nameseq_find_subseq - find sub-sequence (if any) matching a name instance * @@ -176,21 +189,8 @@ static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_heastatic struct sub_seq *nameseq_find_subseq (struct name_seq *nseq,u32 instance) { -struct sub_seq *sseqs = nseq->sseqs; -int low =0 ; -int high = nseq->first_free -1 ; -int mid; - -while (low <= high) { - mid = (low + high) /2 ; -if (instance < sseqs[mid].lower) - high = mid -1 ; -else if (instance > sseqs[mid].upper) - low = mid +1 ; -else -return &sseqs[mid]; - } -return NULL ; +return bsearch (&instance, nseq->sseqs, nseq->first_free, +sizeof (struct sub_seq ), nameseq_find_subseq_cmp); }/**
Quoted 7 lines · Ying Xue
Quoted 4 lines · Thomas Meyer
Use common library function rather than explicitly coding some variant of it yourself.
Signed-off-by Thomas Meyer <[email protected]>Acked-by Ying Xue <[email protected]>
Are you sure you want to do this?
Note the comment above nameseq_find_subseq
* Very time-critical, so binary searches through sub-sequence array.
What impact does this change have on performance?
Quoted 56 lines
Quoted 56 lines
net/tipc/name_table.c +15
-15@@ -44,6 +44,7 @@#include "addr.h" #include "node.h" #include <net/genetlink.h> +#include <linux/bsearch.h> #define TIPC_NAMETBL_SIZE 1024/* must be a power of 2 */ @@ -168,6 +169,18 @@ static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_heareturn nseq; } +static int nameseq_find_subseq_cmp (const void *key,const void *elt) +{ +struct sub_seq *sseq = (struct sub_seq *)elt; +u32 instance = *(u32 *)key; + +if (instance < sseq->lower) +return -1 ; +else if (instance > sseq->upper) +return 1 ; +return 0 ; +} +/** * nameseq_find_subseq - find sub-sequence (if any) matching a name instance * @@ -176,21 +189,8 @@ static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_heastatic struct sub_seq *nameseq_find_subseq (struct name_seq *nseq,u32 instance) { -struct sub_seq *sseqs = nseq->sseqs; -int low =0 ; -int high = nseq->first_free -1 ; -int mid; - -while (low <= high) { - mid = (low + high) /2 ; -if (instance < sseqs[mid].lower) - high = mid -1 ; -else if (instance > sseqs[mid].upper) - low = mid +1 ; -else -return &sseqs[mid]; - } -return NULL ; +return bsearch (&instance, nseq->sseqs, nseq->first_free, +sizeof (struct sub_seq ), nameseq_find_subseq_cmp); }/**
Quoted 9 lines · Joe Perches
Quoted 7 lines · Ying Xue
Quoted 4 lines · Thomas Meyer
Use common library function rather than explicitly coding some variant of it yourself.
Signed-off-by Thomas Meyer <[email protected]>Acked-by Ying Xue <[email protected]>
Quoted 7 lines
Are you sure you want to do this?
Note the comment above
nameseq_find_subseq* Very time-critical, so binary searches through sub-sequence array.
What impact does this change have on performance?
Sorry, I couldn't see any essential difference between this new implementation and the original one except that the former tries to use the library function - bsearch() to replace the original binary search algorithm implemented in TIPC itself. Therefore, I don't think the change will have a big impact on performance.
If I miss something, please let me know.
Thanks,
Ying
Quoted 58 lines
Quoted 57 lines
Quoted 57 lines
net/tipc/name_table.c +15
-15@@ -44,6 +44,7 @@#include "addr.h" #include "node.h" #include <net/genetlink.h> +#include <linux/bsearch.h> #define TIPC_NAMETBL_SIZE 1024/* must be a power of 2 */ @@ -168,6 +169,18 @@ static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_heareturn nseq; } +static int nameseq_find_subseq_cmp (const void *key,const void *elt) +{ +struct sub_seq *sseq = (struct sub_seq *)elt; +u32 instance = *(u32 *)key; + +if (instance < sseq->lower) +return -1 ; +else if (instance > sseq->upper) +return 1 ; +return 0 ; +} +/** * nameseq_find_subseq - find sub-sequence (if any) matching a name instance * @@ -176,21 +189,8 @@ static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_heastatic struct sub_seq *nameseq_find_subseq (struct name_seq *nseq,u32 instance) { -struct sub_seq *sseqs = nseq->sseqs; -int low =0 ; -int high = nseq->first_free -1 ; -int mid; - -while (low <= high) { - mid = (low + high) /2 ; -if (instance < sseqs[mid].lower) - high = mid -1 ; -else if (instance > sseqs[mid].upper) - low = mid +1 ; -else -return &sseqs[mid]; - } -return NULL ; +return bsearch (&instance, nseq->sseqs, nseq->first_free, +sizeof (struct sub_seq ), nameseq_find_subseq_cmp); }/**
Quoted 18 lines · Ying Xue
Quoted 9 lines · Joe Perches
Quoted 7 lines · Ying Xue
Quoted 4 lines · Thomas Meyer
Use common library function rather than explicitly coding some variant of it yourself.
Signed-off-by Thomas Meyer <[email protected]>Acked-by Ying Xue <[email protected]>Quoted 7 lines
Are you sure you want to do this?
Note the comment above
nameseq_find_subseq* Very time-critical, so binary searches through sub-sequence array.
What impact does this change have on performance?
Quoted 7 lines
Sorry, I couldn't see any essential difference between this new implementation and the original one except that the former tries to use the library function -
bsearch()to replace the original binary search algorithm implemented in TIPC itself. Therefore, I don't think the change will have a big impact on performance.If I miss something, please let me know.
Comparison via a function pointer in bsearch is slower than direct code without the function call overhead.
Quoted 25 lines · Joe Perches
Quoted 24 lines · Ying Xue
Quoted 9 lines · Joe Perches
Quoted 7 lines · Ying Xue
Quoted 4 lines · Thomas Meyer
Use common library function rather than explicitly coding some variant of it yourself.
Signed-off-by Thomas Meyer <[email protected]>Acked-by Ying Xue <[email protected]>Quoted 7 lines
Are you sure you want to do this?
Note the comment above
nameseq_find_subseq* Very time-critical, so binary searches through sub-sequence array.
What impact does this change have on performance?
Sorry, I couldn't see any essential difference between this new implementation and the original one except that the former tries to use the library function -
bsearch()to replace the original binary search algorithm implemented in TIPC itself. Therefore, I don't think the change will have a big impact on performance.
Quoted 5 lines
Quoted 1 line
If I miss something, please let me know.
Comparison via a function pointer in bsearch is slower than direct code without the function call overhead.
Right, but probably we can tolerate the slight sacrifice here.
Quoted 1 line
Quoted 26 lines · Ying Xue
Quoted 25 lines · Joe Perches
Quoted 24 lines · Ying Xue
Quoted 9 lines · Joe Perches
Quoted 7 lines · Ying Xue
Quoted 4 lines · Thomas Meyer
Use common library function rather than explicitly coding some variant of it yourself.
Signed-off-by Thomas Meyer <[email protected]>Acked-by Ying Xue <[email protected]>Quoted 7 lines
Are you sure you want to do this?
Note the comment above
nameseq_find_subseq* Very time-critical, so binary searches through sub-sequence array.
What impact does this change have on performance?
Sorry, I couldn't see any essential difference between this new implementation and the original one except that the former tries to use the library function -
bsearch()to replace the original binary search algorithm implemented in TIPC itself. Therefore, I don't think the change will have a big impact on performance.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
If I miss something, please let me know.
Comparison via a function pointer in bsearch is slower than direct code without the function call overhead.
Right, but probably we can tolerate the slight sacrifice here.
What part of "very time critical" have you verified and benchmarked as inconsequential?
Please post your results.
Quoted 45 lines
-----Original Message-----
From: [email protected] [mailto:netdev- [email protected]] On Behalf Of Joe Perches
Sent: Saturday, September 16, 2017 06:18
To: Ying Xue <[email protected]>; Thomas Meyer <[email protected]>; Jon Maloy <[email protected]>; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH V2] tipc: Use bsearch library functionQuoted 32 lines · Ying Xue
Quoted 30 lines · Joe Perches
Quoted 18 lines · Ying Xue
Quoted 9 lines · Joe Perches
Quoted 7 lines · Ying Xue
Quoted 4 lines · Thomas Meyer
Use common library function rather than explicitly coding some variant of it yourself.
Signed-off-by Thomas Meyer <[email protected]>Acked-by Ying Xue <[email protected]>Quoted 7 lines
Are you sure you want to do this?
Note the comment above
nameseq_find_subseq* Very time-critical, so binary searches through sub-sequence array.
What impact does this change have on performance?
Quoted 7 lines
Sorry, I couldn't see any essential difference between this new implementation and the original one except that the former tries to use the library function -
bsearch()to replace the original binary search algorithm implemented in TIPC itself. Therefore, I don't think the change will have a big impact on performance.If I miss something, please let me know.
Comparison via a function pointer in bsearch is slower than direct code without the function call overhead.
Quoted 6 lines
Quoted 1 line
Right, but probably we can tolerate the slight sacrifice here.
What part of "very time critical" have you verified and benchmarked as inconsequential?
Please post your results.
I agree with Joe here. This change does not simplify anything, it does not reduce the amount of code, plus that it introduce an unnecessary outline call in a place where we have every reason to let the compiler do its optimization job properly.
///jon
Quoted 9 lines
Am 16.09.2017 um 15:20 schrieb Jon Maloy <[email protected]>.
Quoted 5 lines
What part of "very time critical" have you verified and benchmarked as inconsequential?
Please post your results.
I agree with Joe here. This change does not simplify anything, it does not reduce the amount of code, plus that it introduce an unnecessary outline call in a place where we have every reason to let the compiler do its optimization job properly.
Hi,
Okay, should I prepare some performance numbers or do we NAK this change?
What about the other binary search implementation in the same file? Should I try to convert it it will it get NAKed for performance reasons too?
With kind regards
Thomas
Quoted 20 lines
-----Original Message-----
From: Thomas Meyer [mailto:[email protected]]
Sent: Sunday, September 17, 2017 11:00
To: Jon Maloy <[email protected]>
Cc: Joe Perches <[email protected]>; Ying Xue <[email protected]>; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH V2] tipc: Use bsearch library functionQuoted 8 lines
Am 16.09.2017 um 15:20 schrieb Jon Maloy <[email protected]>.
Quoted 5 lines
What part of "very time critical" have you verified and benchmarked as inconsequential?
Please post your results.
I agree with Joe here. This change does not simplify anything, it does
not
Quoted 1 line
reduce the amount of code, plus that it introduce an unnecessary outline
call
Quoted 1 line
in a place where we have every reason to let the compiler do its
optimization
Quoted 8 lines
job properly.
Hi,
Okay, should I prepare some performance numbers or do we NAK this change?
What about the other binary search implementation in the same file? Should I try to convert it it will it get NAKed for performance reasons too?
The searches for inserting and removing publications is less time critical, so that would be ok with me.
If you have any more general interest in improving the code in this file (which is needed) it would also be appreciated.
BR
///jon
Quoted 3 lines
With kind regards
Thomas
Quoted 2 lines · Jon Maloy
Quoted 2 lines
-----Original Message-----
From: Thomas Meyer [mailto:[email protected]]
[]
Quoted 7 lines
Quoted 2 lines
What about the other binary search implementation in the same file? Should I try to convert it it will it get NAKed for performance reasons too?
The searches for inserting and removing publications is less time critical, so that would be ok with me.
If you have any more general interest in improving the code in this file (which is needed) it would also be appreciated.
Perhaps using an rbtree would be an improvement.
Quoted 20 lines
-----Original Message-----
From: [email protected] [mailto:netdev- [email protected]] On Behalf Of Joe Perches
Sent: Sunday, September 17, 2017 23:15
To: Jon Maloy <[email protected]>; Thomas Meyer <[email protected]>
Cc: Ying Xue <[email protected]>; [email protected]; tipc- [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH V2] tipc: Use bsearch library functionQuoted 2 lines · Jon Maloy
Quoted 2 lines
-----Original Message-----
From: Thomas Meyer [mailto:[email protected]][]
Quoted 3 lines
Quoted 2 lines
What about the other binary search implementation in the same file? Should I try to convert it it will it get NAKed for performance reasons too?
Quoted 6 lines
Quoted 4 lines
The searches for inserting and removing publications is less time critical, so that would be ok with me.
If you have any more general interest in improving the code in this file (which is needed) it would also be appreciated.Perhaps using an rbtree would be an improvement.
Not a bad idea. It would probably reduce the code amount, possibly at the expense of cache hit rate during the binary lookup. It is worth looking into.
///jon