From nobody Fri Aug 19 23:14:08 2022 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 4M8cyT1NlDz4ZRXV; Fri, 19 Aug 2022 23:14:09 +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 4M8cyT0rf1z3tyX; Fri, 19 Aug 2022 23:14:09 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1660950849; 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=NGybyB1rOCnfLa5jpj0F3NEvQ+CtoaQsfWcYGCbTOhw=; b=bznY8RymHrTCIW6/Eu/W4oacfVgcRc2wtVAAuYXaqH7/DURlN5zToTFYjMZBF2J9vjIu6t GPpU8O1Fc1AiJCtD2q7NV9j6Le7jvF9S6+F57a527FilcsxQFWDT0R3GlMOVqOyNmcu8cA oNf2VNis+9Z3NWQnANDRA0Z3BpB7OMWzCAdPvHsuDXC6BVBqY1osM8ea2+VKrvvras7585 k2ZykOSNxrgJYzWwRzLUAI5cav2RhQ7t1pQq+XGeGUPOUKiMzCuYlj/RN9qCz5swwR2HX3 mv3h9UYR9G4KYgGT2cOVM1J0sVIT388TrZjtxN7wKdfQPk86F2u7ZMHqgnUL9g== 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 4M8cyS71Rsz19BW; Fri, 19 Aug 2022 23:14:08 +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 27JNE8nu039234; Fri, 19 Aug 2022 23:14:08 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 27JNE8o3039233; Fri, 19 Aug 2022 23:14:08 GMT (envelope-from git) Date: Fri, 19 Aug 2022 23:14:08 GMT Message-Id: <202208192314.27JNE8o3039233@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: 02d0c43c9e53 - main - rb_tree: speed-up double rotation 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: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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: 02d0c43c9e53b3055b17719a184a813032040f79 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1660950849; 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=NGybyB1rOCnfLa5jpj0F3NEvQ+CtoaQsfWcYGCbTOhw=; b=h7rS266iA/djYsgcR3o6XT/GtQUcpj9+MSDUyQph/VR0xzgdzmit9K5RDsrprGmBRg9syA 0wgrQwciUYrlhseRHEj97PptZ/VJjYZ5A10TrUIS8qC6jGJLXXa5wBhAgLk/sU/jNsBvme y/qhLWw4XenYsuLLkNCQsytMwQ1vpW4CbgRe3IOH6m3VPKycq7WLNz+617uYO7XjHH84N7 hKPw0HXO/tVoKr3QD3dbdjweEgiVWt//hcaEQ7w+wSfwHDwHyxAcprcWEfoOnpUXYWDVod CgNTRGggZvPWfyu1hdrWzA4VFQ71avvSmXDuNEkCQ0LBe4Taetm1igm5ULz/Kg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1660950849; a=rsa-sha256; cv=none; b=oEBS89tSVUtu0sqcht4Vp7pySoA7vaMJmD980SGgLaWi1R/Sppv/5FlssaORK3500qcb7w I2kYeIVyn9XrVzClBo8k1vo8jOlqjakITMBRRPHC3iZvVvSYk/bLdk3HeasbWwwXDCcmF7 RBUYZHaV0ufrw1MGCn5t2haqT6NKzSxSZF6Yqt023JQ9ZWv97iAuFyK9E3V6DBAvl9NbrQ Iw1SvDlS59ovOJWXN48Zt5jruuELPwDEz1gup9mTesBDYAduFhpTqci/Fn9ZR5foUX/N6v ar4wb/BRUj+RbvlzJ9o7EGt8lDTPKSyzUTswpRp6CiFPH3kfTx5OOodijMp2aA== 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=02d0c43c9e53b3055b17719a184a813032040f79 commit 02d0c43c9e53b3055b17719a184a813032040f79 Author: Doug Moore AuthorDate: 2022-08-19 23:11:29 +0000 Commit: Doug Moore CommitDate: 2022-08-19 23:11:29 +0000 rb_tree: speed-up double rotation RB_ROTATE_LEFT (and it symmetric twin) modify the rb-tree, adjusting pointers so that what started as a proper tree ends up a proper tree. When two consecutive rotations move the same node up the tree, some of the pointers changed in the first rotation are immediately changed again in the second - namely, the pointer from the rising node to its new parent, and the pointer from that parent back to the rising node. This change removes from RB_ROTATE macros the responsibility for managing those two pointers, and leaves it to the code that calls for rotations to fix up those pointers afterward. That drops a comparison and a pair of assignments from every INSERT_COLOR or REMOVE_COLOR call that ends in a double rotation. A side-effect of this change is that the SWAP_CHILD macro must take as a parameter a pointer to the node that is changing children, where it is now computed from the old child. Since this macro is called in a couple of places besides the RB_ROTATE macros, those calls are also affected. Reviewed by: alc MFC after: 3 weeks Differential Revision: https://reviews.freebsd.org/D36266 --- sys/compat/linuxkpi/common/include/linux/rbtree.h | 3 +- sys/sys/tree.h | 63 ++++++++++++++--------- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/sys/compat/linuxkpi/common/include/linux/rbtree.h b/sys/compat/linuxkpi/common/include/linux/rbtree.h index de5ef0d6a3ce..1db9b1d4fa21 100644 --- a/sys/compat/linuxkpi/common/include/linux/rbtree.h +++ b/sys/compat/linuxkpi/common/include/linux/rbtree.h @@ -132,7 +132,8 @@ rb_replace_node(struct rb_node *victim, struct rb_node *new, struct rb_root *root) { - RB_SWAP_CHILD((struct linux_root *)root, victim, new, __entry); + RB_SWAP_CHILD((struct linux_root *)root, rb_parent(victim), + victim, new, __entry); if (victim->rb_left) RB_SET_PARENT(victim->rb_left, new, __entry); if (victim->rb_right) diff --git a/sys/sys/tree.h b/sys/sys/tree.h index d82c341049c6..1d4271c2144a 100644 --- a/sys/sys/tree.h +++ b/sys/sys/tree.h @@ -378,31 +378,40 @@ struct { \ } while ((rb_update_tmp = RB_PARENT(rb_update_tmp, field)) != NULL); \ } while (0) -#define RB_SWAP_CHILD(head, out, in, field) do { \ - if (RB_PARENT(out, field) == NULL) \ +#define RB_SWAP_CHILD(head, par, out, in, field) do { \ + if (par == NULL) \ RB_ROOT(head) = (in); \ - else if ((out) == RB_LEFT(RB_PARENT(out, field), field)) \ - RB_LEFT(RB_PARENT(out, field), field) = (in); \ + else if ((out) == RB_LEFT(par, field)) \ + RB_LEFT(par, field) = (in); \ else \ - RB_RIGHT(RB_PARENT(out, field), field) = (in); \ + RB_RIGHT(par, field) = (in); \ } while (/*CONSTCOND*/ 0) -#define RB_ROTATE_LEFT(head, elm, tmp, field) do { \ +/* + * RB_ROTATE macros partially restructure the tree to improve + * balance. The ROTATE_RIGHT case is just a reflection of the + * ROTATE_LEFT case. Initially, tmp is a right child of elm. After + * rotation, elm is a left child of tmp, and the subtree that + * represented the items between them, which formerly hung to the left + * of tmp now hangs to the right of elm. The parent-child + * relationship between elm and its former parent is not changed; + * where this macro once updated those fields, that is now left to the + * caller of RB_ROTATE to clean up, so that a pair of rotations does + * not twice update the came pair of pointer fields with distinct + * values. + */ +#define RB_ROTATE_LEFT(elm, tmp, field) do { \ if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \ RB_SET_PARENT(RB_RIGHT(elm, field), elm, field); \ } \ - RB_SET_PARENT(tmp, RB_PARENT(elm, field), field); \ - RB_SWAP_CHILD(head, elm, tmp, field); \ RB_LEFT(tmp, field) = (elm); \ RB_SET_PARENT(elm, tmp, field); \ } while (/*CONSTCOND*/ 0) -#define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \ +#define RB_ROTATE_RIGHT(elm, tmp, field) do { \ if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \ RB_SET_PARENT(RB_LEFT(elm, field), elm, field); \ } \ - RB_SET_PARENT(tmp, RB_PARENT(elm, field), field); \ - RB_SWAP_CHILD(head, elm, tmp, field); \ RB_RIGHT(tmp, field) = (elm); \ RB_SET_PARENT(elm, tmp, field); \ } while (/*CONSTCOND*/ 0) @@ -479,8 +488,9 @@ name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ * when a value has been assigned to 'child' in the previous \ * one. \ */ \ - struct type *child, *parent; \ + struct type *child, *gpar, *parent; \ while ((parent = RB_PARENT(elm, field)) != NULL) { \ + gpar = RB_PARENT(parent, field); \ if (RB_LEFT(parent, field) == elm) { \ if (RB_RED_LEFT(parent, field)) { \ RB_FLIP_LEFT(parent, field); \ @@ -496,7 +506,7 @@ name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ child = elm; \ else { \ /* coverity[uninit_use] */ \ - RB_ROTATE_LEFT(head, elm, child, field);\ + RB_ROTATE_LEFT(elm, child, field); \ if (RB_RED_RIGHT(child, field)) \ RB_FLIP_LEFT(parent, field); \ if (RB_RED_LEFT(child, field)) \ @@ -507,7 +517,7 @@ name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ RB_RED_MASK) == 0) \ elm = child; \ } \ - RB_ROTATE_RIGHT(head, parent, child, field); \ + RB_ROTATE_RIGHT(parent, child, field); \ } else { \ if (RB_RED_RIGHT(parent, field)) { \ RB_FLIP_RIGHT(parent, field); \ @@ -523,7 +533,7 @@ name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ child = elm; \ else { \ /* coverity[uninit_use] */ \ - RB_ROTATE_RIGHT(head, elm, child, field);\ + RB_ROTATE_RIGHT(elm, child, field); \ if (RB_RED_LEFT(child, field)) \ RB_FLIP_RIGHT(parent, field); \ if (RB_RED_RIGHT(child, field)) \ @@ -534,8 +544,10 @@ name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ RB_RED_MASK) == 0) \ elm = child; \ } \ - RB_ROTATE_LEFT(head, parent, child, field); \ + RB_ROTATE_LEFT(parent, child, field); \ } \ + RB_SET_PARENT(child, gpar, field); \ + RB_SWAP_CHILD(head, gpar, parent, child, field); \ RB_BITS(child, field) &= ~RB_RED_MASK; \ if (elm != child) \ RB_AUGMENT(elm); \ @@ -560,7 +572,7 @@ attr void \ name##_RB_REMOVE_COLOR(struct name *head, \ struct type *parent, struct type *elm) \ { \ - struct type *sib; \ + struct type *gpar, *sib; \ if (RB_LEFT(parent, field) == elm && \ RB_RIGHT(parent, field) == elm) { \ RB_BITS(parent, field) &= ~RB_RED_MASK; \ @@ -570,6 +582,7 @@ name##_RB_REMOVE_COLOR(struct name *head, \ return; \ } \ do { \ + gpar = RB_PARENT(parent, field); \ if (RB_LEFT(parent, field) == elm) { \ if (!RB_RED_LEFT(parent, field)) { \ RB_FLIP_LEFT(parent, field); \ @@ -588,7 +601,7 @@ name##_RB_REMOVE_COLOR(struct name *head, \ continue; \ case RB_RED_R: \ elm = RB_LEFT(sib, field); \ - RB_ROTATE_RIGHT(head, sib, elm, field); \ + RB_ROTATE_RIGHT(sib, elm, field); \ if (RB_RED_LEFT(elm, field)) \ RB_FLIP_ALL(parent, field); \ else \ @@ -613,7 +626,7 @@ name##_RB_REMOVE_COLOR(struct name *head, \ elm = sib; \ break; \ } \ - RB_ROTATE_LEFT(head, parent, elm, field); \ + RB_ROTATE_LEFT(parent, elm, field); \ } else { \ if (!RB_RED_RIGHT(parent, field)) { \ RB_FLIP_RIGHT(parent, field); \ @@ -632,7 +645,7 @@ name##_RB_REMOVE_COLOR(struct name *head, \ continue; \ case RB_RED_L: \ elm = RB_RIGHT(sib, field); \ - RB_ROTATE_LEFT(head, sib, elm, field); \ + RB_ROTATE_LEFT(sib, elm, field); \ if (RB_RED_RIGHT(elm, field)) \ RB_FLIP_ALL(parent, field); \ else \ @@ -657,8 +670,10 @@ name##_RB_REMOVE_COLOR(struct name *head, \ elm = sib; \ break; \ } \ - RB_ROTATE_RIGHT(head, parent, elm, field); \ + RB_ROTATE_RIGHT(parent, elm, field); \ } \ + RB_SET_PARENT(elm, gpar, field); \ + RB_SWAP_CHILD(head, gpar, parent, elm, field); \ if (sib != elm) \ RB_AUGMENT(sib); \ break; \ @@ -669,10 +684,10 @@ name##_RB_REMOVE_COLOR(struct name *head, \ attr struct type * \ name##_RB_REMOVE(struct name *head, struct type *elm) \ { \ - struct type *child, *old, *parent, *right; \ + struct type *child, *gpar, *old, *parent, *right; \ \ old = elm; \ - parent = RB_PARENT(elm, field); \ + gpar = parent = RB_PARENT(elm, field); \ right = RB_RIGHT(elm, field); \ if (RB_LEFT(elm, field) == NULL) \ elm = child = right; \ @@ -695,7 +710,7 @@ name##_RB_REMOVE(struct name *head, struct type *elm) \ RB_SET_PARENT(RB_LEFT(old, field), elm, field); \ elm->field = old->field; \ } \ - RB_SWAP_CHILD(head, old, elm, field); \ + RB_SWAP_CHILD(head, gpar, old, elm, field); \ if (child != NULL) \ RB_SET_PARENT(child, parent, field); \ if (parent != NULL) { \