[PATCH v2] btree: simplify merge logic by using btree_last() return value

Guan-Chun Wu posted 1 patch 1 month, 1 week ago
lib/btree.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH v2] btree: simplify merge logic by using btree_last() return value
Posted by Guan-Chun Wu 1 month, 1 week ago
Previously btree_merge() called btree_last() only to test existence,
then performed an extra btree_lookup() to fetch the value. This patch
changes it to directly use the value returned by btree_last(), avoiding
redundant lookups and simplifying the merge loop.

v2:
 - Update subject to clarify this is a simplification, not a bug fix

Signed-off-by: Guan-Chun Wu <409411716@gms.tku.edu.tw>
---
 lib/btree.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/btree.c b/lib/btree.c
index bb81d3393ac5..9c80c0c7bba8 100644
--- a/lib/btree.c
+++ b/lib/btree.c
@@ -653,9 +653,9 @@ int btree_merge(struct btree_head *target, struct btree_head *victim,
 	 * walks to remove a single object from the victim.
 	 */
 	for (;;) {
-		if (!btree_last(victim, geo, key))
+		val = btree_last(victim, geo, key);
+		if (!val)
 			break;
-		val = btree_lookup(victim, geo, key);
 		err = btree_insert(target, geo, key, val, gfp);
 		if (err)
 			return err;
-- 
2.34.1
Re: [PATCH v2] btree: simplify merge logic by using btree_last() return value
Posted by Kuan-Wei Chiu 1 month ago
Hi Guan-Chun,

On Wed, Aug 27, 2025 at 12:17:41AM +0800, Guan-Chun Wu wrote:
> Previously btree_merge() called btree_last() only to test existence,
> then performed an extra btree_lookup() to fetch the value. This patch
> changes it to directly use the value returned by btree_last(), avoiding
> redundant lookups and simplifying the merge loop.
> 
> v2:
>  - Update subject to clarify this is a simplification, not a bug fix

For future reference, please put version-to-version changelogs
(e.g. v1 -> v2 notes) between --- and the diffstat. [1] That way they
are not recorded in the permanent commit message but still visible
during review.

[1]: https://www.kernel.org/doc/html/v6.12/process/submitting-patches.html#the-canonical-patch-format

Regards,
Kuan-Wei

> 
> Signed-off-by: Guan-Chun Wu <409411716@gms.tku.edu.tw>
> ---
>  lib/btree.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/btree.c b/lib/btree.c
> index bb81d3393ac5..9c80c0c7bba8 100644
> --- a/lib/btree.c
> +++ b/lib/btree.c
> @@ -653,9 +653,9 @@ int btree_merge(struct btree_head *target, struct btree_head *victim,
>  	 * walks to remove a single object from the victim.
>  	 */
>  	for (;;) {
> -		if (!btree_last(victim, geo, key))
> +		val = btree_last(victim, geo, key);
> +		if (!val)
>  			break;
> -		val = btree_lookup(victim, geo, key);
>  		err = btree_insert(target, geo, key, val, gfp);
>  		if (err)
>  			return err;
> -- 
> 2.34.1
> 
>