Skip to content

Commit da355b8

Browse files
committed
use splice to optimize O(m*n) to O(m+n) in StyledBuffer::prepend
Signed-off-by: xizheyin <[email protected]>
1 parent a980cd4 commit da355b8

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

compiler/rustc_errors/src/styled_buffer.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,8 @@ impl StyledBuffer {
108108
self.ensure_lines(line);
109109
let string_len = string.chars().count();
110110

111-
if !self.lines[line].is_empty() {
112-
// Push the old content over to make room for new content
113-
for _ in 0..string_len {
114-
self.lines[line].insert(0, StyledChar::SPACE);
115-
}
116-
}
111+
// Push the old content over to make room for new content
112+
self.lines[line].splice(0..0, std::iter::repeat(StyledChar::SPACE).take(string_len));
117113

118114
self.puts(line, 0, string, style);
119115
}

0 commit comments

Comments
 (0)