From owner-freebsd-hackers@FreeBSD.ORG Mon May 27 11:55:49 2013 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 208E5BA8 for ; Mon, 27 May 2013 11:55:49 +0000 (UTC) (envelope-from vhaisman@gmail.com) Received: from mail-la0-x22b.google.com (mail-la0-x22b.google.com [IPv6:2a00:1450:4010:c03::22b]) by mx1.freebsd.org (Postfix) with ESMTP id A1138B15 for ; Mon, 27 May 2013 11:55:48 +0000 (UTC) Received: by mail-la0-f43.google.com with SMTP id ez20so6427291lab.30 for ; Mon, 27 May 2013 04:55:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=lF2t13jXSyNvsiPoqlPMNLRokW8ApAArmdGlThWz9hQ=; b=gEQbm8AXDINCvshlKInMdwF21FISzJXrAjLfBhY9sm5z/AEQ/Yv0rVn5QkhXV9wlCh 0Qw5sqju14PGEhH8UU+XdjDM+DkiO9x5r47Ntw8208A12+3gFSyStbjzphRpvSAAZz8o kQvFPSc2DaH+iU3OxijbozI+30YI5Vg6JRt8qhps/5Oesr7+RCyrKw9GPWz3f7O1vVv3 2t0V4Um+0gKEc+d3UL52cfObB5vV7LeLboH1FgHt8ZmUTCsAcVVHORyV3tQ6IuO/W81Z KANayczBTSUv06HyEuxXUE/JRGnWMB34Dmwk12iQQb8piY6dIs3xHN0QlIYBOGzISbeb Vd5Q== MIME-Version: 1.0 X-Received: by 10.152.27.194 with SMTP id v2mr3091007lag.22.1369655267298; Mon, 27 May 2013 04:47:47 -0700 (PDT) Received: by 10.114.98.129 with HTTP; Mon, 27 May 2013 04:47:47 -0700 (PDT) In-Reply-To: References: <2adc4d8e7c55e92d935f61efb4c9d723@lthomas.net> Date: Mon, 27 May 2013 13:47:47 +0200 Message-ID: Subject: Re: Performance improvement to strnlen(). From: =?UTF-8?Q?V=C3=A1clav_Zeman?= To: Lee Thomas Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: freebsd-hackers@freebsd.org X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 May 2013 11:55:49 -0000 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