public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/i915/gvt: Optimize mmio_offset_compare() for efficiency
@ 2023-10-12 23:04 Kuan-Wei Chiu
  2023-10-13 10:36 ` Ville Syrjälä
  0 siblings, 1 reply; 3+ messages in thread
From: Kuan-Wei Chiu @ 2023-10-12 23:04 UTC (permalink / raw)
  To: zhenyuw, zhi.a.wang
  Cc: jani.nikula, joonas.lahtinen, rodrigo.vivi, tvrtko.ursulin,
	airlied, daniel, intel-gvt-dev, intel-gfx, dri-devel,
	linux-kernel, Kuan-Wei Chiu

The original code used conditional branching in the mmio_offset_compare
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.

Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
 drivers/gpu/drm/i915/gvt/debugfs.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/debugfs.c b/drivers/gpu/drm/i915/gvt/debugfs.c
index baccbf1761b7..998d82a259c8 100644
--- a/drivers/gpu/drm/i915/gvt/debugfs.c
+++ b/drivers/gpu/drm/i915/gvt/debugfs.c
@@ -48,11 +48,7 @@ static int mmio_offset_compare(void *priv,
 
 	ma = container_of(a, struct diff_mmio, node);
 	mb = container_of(b, struct diff_mmio, node);
-	if (ma->offset < mb->offset)
-		return -1;
-	else if (ma->offset > mb->offset)
-		return 1;
-	return 0;
+	return ma->offset - mb->offset;
 }
 
 static inline int mmio_diff_handler(struct intel_gvt *gvt,
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] drm/i915/gvt: Optimize mmio_offset_compare() for efficiency
  2023-10-12 23:04 [PATCH] drm/i915/gvt: Optimize mmio_offset_compare() for efficiency Kuan-Wei Chiu
@ 2023-10-13 10:36 ` Ville Syrjälä
  2023-10-13 12:13   ` [PATCH v2] " Kuan-Wei Chiu
  0 siblings, 1 reply; 3+ messages in thread
From: Ville Syrjälä @ 2023-10-13 10:36 UTC (permalink / raw)
  To: Kuan-Wei Chiu
  Cc: zhenyuw, zhi.a.wang, tvrtko.ursulin, intel-gvt-dev, intel-gfx,
	linux-kernel, dri-devel, rodrigo.vivi

On Fri, Oct 13, 2023 at 07:04:49AM +0800, Kuan-Wei Chiu wrote:
> The original code used conditional branching in the mmio_offset_compare
> 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.
> 
> Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
> ---
>  drivers/gpu/drm/i915/gvt/debugfs.c | 6 +-----
>  1 file changed, 1 insertion(+), 5 deletions(-)
> 
> diff --git a/drivers/gpu/drm/i915/gvt/debugfs.c b/drivers/gpu/drm/i915/gvt/debugfs.c
> index baccbf1761b7..998d82a259c8 100644
> --- a/drivers/gpu/drm/i915/gvt/debugfs.c
> +++ b/drivers/gpu/drm/i915/gvt/debugfs.c
> @@ -48,11 +48,7 @@ static int mmio_offset_compare(void *priv,
>  
>  	ma = container_of(a, struct diff_mmio, node);
>  	mb = container_of(b, struct diff_mmio, node);
> -	if (ma->offset < mb->offset)
> -		return -1;
> -	else if (ma->offset > mb->offset)
> -		return 1;
> -	return 0;
> +	return ma->offset - mb->offset;

Those are unsigned ints effectively, so this only works due
to the return value being the same size signed int. Might be
better to add some explicit casts.

>  }
>  
>  static inline int mmio_diff_handler(struct intel_gvt *gvt,
> -- 
> 2.25.1

-- 
Ville Syrjälä
Intel

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2] drm/i915/gvt: Optimize mmio_offset_compare() for efficiency
  2023-10-13 10:36 ` Ville Syrjälä
@ 2023-10-13 12:13   ` Kuan-Wei Chiu
  0 siblings, 0 replies; 3+ messages in thread
From: Kuan-Wei Chiu @ 2023-10-13 12:13 UTC (permalink / raw)
  To: ville.syrjala, zhenyuw, zhi.a.wang
  Cc: jani.nikula, joonas.lahtinen, rodrigo.vivi, tvrtko.ursulin,
	airlied, daniel, intel-gvt-dev, intel-gfx, dri-devel,
	linux-kernel, Kuan-Wei Chiu

The original code used conditional branching in the mmio_offset_compare
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.

Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
v1 -> v2:
- Add explicit type cast in case the sizes of u32 and int differ.

 drivers/gpu/drm/i915/gvt/debugfs.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/debugfs.c b/drivers/gpu/drm/i915/gvt/debugfs.c
index baccbf1761b7..d85d8a3b5ae5 100644
--- a/drivers/gpu/drm/i915/gvt/debugfs.c
+++ b/drivers/gpu/drm/i915/gvt/debugfs.c
@@ -48,11 +48,7 @@ static int mmio_offset_compare(void *priv,
 
 	ma = container_of(a, struct diff_mmio, node);
 	mb = container_of(b, struct diff_mmio, node);
-	if (ma->offset < mb->offset)
-		return -1;
-	else if (ma->offset > mb->offset)
-		return 1;
-	return 0;
+	return (int)ma->offset - (int)mb->offset;
 }
 
 static inline int mmio_diff_handler(struct intel_gvt *gvt,
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-10-13 12:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-12 23:04 [PATCH] drm/i915/gvt: Optimize mmio_offset_compare() for efficiency Kuan-Wei Chiu
2023-10-13 10:36 ` Ville Syrjälä
2023-10-13 12:13   ` [PATCH v2] " Kuan-Wei Chiu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox