diff mbox series

[RESEND,v2,1/5] sbitmap: remove unnecessary calculation of alloc_hint in __sbitmap_get_shallow

Message ID 20221222143353.598042-2-shikemeng@huaweicloud.com (mailing list archive)
State New, archived
Headers show
Series A few bugfix and cleanup patches for sbitmap | expand

Commit Message

Kemeng Shi Dec. 22, 2022, 2:33 p.m. UTC
We calculate allow_hint in next word as following:

/* low sb->shift bit of alloc_hint will be 0 after this shift */
alloc_hint = index << sb->shift;

/* get low sb->shift bit of alloc_hit */
SB_NR_TO_BIT(sb, alloc_hint)

So alloc_hit in next word will always be zero. Simpfy alloc_hit calculation
in __sbitmap_get_shallow according to the alloc_hit calculation in
__sbitmap_get.

Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
---
 lib/sbitmap.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

Comments

Jan Kara Dec. 22, 2022, 11:01 a.m. UTC | #1
On Thu 22-12-22 22:33:49, Kemeng Shi wrote:
> We calculate allow_hint in next word as following:
> 
> /* low sb->shift bit of alloc_hint will be 0 after this shift */
> alloc_hint = index << sb->shift;
> 
> /* get low sb->shift bit of alloc_hit */
> SB_NR_TO_BIT(sb, alloc_hint)
> 
> So alloc_hit in next word will always be zero. Simpfy alloc_hit calculation
> in __sbitmap_get_shallow according to the alloc_hit calculation in
> __sbitmap_get.
> 
> Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>

Looks good to me, nice cleanup. I'd just somewhat rephrase the changelog
to:

Updates to alloc_hint in the loop in __sbitmap_get_shallow() are mostly
pointless and equivalent to setting alloc_hint to zero (because
SB_NR_TO_BIT() considers only low sb->shift bits from alloc_hint). So
simplify the logic.

Anyway, feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
>  lib/sbitmap.c | 11 ++++-------
>  1 file changed, 4 insertions(+), 7 deletions(-)
> 
> diff --git a/lib/sbitmap.c b/lib/sbitmap.c
> index 586deb333237..cb5e03a2d65b 100644
> --- a/lib/sbitmap.c
> +++ b/lib/sbitmap.c
> @@ -243,6 +243,7 @@ static int __sbitmap_get_shallow(struct sbitmap *sb,
>  	int nr = -1;
>  
>  	index = SB_NR_TO_INDEX(sb, alloc_hint);
> +	alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
>  
>  	for (i = 0; i < sb->map_nr; i++) {
>  again:
> @@ -250,7 +251,7 @@ static int __sbitmap_get_shallow(struct sbitmap *sb,
>  					min_t(unsigned int,
>  					      __map_depth(sb, index),
>  					      shallow_depth),
> -					SB_NR_TO_BIT(sb, alloc_hint), true);
> +					alloc_hint, true);
>  		if (nr != -1) {
>  			nr += index << sb->shift;
>  			break;
> @@ -260,13 +261,9 @@ static int __sbitmap_get_shallow(struct sbitmap *sb,
>  			goto again;
>  
>  		/* Jump to next index. */
> -		index++;
> -		alloc_hint = index << sb->shift;
> -
> -		if (index >= sb->map_nr) {
> +		alloc_hint = 0;
> +		if (++index >= sb->map_nr)
>  			index = 0;
> -			alloc_hint = 0;
> -		}
>  	}
>  
>  	return nr;
> -- 
> 2.30.0
>
diff mbox series

Patch

diff --git a/lib/sbitmap.c b/lib/sbitmap.c
index 586deb333237..cb5e03a2d65b 100644
--- a/lib/sbitmap.c
+++ b/lib/sbitmap.c
@@ -243,6 +243,7 @@  static int __sbitmap_get_shallow(struct sbitmap *sb,
 	int nr = -1;
 
 	index = SB_NR_TO_INDEX(sb, alloc_hint);
+	alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
 
 	for (i = 0; i < sb->map_nr; i++) {
 again:
@@ -250,7 +251,7 @@  static int __sbitmap_get_shallow(struct sbitmap *sb,
 					min_t(unsigned int,
 					      __map_depth(sb, index),
 					      shallow_depth),
-					SB_NR_TO_BIT(sb, alloc_hint), true);
+					alloc_hint, true);
 		if (nr != -1) {
 			nr += index << sb->shift;
 			break;
@@ -260,13 +261,9 @@  static int __sbitmap_get_shallow(struct sbitmap *sb,
 			goto again;
 
 		/* Jump to next index. */
-		index++;
-		alloc_hint = index << sb->shift;
-
-		if (index >= sb->map_nr) {
+		alloc_hint = 0;
+		if (++index >= sb->map_nr)
 			index = 0;
-			alloc_hint = 0;
-		}
 	}
 
 	return nr;