Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp4092318ybv; Mon, 10 Feb 2020 12:08:52 -0800 (PST) X-Google-Smtp-Source: APXvYqzYF+oWfehFx80OrvHmCah2F48uA5xyyVvgfTdJzuu1zpmPTSE0HAA6KMlWWc3KmoNinFnC X-Received: by 2002:aca:cdd0:: with SMTP id d199mr522249oig.49.1581365332028; Mon, 10 Feb 2020 12:08:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581365332; cv=none; d=google.com; s=arc-20160816; b=g+TmHvW2xPa8clB4Q5i7/ObxdioL9dlr/iwH+WHI5/MVV78oMLaRo7DVBg+LtTS4U/ UWRvPnkTHEoeQIKUHUeCd0/W6mdm86y+iQ2r2aeU3zev3ilY/6iByCHdAbucksGzRjsR bJ4VFDfSuDNOVYiF1rUgBHsgylIhEvA4QuIkXV7kp5lkv3rbw41TT/lTyktsK3aRLd7r DpV+KmSb0MDlE+fFffhNx3T8ZGy+qSNV5nLH1wYpgYlJgwdWg8DqgSf9KTBAsO7hpn6E bBykcvhB77EBIne3pfUW8QTyKX5T8SPId1ifLDuKeQ7cAfsouKzYQwkoXMhGD+v+d5G+ 8JeQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=n+cqML+P3N7yJQnV2zTRY/y5Nvd3dQGJHvto5Od+38s=; b=irsy4cgzWhl/7UEiWBWQKhWdOzjLb7H7yLmLgyHvM+ZkGG9WqkmZf3Zkx8JcBV1Qq+ t4cbxc1KmO/8c4lgQcuSLwSD/EyjsO+PZvvLIRjkvepa528qz97dASe0k85S+/JqWjdC 8BcHbVXOWUMjPDQHOsAmkfl+PygZ+bKdjlDbqyqWunSD+GChUF0+jSMClvgymtbEBP2z XJBj4P0hTbxJk+BYC9ZySgpKyTDeBUBsrHy1HmfPpqocs0KuFf3FsdLpR6SvFST/wYk0 yI66sPuXtPksMWswqnTRUKDF/WV0tI8rTDZ8IKyNOYXp9G1ZhcgbzBSOxVRltkS33T6T E7qQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m4si697933otr.268.2020.02.10.12.08.40; Mon, 10 Feb 2020 12:08:52 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727484AbgBJUHP (ORCPT + 99 others); Mon, 10 Feb 2020 15:07:15 -0500 Received: from mail-pl1-f196.google.com ([209.85.214.196]:33976 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726563AbgBJUHP (ORCPT ); Mon, 10 Feb 2020 15:07:15 -0500 Received: by mail-pl1-f196.google.com with SMTP id j7so3266335plt.1 for ; Mon, 10 Feb 2020 12:07:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=n+cqML+P3N7yJQnV2zTRY/y5Nvd3dQGJHvto5Od+38s=; b=nYw08oxZjmO4zn2+ikSa97mAagYwtfHTdBAbDM1y5lu4lPlcoKnlxF9No6DTfojLf9 tVgOHn68zq4suzeUF0j6D+fLYiICM/TGXaxFBQZWSZlAi4CQ6iimTStugdqn+gUkeDI+ DC7kQnav5A4bDkIRnszARnTfVqKxZOVAegwDGi7Ncc60q5cM8haAFpv4xCvJYo4v5Ntt MNMbRVRZIauqayUV67J9JyfkRrye5fDdMUFFeslwHEly4BaEcIVK8bT1JeQW7LTs1gBY MRfkZnuJDami5XArICdA3xAxB7hk7Cs/pXOrn8c8i32785FB5xcCVNDQtpnP2R4eKL37 V23g== X-Gm-Message-State: APjAAAUSuzkzgEzkJWISoJ2KfEFVpFX4v9hXzhzd9GGrBVoXKPtCiS3T 498yrZXHTjz3QyLVn1yHbfg= X-Received: by 2002:a17:902:864c:: with SMTP id y12mr14321858plt.8.1581365234327; Mon, 10 Feb 2020 12:07:14 -0800 (PST) Received: from 42.do-not-panic.com (42.do-not-panic.com. [157.230.128.187]) by smtp.gmail.com with ESMTPSA id m12sm244067pjf.25.2020.02.10.12.07.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2020 12:07:13 -0800 (PST) Received: by 42.do-not-panic.com (Postfix, from userid 1000) id 4EE8E4060F; Mon, 10 Feb 2020 20:07:12 +0000 (UTC) Date: Mon, 10 Feb 2020 20:07:12 +0000 From: Luis Chamberlain To: Davidlohr Bueso Cc: akpm@linux-foundation.org, linux-kernel@vger.kernel.org, broonie@kernel.org, alex.williamson@redhat.com, Davidlohr Bueso Subject: Re: [PATCH 1/5] lib/rbtree: introduce linked-list rbtree interface Message-ID: <20200210200712.GM11244@42.do-not-panic.com> References: <20200207180305.11092-1-dave@stgolabs.net> <20200207180305.11092-2-dave@stgolabs.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200207180305.11092-2-dave@stgolabs.net> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Feb 07, 2020 at 10:03:01AM -0800, Davidlohr Bueso wrote: > When doing in-order tree traversals, the rb_next() and rb_prev() helpers can > be sub-optimal as plain node representations incur in extra pointer chasing > up the tree to compute the corresponding node. This can impact negatively > in performance when traversals are a common thing. This, for example, is what > we do in our vm already to efficiently manage VMAs. > > This interface provides branchless O(1) I think including the word "branchless" does injustice to the optimization, just O(1) sells it to me more to how I read the code. Why is the "branchless" prefix needed here? > access to the first node as well as > both its in-order successor and predecessor. This is done at the cost of higher > memory footprint: mainly additional prev and next pointers for each node. Such > benefits can be seen in this table showing the amount of cycles it takes to > do a full tree traversal: > > +--------+--------------+-----------+ > | #nodes | plain rbtree | ll-rbtree | > +--------+--------------+-----------+ > | 10 | 138 | 24 | > | 100 | 7,200 | 425 | > | 1000 | 17,000 | 8,000 | > | 10000 | 501,090 | 222,500 | > +--------+--------------+-----------+ Sold, however I wonder if we can have *one new API* where based on just one Kconfig you either get the two pointers or not, the performance gain then would only be observed if this new kconfig entry is enabled. The benefit of this is that we don't shove the performance benefit down all user's throughts but rather this can be decided by distributions and system integrators. > diff --git a/Documentation/rbtree.txt b/Documentation/rbtree.txt > index 523d54b60087..fe38fb257931 100644 > --- a/Documentation/rbtree.txt > +++ b/Documentation/rbtree.txt > @@ -5,6 +5,7 @@ Red-black Trees (rbtree) in Linux > > :Date: January 18, 2007 > :Author: Rob Landley > + Davidlohr Bueso > > What are red-black trees, and what are they for? > ------------------------------------------------ > @@ -226,6 +227,79 @@ trees:: > struct rb_augment_callbacks *); > > > +Linked-list rbtrees > +------------------- > + > +When doing in-order tree traversals, the rb_next() and rb_prev() helpers can > +be sub-optimal as plain node representations incur in extra pointer chasing > +up the tree to compute the corresponding node. This can have a negative impact > +in latencies in scenarios where tree traversals are not rare. This interface > +provides branchless O(1) access to the first node as well as both its in-order > +successor and predecessor. This is done at the cost of higher memory footprint: > +mainly additional prev and next pointers for each node, essentially duplicating > +the tree data structure. While there are other node representations that optimize > +getting such pointers without bloating the nodes as much, such as keeping a > +parent pointer or threaded trees where the nil prev/next pointers are recycled; > +both incurring in higher runtime penalization for common modification operations > +as well as any rotations. > + > +As with regular rb_root structure, linked-list rbtrees are initialized to be > +empty via:: > + > + struct llrb_root mytree = LLRB_ROOT; > + > +Insertion and deletion are defined by: > + > + void llrb_insert(struct llrb_root *, struct llrb_node *, struct llrb_node *); > + void llrb_erase(struct llrb_node *, struct llrb_root *); > + > +The corresponding helpers needed to iterate through a tree are the following, > +equivalent to an optimized version of the regular rbtree version: > + > + struct llrb_node *llrb_first(struct llrb_root *tree); > + struct llrb_node *llrb_next(struct rb_node *node); > + struct llrb_node *llrb_prev(struct rb_node *node); > + > + > +Inserting data into a Linked-list rbtree > +---------------------------------------- > + > +Because llrb trees can exist anywhere regular rbtrees, the steps are similar. > +The search for insertion differs from the regular search in two ways. First > +the caller must keep track of the previous node, can you explain here why, even though its clear in the code: its because we need to pass it as a parameter when the new node is inserted into the rb tree. Also, what about a selftest for this? Reviewed-by: Luis Chamberlain Luis