Skip to content

Commit 2361b65

Browse files
committed
Switch to binary search in codemap.lookup_pos
Patrick observed excessive slowness when looking up positions in rustc. This might help.
1 parent d3eb3b4 commit 2361b65

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

src/comp/front/codemap.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,22 @@ fn next_line(filemap file, uint pos) {
2828
}
2929

3030
fn lookup_pos(codemap map, uint pos) -> loc {
31-
auto i = _vec.len[filemap](map.files);
32-
while (i > 0u) {
33-
i -= 1u;
34-
auto f = map.files.(i);
35-
if (f.start_pos <= pos) {
36-
// FIXME this can be a binary search if we need to be faster
37-
auto line = _vec.len[uint](f.lines);
38-
while (line > 0u) {
39-
line -= 1u;
40-
auto line_start = f.lines.(line);
41-
if (line_start <= pos) {
42-
ret rec(filename=f.name,
43-
line=line + 1u,
44-
col=pos-line_start);
45-
}
46-
}
47-
}
31+
auto a = 0u; auto b = _vec.len[filemap](map.files);
32+
while (b - a > 1u) {
33+
auto m = (a + b) / 2u;
34+
if (map.files.(m).start_pos > pos) { b = m; }
35+
else { a = m; }
4836
}
49-
log_err #fmt("Failed to find a location for character %u", pos);
50-
fail;
37+
auto f = map.files.(a);
38+
a = 0u; b = _vec.len[uint](f.lines);
39+
while (b - a > 1u) {
40+
auto m = (a + b) / 2u;
41+
if (f.lines.(m) > pos) { b = m; }
42+
else { a = m; }
43+
}
44+
ret rec(filename=f.name,
45+
line=a + 1u,
46+
col=pos - f.lines.(a));
5147
}
5248

5349
//

0 commit comments

Comments
 (0)