Skip to contentSkip to conversation

tipc: Use bsearch library function

PATCH Superseded Thomas Meyer 6 participants 15 messages Sep 11, 2017 – Sep 19, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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
index bd0aac87b41a..345454106390 100644
@@ -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_hea
 	return 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_hea
 static 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
maintainer
#1 lore raw

From: 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_hea
 	return 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.

#2 lore raw

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 +0200

Review 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_hea

Quoted 3 lines
net/tipc/name_table.c +0 -0
 	return 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

#3 lore raw

PATCH v2 1/1 tipc: Use bsearch library function

In reply to David Miller

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(-)

V2: Coding style

net/tipc/name_table.c +15 -15
index bd0aac87b41a..eeb4d7a13de2 100644
@@ -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_hea
 	return 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_hea
 static 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
Acked-by on v2 1/1
#4 lore raw
Quoted 4 lines · Thomas Meyer

Use common library function rather than explicitly coding some variant of it yourself.

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
index bd0aac87b41a..eeb4d7a13de2 100644
@@ -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_hea
 	return 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_hea
 static 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);
 }
 
 /**
#5 lore raw
Quoted 7 lines · Ying Xue
Quoted 4 lines · Thomas Meyer

Use common library function rather than explicitly coding some variant of it yourself.

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
index bd0aac87b41a..eeb4d7a13de2 100644
@@ -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_hea
 	return 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_hea
 static 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);
 }
 
 /**
#6 lore raw
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.

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
index bd0aac87b41a..eeb4d7a13de2 100644
@@ -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_hea
 	return 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_hea
 static 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);
 }
 
 /**
#7 lore raw
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.

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.

#8 lore raw
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.

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
#9 lore raw
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.

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.

#10 lore raw
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 function

Quoted 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.

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

#11 lore raw
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

#12 lore raw
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 function

Quoted 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

#13 lore raw
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.

#14 lore raw
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 function

Quoted 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