Skip to content

Commit ea0a411

Browse files
byrootsamyron
authored andcommitted
[ruby/json] parser.c: simplify sorted insert loop in rstring_cache_fetch
ruby/json@31453b8e95 Co-Authored-By: Scott Myron <samyron@gmail.com>
1 parent edebd83 commit ea0a411

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

ext/json/parser/parser.c

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,15 @@ static VALUE rstring_cache_fetch(rvalue_cache *cache, const char *str, const lon
105105

106106
int low = 0;
107107
int high = cache->length - 1;
108-
int mid = 0;
109-
int last_cmp = 0;
110108

111109
while (low <= high) {
112-
mid = (high + low) >> 1;
110+
int mid = (high + low) >> 1;
113111
VALUE entry = cache->entries[mid];
114-
last_cmp = rstring_cache_cmp(str, length, entry);
112+
int cmp = rstring_cache_cmp(str, length, entry);
115113

116-
if (last_cmp == 0) {
114+
if (cmp == 0) {
117115
return entry;
118-
} else if (last_cmp > 0) {
116+
} else if (cmp > 0) {
119117
low = mid + 1;
120118
} else {
121119
high = mid - 1;
@@ -125,11 +123,7 @@ static VALUE rstring_cache_fetch(rvalue_cache *cache, const char *str, const lon
125123
VALUE rstring = build_interned_string(str, length);
126124

127125
if (cache->length < JSON_RVALUE_CACHE_CAPA) {
128-
if (last_cmp > 0) {
129-
mid += 1;
130-
}
131-
132-
rvalue_cache_insert_at(cache, mid, rstring);
126+
rvalue_cache_insert_at(cache, low, rstring);
133127
}
134128
return rstring;
135129
}
@@ -151,17 +145,15 @@ static VALUE rsymbol_cache_fetch(rvalue_cache *cache, const char *str, const lon
151145

152146
int low = 0;
153147
int high = cache->length - 1;
154-
int mid = 0;
155-
int last_cmp = 0;
156148

157149
while (low <= high) {
158-
mid = (high + low) >> 1;
150+
int mid = (high + low) >> 1;
159151
VALUE entry = cache->entries[mid];
160-
last_cmp = rstring_cache_cmp(str, length, rb_sym2str(entry));
152+
int cmp = rstring_cache_cmp(str, length, rb_sym2str(entry));
161153

162-
if (last_cmp == 0) {
154+
if (cmp == 0) {
163155
return entry;
164-
} else if (last_cmp > 0) {
156+
} else if (cmp > 0) {
165157
low = mid + 1;
166158
} else {
167159
high = mid - 1;
@@ -171,11 +163,7 @@ static VALUE rsymbol_cache_fetch(rvalue_cache *cache, const char *str, const lon
171163
VALUE rsymbol = build_symbol(str, length);
172164

173165
if (cache->length < JSON_RVALUE_CACHE_CAPA) {
174-
if (last_cmp > 0) {
175-
mid += 1;
176-
}
177-
178-
rvalue_cache_insert_at(cache, mid, rsymbol);
166+
rvalue_cache_insert_at(cache, low, rsymbol);
179167
}
180168
return rsymbol;
181169
}

0 commit comments

Comments
 (0)