Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1639274

[PATCH v2] lib/btree.c: optimise the code by previously getpos function

Path csiph.com!news.redatomik.org!aioe.org!news.servidellagleba.it!bofh.it!news.nic.it!robomod
From Leno Hou <lenohou@gmail.com>
Newsgroups linux.kernel
Subject [PATCH v2] lib/btree.c: optimise the code by previously getpos function
Date Thu, 11 May 2017 11:50:01 +0200
Message-ID <tFSTn-4CQ-11@gated-at.bofh.it> (permalink)
X-Original-To linux-kernel@vger.kernel.org
Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=xIJ3PCg0Bd+kh/UntVfSxYF4Zl/m+dSzZjYdxLmt4FI=; b=KFh2Lxf08g0XYXVJUQHolZkQL4EiCngXP+EmaBUdJk4gjpuZmIu44POgPM8s2HZedK 219jBYmQ1Bqgm1I1CvPHnmt8BgV3+ovaJAe3jqSa798jIgIroN0xdQKmRrONwc3nLGjT rUDj4hurOj5Vu5FOUQEGTRUwZPVSQmH8pGKfyb5WDjSD35Bajmc0mE+J77G3Hmr1rNyT bwZpYPJ1HgJcA0tdLjCuZv/Ol46lX9Jf1qSXxrZVKheF9mkTWA5lOSqxS9XqchMChf73 pfljt0mci6gOLJFlIaT8AHiNaMeb/22BhNqEtMpFUX1jqC3F5If5CEK9TWhn/NtqfDXB 6Y0g==
X-Google-Dkim-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=xIJ3PCg0Bd+kh/UntVfSxYF4Zl/m+dSzZjYdxLmt4FI=; b=NHlBtNlz9jWXcGkUkZ0C7tLBP5rIVi251Y8yQTwJqd6qjAy/OJSkexlgqjYw/f/pe+ BR0ieGz5GTSLEfuaMRRBTtf8z3OrJ/WTID1NM74iCJK9TIEWqrN3tXdwqDO1vRE1xxN8 41u8M/ioz1O2D31IgbioI74qoZcATDQRHtpSFt1BJmBh7JLOZWsO183EqSDvvzgZBenj dlcU+wTxbz8HHiVqZpPmbAXLSUu90C0Gbpc2nBpMzdClY8LB3luQ3OVE9H/uHsjYLxHc mA5cNllBvFrPkBhtUipkjhqczFRAtv94f5hSlody3fFxbVfksZLA9tmEBQlJGOQRgZJs SjMg==
X-Gm-Message-State AODbwcB2n6i9yxdq45LR4aVwBtTznLYVvXNJBXRCCkNRWgrK8c+ZKtsZ jwltWp9KMxuxyA==
X-Received by 10.237.63.69 with SMTP id q5mr3021724qtf.232.1494495747607; Thu, 11 May 2017 02:42:27 -0700 (PDT)
X-Mailer git-send-email 2.7.4
Sender robomod@news.nic.it
List-ID <linux-kernel.vger.kernel.org>
X-Mailing-List linux-kernel@vger.kernel.org
Approved robomod@news.nic.it
Lines 120
Organization linux.* mail to news gateway
X-Original-Cc andy.shevchenko@gmail.com, hch@infradead.org, Leno Hou <lenohou@gmail.com>
X-Original-Date Thu, 11 May 2017 17:42:21 +0800
X-Original-Message-ID <1494495741-30760-1-git-send-email-lenohou@gmail.com>
X-Original-Sender linux-kernel-owner@vger.kernel.org
Xref csiph.com linux.kernel:1639274

Show key headers only | View raw


This patch optimized the code by previously getpos function call.
Therefore, It's takes the convenience to understand logic of code.

v2:
  - getpos() returns errorno if not found the key

v1:
  - cleanup code by using getpos()

Signed-off-by: Leno Hou <lenohou@gmail.com>
---
 lib/btree.c | 50 +++++++++++++++++++++-----------------------------
 1 file changed, 21 insertions(+), 29 deletions(-)

diff --git a/lib/btree.c b/lib/btree.c
index f93a945..8604773 100644
--- a/lib/btree.c
+++ b/lib/btree.c
@@ -238,6 +238,18 @@ static int keyzero(struct btree_geo *geo, unsigned long *key)
 	return 1;
 }
 
+static int getpos(struct btree_geo *geo, unsigned long *node,
+		unsigned long *key)
+{
+	unsigned int i;
+
+	for (i = 0; i < geo->no_pairs; i++) {
+		if (keycmp(geo, node, i, key) <= 0)
+			return i;
+	}
+	return -ENOENT;
+}
+
 void *btree_lookup(struct btree_head *head, struct btree_geo *geo,
 		unsigned long *key)
 {
@@ -248,10 +260,8 @@ void *btree_lookup(struct btree_head *head, struct btree_geo *geo,
 		return NULL;
 
 	for ( ; height > 1; height--) {
-		for (i = 0; i < geo->no_pairs; i++)
-			if (keycmp(geo, node, i, key) <= 0)
-				break;
-		if (i == geo->no_pairs)
+		i = getpos(geo, node, key);
+		if (i < 0)
 			return NULL;
 		node = bval(geo, node, i);
 		if (!node)
@@ -278,10 +288,8 @@ int btree_update(struct btree_head *head, struct btree_geo *geo,
 		return -ENOENT;
 
 	for ( ; height > 1; height--) {
-		for (i = 0; i < geo->no_pairs; i++)
-			if (keycmp(geo, node, i, key) <= 0)
-				break;
-		if (i == geo->no_pairs)
+		i = getpos(geo, node, key);
+		if (i < 0)
 			return -ENOENT;
 		node = bval(geo, node, i);
 		if (!node)
@@ -326,10 +334,8 @@ void *btree_get_prev(struct btree_head *head, struct btree_geo *geo,
 
 	node = head->node;
 	for (height = head->height ; height > 1; height--) {
-		for (i = 0; i < geo->no_pairs; i++)
-			if (keycmp(geo, node, i, key) <= 0)
-				break;
-		if (i == geo->no_pairs)
+		i = getpos(geo, node, key);
+		if (i < 0)
 			goto miss;
 		oldnode = node;
 		node = bval(geo, node, i);
@@ -360,18 +366,6 @@ void *btree_get_prev(struct btree_head *head, struct btree_geo *geo,
 }
 EXPORT_SYMBOL_GPL(btree_get_prev);
 
-static int getpos(struct btree_geo *geo, unsigned long *node,
-		unsigned long *key)
-{
-	int i;
-
-	for (i = 0; i < geo->no_pairs; i++) {
-		if (keycmp(geo, node, i, key) <= 0)
-			break;
-	}
-	return i;
-}
-
 static int getfill(struct btree_geo *geo, unsigned long *node, int start)
 {
 	int i;
@@ -392,11 +386,9 @@ static unsigned long *find_level(struct btree_head *head, struct btree_geo *geo,
 	int i, height;
 
 	for (height = head->height; height > level; height--) {
-		for (i = 0; i < geo->no_pairs; i++)
-			if (keycmp(geo, node, i, key) <= 0)
-				break;
+		i = getpos(geo, node, key);
 
-		if ((i == geo->no_pairs) || !bval(geo, node, i)) {
+		if ((i < 0) || !bval(geo, node, i)) {
 			/* right-most key is too large, update it */
 			/* FIXME: If the right-most key on higher levels is
 			 * always zero, this wouldn't be necessary. */
@@ -466,7 +458,7 @@ static int btree_insert_level(struct btree_head *head, struct btree_geo *geo,
 	/* two identical keys are not allowed */
 	BUG_ON(pos < fill && keycmp(geo, node, pos, key) == 0);
 
-	if (fill == geo->no_pairs) {
+	if (fill < 0) {
 		/* need to split node */
 		unsigned long *new;
 
-- 
2.7.4

Back to linux.kernel | Previous | NextNext in thread | Find similar | Unroll thread


Thread

[PATCH v2] lib/btree.c: optimise the code by previously getpos function Leno Hou <lenohou@gmail.com> - 2017-05-11 11:50 +0200
  Re: [PATCH v2] lib/btree.c: optimise the code by previously getpos function Andy Shevchenko <andy.shevchenko@gmail.com> - 2017-05-11 13:10 +0200

csiph-web