Skip to contentSkip to conversation

ACPI: NFIT: Optimize nfit_mem_cmp() for efficiency

PATCH Superseded Kuan-Wei Chiu 2 participants 4 messages Oct 12, 2023 – Oct 19, 2023

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

PATCH 1/1 ACPI: NFIT: Optimize nfit_mem_cmp() for efficiency

The original code used conditional branching in the nfit_mem_cmp function to compare two values and return -1, 1, or 0 based on the result. However, the list_sort comparison function only needs results <0, >0, or =0. This patch optimizes the code to make the comparison branchless, improving efficiency and reducing code size. This change reduces the number of comparison operations from 1-2 to a single subtraction operation, thereby saving the number of instructions.

---
 drivers/acpi/nfit/core.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/acpi/nfit/core.c +1 -5
index f96bf32cd368..eea827d9af08 100644
@@ -1138,11 +1138,7 @@ static int nfit_mem_cmp(void *priv, const struct list_head *_a,
 
 	handleA = __to_nfit_memdev(a)->device_handle;
 	handleB = __to_nfit_memdev(b)->device_handle;
-	if (handleA < handleB)
-		return -1;
-	else if (handleA > handleB)
-		return 1;
-	return 0;
+	return handleA - handleB;
 }
 
 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/1 ACPI: NFIT: Optimize nfit_mem_cmp() for efficiency

The original code used conditional branching in the nfit_mem_cmp function to compare two values and return -1, 1, or 0 based on the result. However, the list_sort comparison function only needs results <0, >0, or =0. This patch optimizes the code to make the comparison branchless, improving efficiency and reducing code size. This change reduces the number of comparison operations from 1-2 to a single subtraction operation, thereby saving the number of instructions.

---
v1 -> v2:
- Add explicit type cast in case the sizes of u32 and int differ.

 drivers/acpi/nfit/core.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/acpi/nfit/core.c +1 -5
index f96bf32cd368..563a32eba888 100644
@@ -1138,11 +1138,7 @@ static int nfit_mem_cmp(void *priv, const struct list_head *_a,
 
 	handleA = __to_nfit_memdev(a)->device_handle;
 	handleB = __to_nfit_memdev(b)->device_handle;
-	if (handleA < handleB)
-		return -1;
-	else if (handleA > handleB)
-		return 1;
-	return 0;
+	return (int)handleA - (int)handleB;
 }
 
 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
Signature · 1 line
2.25.1
#2 lore raw
Quoted 22 lines · Kuan-Wei Chiu

The original code used conditional branching in the nfit_mem_cmp function to compare two values and return -1, 1, or 0 based on the result. However, the list_sort comparison function only needs results <0, >0, or =0. This patch optimizes the code to make the comparison branchless, improving efficiency and reducing code size. This change reduces the number of comparison operations from 1-2 to a single subtraction operation, thereby saving the number of instructions.

---
v1 -> v2:
- Add explicit type cast in case the sizes of u32 and int differ.

 drivers/acpi/nfit/core.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/acpi/nfit/core.c +0 -0
index f96bf32cd368..563a32eba888 100644

drivers/acpi/nfit/core.c static int nfit_mem_cmp(void *priv, const struct list_head *_a, · line 1141

Quoted 9 lines
drivers/acpi/nfit/core.c +1 -5
@@ -1138,11 +1138,7 @@ static int nfit_mem_cmp(void *priv, const struct list_head *_a,
 
        handleA = __to_nfit_memdev(a)->device_handle;
        handleB = __to_nfit_memdev(b)->device_handle;
-       if (handleA < handleB)
-               return -1;
-       else if (handleA > handleB)
-               return 1;
-       return 0;
+       return (int)handleA - (int)handleB;

Are you sure that you are not losing bits in these conversions?

Quoted 4 lines
drivers/acpi/nfit/core.c +0 -1
 }
 
 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
--
#3 lore raw
Quoted 24 lines · Rafael J. Wysocki
Quoted 23 lines · Kuan-Wei Chiu

The original code used conditional branching in the nfit_mem_cmp function to compare two values and return -1, 1, or 0 based on the result. However, the list_sort comparison function only needs results <0, >0, or =0. This patch optimizes the code to make the comparison branchless, improving efficiency and reducing code size. This change reduces the number of comparison operations from 1-2 to a single subtraction operation, thereby saving the number of instructions.

---
v1 -> v2:
- Add explicit type cast in case the sizes of u32 and int differ.

 drivers/acpi/nfit/core.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/acpi/nfit/core.c +0 -0
index f96bf32cd368..563a32eba888 100644
@@ -1138,11 +1138,7 @@ static int nfit_mem_cmp(void *priv, const struct list_head *_a,
 
Quoted 10 lines

drivers/acpi/nfit/core.c static int nfit_mem_cmp(void *priv, const struct list_head *_a, · line 1141

Quoted 8 lines
drivers/acpi/nfit/core.c +1 -5
        handleA = __to_nfit_memdev(a)->device_handle;
        handleB = __to_nfit_memdev(b)->device_handle;
-       if (handleA < handleB)
-               return -1;
-       else if (handleA > handleB)
-               return 1;
-       return 0;
+       return (int)handleA - (int)handleB;

Are you sure that you are not losing bits in these conversions?

I believe your concerns are valid. Perhaps this was a stupid mistake I made. Initially, I proposed this patch because I noticed that other parts of the Linux kernel, such as the sram_reserve_cmp() function in drivers/misc/sram.c, directly used subtraction for comparisons involving u32. However, this approach could potentially lead to issues when the size of int is 2 bytes instead of 4 bytes. I think maybe we should consider dropping this patch. I apologize for proposing an incorrect patch.

Thanks,
Kuan-Wei Chiu

Quoted 5 lines
Quoted 5 lines
drivers/acpi/nfit/core.c +0 -1
 }
 
 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
--