[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH v4 18/23] qemu-img: Change compare_sectors() to
From: |
John Snow |
Subject: |
Re: [Qemu-devel] [PATCH v4 18/23] qemu-img: Change compare_sectors() to be byte-based |
Date: |
Wed, 27 Sep 2017 18:25:59 -0400 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 |
On 09/13/2017 12:03 PM, Eric Blake wrote:
> In the continuing quest to make more things byte-based, change
> compare_sectors(), renaming it to compare_buffers() in the
> process. Note that one caller (qemu-img compare) only cares
> about the first difference, while the other (qemu-img rebase)
> cares about how many consecutive sectors have the same
> equal/different status; however, this patch does not bother to
> micro-optimize the compare case to avoid the comparisons of
> sectors beyond the first mismatch. Both callers are always
> passing valid buffers in, so the initial check for buffer size
> can be turned into an assertion.
>
> Signed-off-by: Eric Blake <address@hidden>
>
> ---
> v3: new patch
> ---
> qemu-img.c | 55 +++++++++++++++++++++++++++----------------------------
> 1 file changed, 27 insertions(+), 28 deletions(-)
>
> diff --git a/qemu-img.c b/qemu-img.c
> index 2e05f92e85..034122eba5 100644
> --- a/qemu-img.c
> +++ b/qemu-img.c
> @@ -1155,31 +1155,28 @@ static int is_allocated_sectors_min(const uint8_t
> *buf, int n, int *pnum,
> }
>
> /*
> - * Compares two buffers sector by sector. Returns 0 if the first sector of
> both
> - * buffers matches, non-zero otherwise.
> + * Compares two buffers sector by sector. Returns 0 if the first
> + * sector of each buffer matches, non-zero otherwise.
> *
> - * pnum is set to the number of sectors (including and immediately following
> - * the first one) that are known to have the same comparison result
> + * pnum is set to the sector-aligned size of the buffer prefix that
> + * has the same matching status as the first sector.
> */
> -static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
> - int *pnum)
> +static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2,
> + int64_t bytes, int64_t *pnum)
> {
> bool res;
> - int i;
> + int64_t i = MIN(bytes, BDRV_SECTOR_SIZE);
>
> - if (n <= 0) {
> - *pnum = 0;
> - return 0;
> - }
> + assert(bytes > 0);
>
> - res = !!memcmp(buf1, buf2, 512);
> - for(i = 1; i < n; i++) {
> - buf1 += 512;
> - buf2 += 512;
> + res = !!memcmp(buf1, buf2, i);
It is temporarily confusing that 'i' is never again used for this
particular parameter, because
> + while (i < bytes) {
This gives the brief impression that we might be looping in a way that
changes the comparison size passed to memcmp, which isn't true.
Just me being cranky, though. It's probably still the best way, because
of how you have to prime the loop. Doing it the literal-minded way
requires an extra i += len, so:
Reviewed-by: John Snow <address@hidden>
- [Qemu-devel] [PATCH v4 13/23] qemu-img: Simplify logic in img_compare(), (continued)
- [Qemu-devel] [PATCH v4 17/23] qemu-img: Change check_empty_sectors() to byte-based, Eric Blake, 2017/09/13
- [Qemu-devel] [PATCH v4 18/23] qemu-img: Change compare_sectors() to be byte-based, Eric Blake, 2017/09/13
- Re: [Qemu-devel] [PATCH v4 18/23] qemu-img: Change compare_sectors() to be byte-based,
John Snow <=
- [Qemu-devel] [PATCH v4 19/23] qemu-img: Change img_rebase() to be byte-based, Eric Blake, 2017/09/13
- [Qemu-devel] [PATCH v4 20/23] qemu-img: Change img_compare() to be byte-based, Eric Blake, 2017/09/13
- [Qemu-devel] [PATCH v4 21/23] block: Align block status requests, Eric Blake, 2017/09/13
- [Qemu-devel] [PATCH v4 22/23] block: Relax bdrv_aligned_preadv() assertion, Eric Blake, 2017/09/13
- [Qemu-devel] [PATCH v4 23/23] qemu-io: Relax 'alloc' now that block-status doesn't assert, Eric Blake, 2017/09/13
- Re: [Qemu-devel] [PATCH v4 00/23] make bdrv_get_block_status byte-based, Eric Blake, 2017/09/13