Skip to content

Commit 7e446af

Browse files
committed
std::trie: make lower_bound and upper_bound about 15% faster.
I believe this is mainly due to code-size reduction. Before: test [...]::bench_lower_bound ... bench: 818 ns/iter (+/- 100) test [...]::bench_upper_bound ... bench: 939 ns/iter (+/- 34) After: test [...]::bench_lower_bound ... bench: 698 ns/iter (+/- 60) test [...]::bench_upper_bound ... bench: 817 ns/iter (+/- 20)
1 parent 3395f9d commit 7e446af

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/libstd/trie.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,24 +198,24 @@ macro_rules! bound {
198198
addr!(loop {
199199
let children = unsafe {addr!(& $($mut_)* (*node).children)};
200200
let child_id = chunk(key, idx);
201-
match children[child_id] {
201+
let (slice_idx, ret) = match children[child_id] {
202202
Internal(ref $($mut_)* n) => {
203203
node = addr!(& $($mut_)* **n as * $($mut_)* TrieNode<T>);
204+
(child_id + 1, false)
204205
}
205206
External(stored, _) => {
206-
if stored < key || ($upper && stored == key) {
207-
it.stack.push(children.$slice_from(child_id + 1).$iter());
207+
(if stored < key || ($upper && stored == key) {
208+
child_id + 1
208209
} else {
209-
it.stack.push(children.$slice_from(child_id).$iter());
210-
}
211-
return it;
210+
child_id
211+
}, true)
212212
}
213213
Nothing => {
214-
it.stack.push(children.$slice_from(child_id + 1).$iter());
215-
return it
214+
(child_id + 1, true)
216215
}
217-
}
218-
it.stack.push(children.$slice_from(child_id + 1).$iter());
216+
};
217+
it.stack.push(children.$slice_from(slice_idx).$iter());
218+
if ret { return it }
219219
idx += 1;
220220
})
221221
}

0 commit comments

Comments
 (0)