Skip to content

Commit

Permalink
perf(codegen): use iter::repeat_n in CodeBuffer (#9325)
Browse files Browse the repository at this point in the history
`CodeBuffer::print_indent` use `iter::repeat_n(...)` instead of `iter::repeat(...).take(...)`. `repeat_n` is faster as its iterator has a guaranteed size bound, and so doesn't bounds check repeatedly before pushing each individual byte.
  • Loading branch information
overlookmotel committed Feb 24, 2025
1 parent 186d18a commit 35ee399
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion crates/oxc_codegen/src/code_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::iter;

use assert_unchecked::assert_unchecked;

/// A string builder for constructing source code.
Expand Down Expand Up @@ -392,7 +394,7 @@ impl CodeBuffer {
#[cold]
#[inline(never)]
fn write_slow(code_buffer: &mut CodeBuffer, n: usize) {
code_buffer.buf.extend(std::iter::repeat(b'\t').take(n));
code_buffer.buf.extend(iter::repeat_n(b'\t', n));
}

let len = self.len();
Expand Down

0 comments on commit 35ee399

Please sign in to comment.