Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: Aleksandr Lyapunov <alyapunov@tarantool.org>,
	tarantool-patches@dev.tarantool.org, tsafin@tarantool.org,
	gorcunov@gmail.com
Subject: Re: [Tarantool-patches] [PATCH 2/2] uuid: fix unaligned memory access
Date: Mon, 18 May 2020 23:17:28 +0200	[thread overview]
Message-ID: <e7a1a29d-c30b-9800-b55f-0f9ce00f8c4e@tarantool.org> (raw)
In-Reply-To: <52af1bb7-b125-7490-3882-ce74698789d2@tarantool.org>

Thanks for the review!

> On 5/16/20 2:03 AM, Vladislav Shpilevoy wrote:
>>   tt_uuid_is_nil(const struct tt_uuid *uu)
>>   {
>> -    const uint64_t *p = (const uint64_t *) uu;
>> -    return !p[0] && !p[1];
>> +    const uint32_t *p = (const uint32_t *) uu;
>> +    return p[0] == 0 && p[1] == 0 && p[2] == 0 && p[3] == 0;
>>   }
>>     /**
>> @@ -172,9 +172,10 @@ tt_uuid_is_nil(const struct tt_uuid *uu)
>>   inline bool
>>   tt_uuid_is_equal(const struct tt_uuid *lhs, const struct tt_uuid *rhs)
>>   {
>> -    const uint64_t *lp = (const uint64_t *) lhs;
>> -    const uint64_t *rp = (const uint64_t *) rhs;
>> -    return lp[0] == rp[0] && lp[1] == rp[1];
>> +    const uint32_t *lp = (const uint32_t *) lhs;
>> +    const uint32_t *rp = (const uint32_t *) rhs;
>> +    return lp[0] == rp[0] && lp[1] == rp[1] && lp[2] == rp[2] &&
>> +           lp[3] == rp[3];
> 
> It seems that we degrade performance just for clang to be happy..

Yeah, well. This is the same like saying that we degrade performance
when we do OOM checks. Unaligned memory access is UB. This is a bug.

> I would suggest to use memcmp in this case.
> It's portable and allows a compiler to generate the best possible code.
> I've measured it (gcc) and memcmp version is twice faster than your solution.
> Even for _is_nil method it's better to use memcmp with statically allocated zero buffer.

Could you please show the benchmark? I did my own, and I can't see any
significant difference. The present difference is so small, that it
looks like jitter. Both in is_nil and is_eq.

I did a very simple bench in Lua, without any GCed objects.

    uuid = require('uuid')
    uuid1 = uuid.new()
    uuid2 = uuid.new()
    clock = require('clock')
    function bench1()
        local res = 0
        for i = 1, 10000000 do
            res = res + ((uuid1 == uuid2) and 1 or 0)
        end
        return res
    end
    function bench2()
        local res = 0
        for i = 1, 100000000 do
            res = res + ((uuid1:isnil() and uuid2:isnil()) and 1 or 0)
        end
        return res
    end
    clock.bench(bench2)


Bench1 is to check is_eq(). Bench2 is to check is_nil(). The build
was Release. Here is what I've got:

    uuid is eq:

        4 x uint32 (my solution):

            2.691714
            2.727981
            2.852932
            2.764013

        memcmp (your solution):

            2.701216
            2.731426
            2.641924
            2.628619

        2 x uint64 (old solution):

            2.671254
            2.644062
            2.639914
            2.66742


    uuid is nil:

        4 x uint32:

            0.2599
            0.256139
            0.258356
            0.259495

        memcmp:

            0.26042
            0.263186
            0.262118
            0.261504

        2 x uint64:

            0.256665
            0.259839
            0.259746
            0.258651

  reply	other threads:[~2020-05-18 21:17 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-15 23:03 [Tarantool-patches] [PATCH 0/2] Sanitize uuid and bit alignment Vladislav Shpilevoy
2020-05-15 23:03 ` [Tarantool-patches] [PATCH 1/2] bit: fix unaligned memory access and UB bit shift Vladislav Shpilevoy
2020-05-21 14:37   ` Timur Safin
2020-05-15 23:03 ` [Tarantool-patches] [PATCH 2/2] uuid: fix unaligned memory access Vladislav Shpilevoy
2020-05-18 12:55   ` Aleksandr Lyapunov
2020-05-18 21:17     ` Vladislav Shpilevoy [this message]
2020-05-19  7:28       ` Aleksandr Lyapunov
2020-05-19  8:34         ` Timur Safin
2020-05-19 21:24         ` Vladislav Shpilevoy
2020-05-20  8:18           ` Aleksandr Lyapunov
2020-05-20 21:38             ` Vladislav Shpilevoy
2020-05-21  8:28               ` Aleksandr Lyapunov
2020-05-21 14:37           ` Timur Safin
2020-05-21 19:33 ` [Tarantool-patches] [PATCH 0/2] Sanitize uuid and bit alignment Vladislav Shpilevoy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e7a1a29d-c30b-9800-b55f-0f9ce00f8c4e@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=alyapunov@tarantool.org \
    --cc=gorcunov@gmail.com \
    --cc=tarantool-patches@dev.tarantool.org \
    --cc=tsafin@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 2/2] uuid: fix unaligned memory access' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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