From nobody Mon Jul 04 17:32:55 2022 X-Original-To: dev-commits-src-all@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 863351C038BD; Mon, 4 Jul 2022 17:32:55 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4LcCYz3Kgvz4fMD; Mon, 4 Jul 2022 17:32:55 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1656955975; 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=/aY/9knjarEEagIHNIWSIzi8zjmROyfK+pWhMF8JWfs=; b=oEx6KI5Cli7WpVe1IiVKQ7IO6uFyOgF7lKerUx8z7jFrttbz4hVCw3/PpijEzJED45WK3O CKlrv4BTqx1X5VYMpXeG1MEgD3viijkKgD93ZpVC00b5TXZRDcEDaVKeSjnSgg6gxh5UGp 2B2mMXEJRHcanAhloli+xChs/ciCEL1A2VdEMOYxI284DNfaQc5gvGCetxIPKacuU8oGaL 62DgnkzUKJEhYGsyg14LZqV73jvZHP4PbPXY0vF4yD6deofQ92VxiCrJpy12TcoVPBDOwM MmO3KAs86PMLWVNYpW8Nx8NByejos2r3kgOu9zQbuLIJlme3SFCvsZfBFa3m9w== 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 4E63A15B02; Mon, 4 Jul 2022 17:32:55 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 264HWtDk022644; Mon, 4 Jul 2022 17:32:55 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 264HWtDn022643; Mon, 4 Jul 2022 17:32:55 GMT (envelope-from git) Date: Mon, 4 Jul 2022 17:32:55 GMT Message-Id: <202207041732.264HWtDn022643@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Doug Moore Subject: git: 2120d7f57aa0 - main - rb_tree: fine-tune rebalancing code List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dougm X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 2120d7f57aa0ee48d0be7a4309072bb332d568dd Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1656955975; 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=/aY/9knjarEEagIHNIWSIzi8zjmROyfK+pWhMF8JWfs=; b=q+sorzCCZ1G/l8KfD8o+7K5P0aCJOHXjpzaNlkJSNN1j8JpOkmnXrYuxgzlKvoEDwVaH9s 1a+6BMeY81hDbZtJ7sPQdwGlY9kU2d1skPFWZvrqAcVheeh6GIyT6GBIEBHlMbA6xEpTKY 0xRJSkR6eQqZ3pOrRSe10NyHZQmTyyDcvjajMkMUaisITAbQjB5j+SvgDIxWp33MYOUGsU 4mPigUFaA7WJSLoKgkbXriII0ML60cfm4Rw/bhobYy06yRamQxeG7gGP9euspfSVff+w7a mJIeu4Zi3+uocKF7qoDnK8lVVvYOuHJHy6IvBUBj3vhck5jww6sLaoH3FbR73g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1656955975; a=rsa-sha256; cv=none; b=IptxAGi0sEx+ZSV5SuUegGm8DzD3e3ZqhZsfyf3iA+iKeyHN5ZI3mWuTmKilGJolC6f5ok SdzStm461Ovs5+xvLyiR6LdKOsNRjNbpUGYSaiH78RDL7ihoAaoxEUC/igyagNR99yKAms gHXqb7Fx/WWuhsKPFAB3nEuS/ArBgFlVPjGSh+X2mrVvfQJQ7KEak3MK2Ld0aRaOfQvGVQ yWNieBLyuK0KVkKosBledxZkEZ1ABWC8O44BznljX21EE8ZtSg1WYqToKKd3mzE+hkvmNx flBuHyTvY4QKpAvwdYCk7mSnmf0Nuka4YtzcI6EvU2vULS7Yomgnchw27Ur59w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by dougm: URL: https://cgit.FreeBSD.org/src/commit/?id=2120d7f57aa0ee48d0be7a4309072bb332d568dd commit 2120d7f57aa0ee48d0be7a4309072bb332d568dd Author: Doug Moore AuthorDate: 2022-07-04 17:28:35 +0000 Commit: Doug Moore CommitDate: 2022-07-04 17:28:35 +0000 rb_tree: fine-tune rebalancing code Change parts of RB_INSERT_COLOR and RB_REMOVE_COLOR to reduce the number of operations, by, in some cases, flipping two color bits at a time instead of flipping each individually, in separate operations, and by using a switch statement to replace a sequence of if-elses. Rewrite RB_SET_PARENT to generate fewer instructions. These changes reduce the code size by over 100 bytes on some architectures. Also, allow RB users to define a preprocessor symbol to generate RB_REMOVE_COLOR code that matches the implementation described in the original weak-AVL paper in one particular case, instead of the still correct, but slightly more efficient implementation of that case currently implemented. Reviewed by: alc MFC after: 3 weeks Differential Revision: https://reviews.freebsd.org/D35524 --- sys/sys/tree.h | 104 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 71 insertions(+), 33 deletions(-) diff --git a/sys/sys/tree.h b/sys/sys/tree.h index 553d84e57cc8..3f48c07a70b1 100644 --- a/sys/sys/tree.h +++ b/sys/sys/tree.h @@ -340,6 +340,7 @@ struct { \ #define RB_RED_MASK ((__uintptr_t)3) #define RB_FLIP_LEFT(elm, field) (RB_BITS(elm, field) ^= RB_RED_L) #define RB_FLIP_RIGHT(elm, field) (RB_BITS(elm, field) ^= RB_RED_R) +#define RB_FLIP_ALL(elm, field) (RB_BITS(elm, field) ^= RB_RED_MASK) #define RB_RED_LEFT(elm, field) ((RB_BITS(elm, field) & RB_RED_L) != 0) #define RB_RED_RIGHT(elm, field) ((RB_BITS(elm, field) & RB_RED_R) != 0) #define RB_PARENT(elm, field) ((__typeof(RB_UP(elm, field))) \ @@ -348,8 +349,8 @@ struct { \ #define RB_EMPTY(head) (RB_ROOT(head) == NULL) #define RB_SET_PARENT(dst, src, field) do { \ - RB_BITS(dst, field) &= RB_RED_MASK; \ - RB_BITS(dst, field) |= (__uintptr_t)src; \ + RB_BITS(dst, field) = (__uintptr_t)src | \ + (RB_BITS(dst, field) & RB_RED_MASK); \ } while (/*CONSTCOND*/ 0) #define RB_SET(elm, parent, field) do { \ @@ -487,13 +488,14 @@ name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ continue; \ } \ if (!RB_RED_RIGHT(elm, field)) { \ - RB_FLIP_LEFT(elm, field); \ /* coverity[uninit_use] */ \ RB_ROTATE_LEFT(head, elm, child, field);\ - if (RB_RED_LEFT(child, field)) \ - RB_FLIP_RIGHT(elm, field); \ - else if (RB_RED_RIGHT(child, field)) \ + if (RB_RED_RIGHT(child, field)) \ RB_FLIP_LEFT(parent, field); \ + if (RB_RED_LEFT(child, field)) \ + RB_FLIP_ALL(elm, field); \ + else \ + RB_FLIP_LEFT(elm, field); \ elm = child; \ } \ RB_ROTATE_RIGHT(head, parent, elm, field); \ @@ -509,13 +511,14 @@ name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ continue; \ } \ if (!RB_RED_LEFT(elm, field)) { \ - RB_FLIP_RIGHT(elm, field); \ /* coverity[uninit_use] */ \ RB_ROTATE_RIGHT(head, elm, child, field);\ - if (RB_RED_RIGHT(child, field)) \ - RB_FLIP_LEFT(elm, field); \ - else if (RB_RED_LEFT(child, field)) \ + if (RB_RED_LEFT(child, field)) \ RB_FLIP_RIGHT(parent, field); \ + if (RB_RED_RIGHT(child, field)) \ + RB_FLIP_ALL(elm, field); \ + else \ + RB_FLIP_RIGHT(elm, field); \ elm = child; \ } \ RB_ROTATE_LEFT(head, parent, elm, field); \ @@ -525,6 +528,17 @@ name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ } \ } +#ifndef RB_STRICT_HST +/* + * In REMOVE_COLOR, the HST paper, in figure 3, in the single-rotate case, has + * 'parent' with one higher rank, and then reduces its rank if 'parent' has + * become a leaf. This implementation always has the parent in its new position + * with lower rank, to avoid the leaf check. Define RB_STRICT_HST to 1 to get + * the behavior that HST describes. + */ +#define RB_STRICT_HST 0 +#endif + #define RB_GENERATE_REMOVE_COLOR(name, type, field, attr) \ attr void \ name##_RB_REMOVE_COLOR(struct name *head, \ @@ -539,7 +553,7 @@ name##_RB_REMOVE_COLOR(struct name *head, \ if (parent == NULL) \ return; \ } \ - do { \ + do { \ if (RB_LEFT(parent, field) == elm) { \ if (!RB_RED_LEFT(parent, field)) { \ RB_FLIP_LEFT(parent, field); \ @@ -551,24 +565,36 @@ name##_RB_REMOVE_COLOR(struct name *head, \ continue; \ } \ sib = RB_RIGHT(parent, field); \ - if ((~RB_BITS(sib, field) & RB_RED_MASK) == 0) {\ - RB_BITS(sib, field) &= ~RB_RED_MASK; \ + switch (RB_BITS(sib, field) & RB_RED_MASK) { \ + case RB_RED_MASK: \ + RB_FLIP_ALL(sib, field); \ elm = parent; \ continue; \ - } \ - RB_FLIP_RIGHT(sib, field); \ - if (RB_RED_LEFT(sib, field)) \ - RB_FLIP_LEFT(parent, field); \ - else if (!RB_RED_RIGHT(sib, field)) { \ - RB_FLIP_LEFT(parent, field); \ + case RB_RED_R: \ elm = RB_LEFT(sib, field); \ RB_ROTATE_RIGHT(head, sib, elm, field); \ - if (RB_RED_RIGHT(elm, field)) \ - RB_FLIP_LEFT(sib, field); \ if (RB_RED_LEFT(elm, field)) \ - RB_FLIP_RIGHT(parent, field); \ + RB_FLIP_ALL(parent, field); \ + else \ + RB_FLIP_LEFT(parent, field); \ + if (RB_RED_RIGHT(elm, field)) \ + RB_FLIP_ALL(sib, field); \ + else \ + RB_FLIP_RIGHT(sib, field); \ RB_BITS(elm, field) |= RB_RED_MASK; \ sib = elm; \ + break; \ + case RB_RED_L: \ + if (RB_STRICT_HST && elm != NULL) { \ + RB_FLIP_RIGHT(parent, field); \ + RB_FLIP_ALL(sib, field); \ + break; \ + } \ + RB_FLIP_LEFT(parent, field); \ + /* FALLTHROUGH */ \ + default: \ + RB_FLIP_RIGHT(sib, field); \ + break; \ } \ RB_ROTATE_LEFT(head, parent, sib, field); \ } else { \ @@ -582,24 +608,36 @@ name##_RB_REMOVE_COLOR(struct name *head, \ continue; \ } \ sib = RB_LEFT(parent, field); \ - if ((~RB_BITS(sib, field) & RB_RED_MASK) == 0) {\ - RB_BITS(sib, field) &= ~RB_RED_MASK; \ + switch (RB_BITS(sib, field) & RB_RED_MASK) { \ + case RB_RED_MASK: \ + RB_FLIP_ALL(sib, field); \ elm = parent; \ continue; \ - } \ - RB_FLIP_LEFT(sib, field); \ - if (RB_RED_RIGHT(sib, field)) \ - RB_FLIP_RIGHT(parent, field); \ - else if (!RB_RED_LEFT(sib, field)) { \ - RB_FLIP_RIGHT(parent, field); \ + case RB_RED_L: \ elm = RB_RIGHT(sib, field); \ RB_ROTATE_LEFT(head, sib, elm, field); \ - if (RB_RED_LEFT(elm, field)) \ - RB_FLIP_RIGHT(sib, field); \ if (RB_RED_RIGHT(elm, field)) \ - RB_FLIP_LEFT(parent, field); \ + RB_FLIP_ALL(parent, field); \ + else \ + RB_FLIP_RIGHT(parent, field); \ + if (RB_RED_LEFT(elm, field)) \ + RB_FLIP_ALL(sib, field); \ + else \ + RB_FLIP_LEFT(sib, field); \ RB_BITS(elm, field) |= RB_RED_MASK; \ sib = elm; \ + break; \ + case RB_RED_R: \ + if (RB_STRICT_HST && elm != NULL) { \ + RB_FLIP_LEFT(parent, field); \ + RB_FLIP_ALL(sib, field); \ + break; \ + } \ + RB_FLIP_RIGHT(parent, field); \ + /* FALLTHROUGH */ \ + default: \ + RB_FLIP_LEFT(sib, field); \ + break; \ } \ RB_ROTATE_RIGHT(head, parent, sib, field); \ } \