Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 27 May 2013 13:47:47 +0200
From:      =?UTF-8?Q?V=C3=A1clav_Zeman?= <vhaisman@gmail.com>
To:        Lee Thomas <lee_thomas@aslantools.com>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: Performance improvement to strnlen().
Message-ID:  <CAKw7uVi3HoCg8qtTn-Sy9VCnDoVTnGJcxAcn5C_he1k8E7DqDw@mail.gmail.com>
In-Reply-To: <a88fc3c3c7c4da882af20eed5a61aee1@lthomas.net>
References:  <2adc4d8e7c55e92d935f61efb4c9d723@lthomas.net> <CAKw7uVi2oofoy=yJQiHR927oGfy74UuYFq68r=C=gUVXBXZuKw@mail.gmail.com> <a88fc3c3c7c4da882af20eed5a61aee1@lthomas.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On 27 May 2013 12:20, Lee Thomas wrote:
> On 2013-05-27 04:37, V=C3=A1clav Zeman wrote:
>>
>> On 26 May 2013 21:01, Lee Thomas wrote:
>>>
>>> On 2013-05-26 08:00, V=C3=A1clav Zeman wrote:
>>>>
>>>>
>>>> On 05/25/2013 10:27 PM, Lee Thomas wrote:
>>>>>
>>>>>
>>>>> +       lp =3D (const unsigned long *)((uintptr_t)str & ~LONGPTR_MASK=
);
>>>>> +       va =3D (*lp - mask01);
>>>>> +       vb =3D ((~*lp) & mask80);
>>>>
>>>>
>>>> I do not think that this correct C. This is type punning violating the
>>>> rules of the language.
>>>
>>>
>>>
>>> Hello V=C3=A1clav,
>>>
>>> The aliasing here is safe, because there are no writes through either o=
f
>>> the
>>> pointers, and the reads are correctly aligned.
>>
>> I disagree. IANALL but AFAIK, this is simply not allowed by the
>> language =3D> UB =3D> even though it seems to work in this instance, you
>> are just lucky the UB is actually doing what you expect.
>>
>> --
>> VZ
>
>
> Hello V=C3=A1clav,
>
> I am not an expert in C either, so you may be right that this is technica=
lly
> illegal. However, I copied this code from strlen.c, which has had it, and
> still has it, for 4.5 years, and I can't see any way any alias analysis d=
one
> by the compiler could invalidate this code. In addition, there are many
> places in the kernel, and in other codebases I've worked on, where this k=
ind
> of type conversion is done. See for instance
> /sys/amd64/amd64/vm_macdep.c:200, where we compute the base of a thread's
> stackframe from a pointer to an unrelated type of 'struct pcb', and then
> write to it.
>
> I am willing to uglify the code in the way you suggest if that is the
> general concensus, but I think the code as it stands is both safe and mor=
e
> legible.
You could always put the three lines into a macro to keep the
strnlen() more readable.


--
VZ



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAKw7uVi3HoCg8qtTn-Sy9VCnDoVTnGJcxAcn5C_he1k8E7DqDw>