From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752965AbdDKGzT (ORCPT ); Tue, 11 Apr 2017 02:55:19 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:40720 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751527AbdDKGzR (ORCPT ); Tue, 11 Apr 2017 02:55:17 -0400 From: Leno Hou To: linux-kernel@vger.kernel.org Cc: Leno Hou Subject: [PATCH v1]] lib/btree.c: optimise the code by previously getpos function Date: Tue, 11 Apr 2017 02:53:56 -0400 X-Mailer: git-send-email 1.8.3.1 X-TM-AS-GCONF: 00 x-cbid: 17041106-0048-0000-0000-000001553B28 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00006915; HX=3.00000240; KW=3.00000007; PH=3.00000004; SC=3.00000208; SDB=6.00845913; UDB=6.00417191; IPR=6.00624346; BA=6.00005282; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00015001; XFM=3.00000013; UTC=2017-04-11 06:55:14 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17041106-0049-0000-0000-000040660D03 Message-Id: <1491893636-60759-1-git-send-email-lenohou@gmail.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-04-11_06:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1702020001 definitions=main-1704110061 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch optimized the code by previously getpos function call. Therefore, It's takes the convenience to understand logic of code. Signed-off-by: Leno Hou --- lib/btree.c | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/lib/btree.c b/lib/btree.c index f93a945..87d690f 100644 --- a/lib/btree.c +++ b/lib/btree.c @@ -238,6 +238,19 @@ 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) +{ + int i; + + for (i = 0; i < geo->no_pairs; i++) { + if (keycmp(geo, node, i, key) <= 0) + break; + } + return i; +} + + void *btree_lookup(struct btree_head *head, struct btree_geo *geo, unsigned long *key) { @@ -248,9 +261,7 @@ 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; + i = getpos(geo, node, key); if (i == geo->no_pairs) return NULL; node = bval(geo, node, i); @@ -278,9 +289,7 @@ 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; + i = getpos(geo, node, key); if (i == geo->no_pairs) return -ENOENT; node = bval(geo, node, i); @@ -326,9 +335,7 @@ 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; + i = getpos(geo, node, key); if (i == geo->no_pairs) goto miss; oldnode = node; @@ -360,18 +367,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,9 +387,7 @@ 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)) { /* right-most key is too large, update it */ -- 1.8.3.1