Patchwork [v3,14/15] qcow2: small math optimization

login
register
mail settings
Submitter Frediano Ziglio
Date Aug. 23, 2011, 1:21 p.m.
Message ID <1314105682-28396-15-git-send-email-freddy77@gmail.com>
Download mbox | patch
Permalink /patch/111108/
State New
Headers show

Comments

Frediano Ziglio - Aug. 23, 2011, 1:21 p.m.
Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
---
 block/qcow2-refcount.c |    5 +----
 1 files changed, 1 insertions(+), 4 deletions(-)
Kevin Wolf - Aug. 23, 2011, 3:34 p.m.
Am 23.08.2011 15:21, schrieb Frediano Ziglio:
> Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
> ---
>  block/qcow2-refcount.c |    5 +----
>  1 files changed, 1 insertions(+), 4 deletions(-)
> 
> diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
> index 2a915be..0f9a64a 100644
> --- a/block/qcow2-refcount.c
> +++ b/block/qcow2-refcount.c
> @@ -140,10 +140,7 @@ static unsigned int next_refcount_table_size(BDRVQcowState *s,
>  static int in_same_refcount_block(BDRVQcowState *s, uint64_t offset_a,
>      uint64_t offset_b)
>  {
> -    uint64_t block_a = offset_a >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
> -    uint64_t block_b = offset_b >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
> -
> -    return (block_a == block_b);
> +    return ((offset_a ^ offset_b) >> (2 * s->cluster_bits - REFCOUNT_SHIFT)) == 0;
>  }

Depending on whether the compiler is smart enough this will or will not
change performance. However, even if we assume that it's a slight
improvement, this is in a function that is hardly ever run and the
optimisation comes with a high cost in terms of readability.

I wouldn't do this.

Kevin

Patch

diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
index 2a915be..0f9a64a 100644
--- a/block/qcow2-refcount.c
+++ b/block/qcow2-refcount.c
@@ -140,10 +140,7 @@  static unsigned int next_refcount_table_size(BDRVQcowState *s,
 static int in_same_refcount_block(BDRVQcowState *s, uint64_t offset_a,
     uint64_t offset_b)
 {
-    uint64_t block_a = offset_a >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
-    uint64_t block_b = offset_b >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
-
-    return (block_a == block_b);
+    return ((offset_a ^ offset_b) >> (2 * s->cluster_bits - REFCOUNT_SHIFT)) == 0;
 }
 
 /*