From nobody Wed Jul 03 18:56:57 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4WDpt11n26z5Q0fZ; Wed, 03 Jul 2024 18:56:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4WDpt11D1yz4Vhm; Wed, 3 Jul 2024 18:56:57 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1720033017; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7yHgcOeNjhKFB27cRH71M3UwDQUT6Q9gGk8mNco1rpo=; b=nCXo8gOl0KkLlzKMIyE0ClqeNuJkKcUejwD86ndKaRmRDn9bjttUcvr5otrtxzFd+7GbB3 7OazXody42KZUkkq/MJHfeyBmSzWfHbpOKyUX1TJ/XwdZB8W4+0xOYArFb9sXc05frR8vt fWpEWtFt9C3mkThT9t8Iv4xjs5HaDJ2+qG1yTpyIRwRz4CzJXzjxPz962oPOKfb5MT011d j8+o8SksuPKyI6zIH4doNv2EbSv7e2wu7w1dA6aJpdJrbsNCvUfyj+RfqQfv+OOuDi7Atn Lj4HB0P544UDhpJwSN1I0H1ZSkjK8N9+J0DbD5bcp5lyKqxLCnStpVFa/F4REw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1720033017; a=rsa-sha256; cv=none; b=l2sHtfWpPIteH1DhsrlIwAp1oXh5Iwyd9pulOhrj0GSD2Hd1J8ioVz42uDrsmTmt85KyIQ zNwGvLGzGqHIWg4kXc0HW6HTQGJmsdzE88DQD8LZXRqOLfPOHkCSxy3ZFTQYCvw8piElct wH6CnNDqIAn0gpGXf4n4K6rye0FRljBqfiKSycJui7V2X8m0G10gUESK5eBEOWtCPc1sif cAKOLrJbt2lc8t6t8b1+ffP64iKoxmS9hiLWUz75qL91UicHtLwM+LzJP0/483ZcAedVx9 /hR+P8zS9fDHqxfMoyVJUznSFj1ZxwUSnTydAWoBuIs6f3VJKmFBA/WKKePn3Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1720033017; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7yHgcOeNjhKFB27cRH71M3UwDQUT6Q9gGk8mNco1rpo=; b=KDEvsvB+JbmmXC05H5OjHfolbN6K/c1Rw8++1eHaCGe2/XTqMQGLkBbr5Gf98HXhZd3Baq 18J9HnkNsJu1vdS4b3h5eQlQ0D74RrDAf+wBu07nRKUkaZkdiyVGkZmT8UmyVE+R3y/KR2 amEPwVX9KXr2vzQdE3TtoD0Axu44Os0o2uPPCqLetm43xrATXLM7a2KRlmp5ICMH/ztpbi d6J7p1KgQyWJtiMvH2X9zau/Nm3DxjNEmgFKV9YbdXaXKAQrBXiOAEx3hgdb3AP3/yE3gd TOZgISf9Dzx9bWSgFSI6AK5Q5c/X5aMAmnwAxCwo76BWSzb15SHHxH1YuRuhXw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4WDpt10qQWzjc0; Wed, 3 Jul 2024 18:56:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 463Iuv0r011765; Wed, 3 Jul 2024 18:56:57 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 463IuvdS011762; Wed, 3 Jul 2024 18:56:57 GMT (envelope-from git) Date: Wed, 3 Jul 2024 18:56:57 GMT Message-Id: <202407031856.463IuvdS011762@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: fd31c0946ee2 - main - cdefs.h: Add back comment about branch prediction List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: fd31c0946ee2240491db1301c2d33eb846221a54 Auto-Submitted: auto-generated The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=fd31c0946ee2240491db1301c2d33eb846221a54 commit fd31c0946ee2240491db1301c2d33eb846221a54 Author: Warner Losh AuthorDate: 2024-07-03 18:52:38 +0000 Commit: Warner Losh CommitDate: 2024-07-03 18:56:42 +0000 cdefs.h: Add back comment about branch prediction Add back, with editing, the comments about branch prediction, when to use it, etc. Offer stronger opinions about this in style(9). Add in the convention for FreeBSD that we do only the entire expression in the if expression. Advise use only when it makes things measurably faster. Requested by: jhb Sponsored by: Netflix Reviewed by: brooks, jhb Differential Revision: https://reviews.freebsd.org/D45837 --- share/man/man9/style.9 | 16 ++++++++++++++++ sys/sys/cdefs.h | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/share/man/man9/style.9 b/share/man/man9/style.9 index 381f3aa3bfa3..daddc57bfb1f 100644 --- a/share/man/man9/style.9 +++ b/share/man/man9/style.9 @@ -897,6 +897,22 @@ New code should use .Fn _Static_assert instead of the older .Fn CTASSERT . +.Pp +.Fn __predict_true +and +.Fn __predict_false +should only be used in frequently executed code when it makes the code +measurably faster. +It is wasteful to make predictions for infrequently run code, like subsystem +initialization. +When using branch prediction hints, atypical error conditions should use +.Fn __predict_false +(document the exceptions). +Operations that almost always succeed use +.Fn __predict_true . +Only use the annotation for the entire if statement, rather than individual clauses. +Do not add these annotations without empirical evidence of the likelihood of the +branch. .Sh FILES .Bl -tag -width indent .It Pa /usr/src/tools/build/checkstyle9.pl diff --git a/sys/sys/cdefs.h b/sys/sys/cdefs.h index 7c4890ece8d5..b92992c1b5c3 100644 --- a/sys/sys/cdefs.h +++ b/sys/sys/cdefs.h @@ -321,6 +321,13 @@ #define __restrict restrict #endif +/* + * All modern compilers have explicit branch prediction so that the CPU back-end + * can hint to the processor and also so that code blocks can be reordered such + * that the predicted path sees a more linear flow, thus improving cache + * behavior, etc. Use sparingly, except in performance critical code where + * they make things measurably faster. + */ #define __predict_true(exp) __builtin_expect((exp), 1) #define __predict_false(exp) __builtin_expect((exp), 0)