@@ -60,72 +60,29 @@ def iter_nodes(key)
60
60
end
61
61
end
62
62
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
125
79
end
80
+ end
126
81
82
+ if upper < 0
83
+ upper = ary . size - 1
127
84
end
85
+ return upper
128
86
end
129
-
130
87
end
131
88
end
0 commit comments