Skip to contentSkip to conversation

libxfs: Optimize the loop for xfs_bitmap_empty

PATCH Superseded Jia He 2 participants 6 messages Nov 9, 2015 – Dec 16, 2015 XFS filesystem

lore
Share & tools
lore

Jia He

author
#0 lore raw

PATCH 1/1 libxfs: Optimize the loop for xfs_bitmap_empty

If there is any non zero bit in a long bitmap, it can jump out of the for loop and finish the function as soon as possible.

Cc Dave Chinner <[email protected]>
Cc Brian Foster <[email protected]>
---
 fs/xfs/libxfs/xfs_bit.c | 2 ++
 1 file changed, 2 insertions(+)
fs/xfs/libxfs/xfs_bit.c +2 -0
index 0e8885a..84614b0 100644
@@ -36,6 +36,8 @@ xfs_bitmap_empty(uint *map, uint size)
 
 	for (i = 0; i < size; i++) {
 		ret |= map[i];
+		if (ret != 0)
+			return 0;
 	}
 
 	return (ret == 0);
Signature · 6 lines
2.5.0

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#1 lore raw
Quoted 15 lines · Jia He

If there is any non zero bit in a long bitmap, it can jump out of the for loop and finish the function as soon as possible.

Cc Dave Chinner <[email protected]>
Cc Brian Foster <[email protected]>
---
 fs/xfs/libxfs/xfs_bit.c | 2 ++
 1 file changed, 2 insertions(+)
fs/xfs/libxfs/xfs_bit.c +0 -0
index 0e8885a..84614b0 100644

fs/xfs/libxfs/xfs_bit.c xfs_bitmap_empty(uint *map, uint size) · line 42

Quoted 7 lines
fs/xfs/libxfs/xfs_bit.c +2 -0
@@ -36,6 +36,8 @@ xfs_bitmap_empty(uint *map, uint size)
 
 	for (i = 0; i < size; i++) {
 		ret |= map[i];
+		if (ret != 0)
+			return 0;
 	}
 

Seems Ok to me, but if we're going to do this, why not just kill ret entirely? For example, check 'if (map[i] != 0) return 0;' in the loop and unconditionally return 1 if we make it to the end.

Brian

Quoted 8 lines
fs/xfs/libxfs/xfs_bit.c +0 -0
 	return (ret == 0);
Signature · 6 lines
2.5.0

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

author
#2 lore raw

Hi Brian
Thanks, I will resend the patch V2

B.R.
Justin

Quoted 33 lines · Brian Foster
Quoted 15 lines · Jia He

If there is any non zero bit in a long bitmap, it can jump out of the for loop and finish the function as soon as possible.

Cc Dave Chinner <[email protected]>
Cc Brian Foster <[email protected]>
---
  fs/xfs/libxfs/xfs_bit.c | 2 ++
  1 file changed, 2 insertions(+)
fs/xfs/libxfs/xfs_bit.c +0 -0
index 0e8885a..84614b0 100644

fs/xfs/libxfs/xfs_bit.c xfs_bitmap_empty(uint *map, uint size) · line 42

Quoted 7 lines
fs/xfs/libxfs/xfs_bit.c +2 -0
@@ -36,6 +36,8 @@ xfs_bitmap_empty(uint *map, uint size)
 
  	for (i = 0; i < size; i++) {
  		ret |= map[i];
+		if (ret != 0)
+			return 0;
  	}
 

Seems Ok to me, but if we're going to do this, why not just kill ret entirely? For example, check 'if (map[i] != 0) return 0;' in the loop and unconditionally return 1 if we make it to the end.

Brian

Quoted 4 lines
fs/xfs/libxfs/xfs_bit.c +0 -0
  	return (ret == 0);
Signature · 1 line
2.5.0
Quoted 9 lines
Quoted 4 lines

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

author
#3 lore raw

PATCH v2 1/1 libxfs: Optimize the loop for xfs_bitmap_empty

In reply to Brian Foster

If there is any non zero bit in a long bitmap, it can jump out of the loop and finish the function as soon as possible.

Cc Dave Chinner <[email protected]>
Cc Brian Foster <[email protected]>
---
 fs/xfs/libxfs/xfs_bit.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/xfs/libxfs/xfs_bit.c +3 -3
index 0e8885a..0a94cce 100644
@@ -32,13 +32,13 @@ int
 xfs_bitmap_empty(uint *map, uint size)
 {
 	uint i;
-	uint ret = 0;
 
 	for (i = 0; i < size; i++) {
-		ret |= map[i];
+		if (map[i] != 0)
+			return 0;
 	}
 
-	return (ret == 0);
+	return 1;
 }
 
 /*
Signature · 6 lines
2.5.0

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Reviewed-by on v2 1/1
#4 lore raw
Quoted 7 lines · Jia He

If there is any non zero bit in a long bitmap, it can jump out of the loop and finish the function as soon as possible.

Cc Dave Chinner <[email protected]>
Cc Brian Foster <[email protected]>

---

Looks good to me:

Quoted 31 lines
 fs/xfs/libxfs/xfs_bit.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/xfs/libxfs/xfs_bit.c +3 -3
index 0e8885a..0a94cce 100644
@@ -32,13 +32,13 @@ int
 xfs_bitmap_empty(uint *map, uint size)
 {
 	uint i;
-	uint ret = 0;
 
 	for (i = 0; i < size; i++) {
-		ret |= map[i];
+		if (map[i] != 0)
+			return 0;
 	}
 
-	return (ret == 0);
+	return 1;
 }
 
 /*
Signature · 6 lines
2.5.0

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

author
#5 lore raw

Hi David

Do you have any comments from your point of view? Thanks ;)

B.R.
Justin

Quoted 39 lines · Brian Foster
Quoted 7 lines · Jia He

If there is any non zero bit in a long bitmap, it can jump out of the loop and finish the function as soon as possible.

Cc Dave Chinner <[email protected]>
Cc Brian Foster <[email protected]>

---

Looks good to me:

Quoted 27 lines
  fs/xfs/libxfs/xfs_bit.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
fs/xfs/libxfs/xfs_bit.c +3 -3
index 0e8885a..0a94cce 100644
@@ -32,13 +32,13 @@ int
  xfs_bitmap_empty(uint *map, uint size)
  {
  	uint i;
-	uint ret = 0;
 
  	for (i = 0; i < size; i++) {
-		ret |= map[i];
+		if (map[i] != 0)
+			return 0;
  	}
 
-	return (ret == 0);
+	return 1;
  }
 
  /*
Signature · 1 line
2.5.0
Quoted 9 lines
Quoted 4 lines

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs