[Sheepdog] [PATCH v2 2/2] use binary search in hval_to_sheep()
levin li
levin108 at gmail.com
Mon May 14 08:29:29 CEST 2012
As we know, binary search is much faster than sequential search,
we can use binary search to make hval_to_sheep() faster.
Signed-off-by: levin li <xingke.lwp at taobao.com>
---
Patch update:
add a bsearch helper bsearch_in_entries()
include/sheep.h | 44 ++++++++++++++++++++++++++++----------------
1 file changed, 28 insertions(+), 16 deletions(-)
diff --git a/include/sheep.h b/include/sheep.h
index 4fd05f7..9537970 100644
--- a/include/sheep.h
+++ b/include/sheep.h
@@ -225,18 +225,35 @@ next:
return idx;
}
+static inline int bsearch_in_entries(struct sd_vnode *entries,
+ int nr_entries, uint64_t id)
+{
+ int start, end, pos;
+
+ start = 0;
+ end = nr_entries - 1;
+
+ if (id > entries[end].id)
+ return end;
+again:
+ pos = (end - start) / 2 + start;
+ if (entries[pos].id < id) {
+ if (entries[pos + 1].id >= id)
+ return pos;
+ start = pos;
+ } else
+ end = pos;
+ goto again;
+
+ return pos;
+}
+
static inline int hval_to_sheep(struct sd_vnode *entries,
int nr_entries, uint64_t id, int idx)
{
- int i;
- struct sd_vnode *e = entries, *n;
+ int pos = bsearch_in_entries(entries, nr_entries, id);
- for (i = 0; i < nr_entries - 1; i++, e++) {
- n = e + 1;
- if (id > e->id && id <= n->id)
- break;
- }
- return get_nth_node(entries, nr_entries, (i + 1) % nr_entries, idx);
+ return get_nth_node(entries, nr_entries, (pos + 1) % nr_entries, idx);
}
static inline int obj_to_sheep(struct sd_vnode *entries,
@@ -250,17 +267,12 @@ static inline int obj_to_sheep(struct sd_vnode *entries,
static inline void hval_to_sheeps(struct sd_vnode *entries,
int nr_entries, uint64_t id, int nr_copies, int *idxs)
{
- int i, idx;
- struct sd_vnode *e = entries, *n;
+ int idx;
+ int pos = bsearch_in_entries(entries, nr_entries, id);
- for (i = 0; i < nr_entries - 1; i++, e++) {
- n = e + 1;
- if (id > e->id && id <= n->id)
- break;
- }
for (idx = 0; idx < nr_copies; idx++)
idxs[idx] = get_nth_node(entries, nr_entries,
- (i + 1) % nr_entries, idx);
+ (pos + 1) % nr_entries, idx);
}
static inline void obj_to_sheeps(struct sd_vnode *entries,
--
1.7.10
More information about the sheepdog
mailing list