Skip to content

Commit 015a4fb

Browse files
committed
Drop unnecessary C code.
1 parent ac5fbde commit 015a4fb

File tree

1 file changed

+20
-63
lines changed

1 file changed

+20
-63
lines changed

lib/redis/hash_ring.rb

Lines changed: 20 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -60,72 +60,29 @@ def iter_nodes(key)
6060
end
6161
end
6262

63-
class << self
64-
65-
# gem install RubyInline to use this code
66-
# Native extension to perform the binary search within the hashring.
67-
# There's a pure ruby version below so this is purely optional
68-
# for performance. In testing 20k gets and sets, the native
69-
# binary search shaved about 12% off the runtime (9sec -> 8sec).
70-
begin
71-
require 'inline'
72-
inline do |builder|
73-
builder.c <<-EOM
74-
int binary_search(VALUE ary, unsigned int r) {
75-
int upper = RARRAY_LEN(ary) - 1;
76-
int lower = 0;
77-
int idx = 0;
78-
79-
while (lower <= upper) {
80-
idx = (lower + upper) / 2;
81-
82-
VALUE continuumValue = RARRAY_PTR(ary)[idx];
83-
unsigned int l = NUM2UINT(continuumValue);
84-
if (l == r) {
85-
return idx;
86-
}
87-
else if (l > r) {
88-
upper = idx - 1;
89-
}
90-
else {
91-
lower = idx + 1;
92-
}
93-
}
94-
if (upper < 0) {
95-
upper = RARRAY_LEN(ary) - 1;
96-
}
97-
return upper;
98-
}
99-
EOM
100-
end
101-
rescue Exception
102-
# Find the closest index in HashRing with value <= the given value
103-
def binary_search(ary, value, &block)
104-
upper = ary.size - 1
105-
lower = 0
106-
idx = 0
107-
108-
while(lower <= upper) do
109-
idx = (lower + upper) / 2
110-
comp = ary[idx] <=> value
111-
112-
if comp == 0
113-
return idx
114-
elsif comp > 0
115-
upper = idx - 1
116-
else
117-
lower = idx + 1
118-
end
119-
end
120-
121-
if upper < 0
122-
upper = ary.size - 1
123-
end
124-
return upper
63+
# Find the closest index in HashRing with value <= the given value
64+
def self.binary_search(ary, value, &block)
65+
upper = ary.size - 1
66+
lower = 0
67+
idx = 0
68+
69+
while(lower <= upper) do
70+
idx = (lower + upper) / 2
71+
comp = ary[idx] <=> value
72+
73+
if comp == 0
74+
return idx
75+
elsif comp > 0
76+
upper = idx - 1
77+
else
78+
lower = idx + 1
12579
end
80+
end
12681

82+
if upper < 0
83+
upper = ary.size - 1
12784
end
85+
return upper
12886
end
129-
13087
end
13188
end

0 commit comments

Comments
 (0)