Skip to content

Commit

Permalink
reduce number of useless color codes written to stdout (average 21.5%)
Browse files Browse the repository at this point in the history
This reduces output size in our tests as follows:

```
./testdata/syntect/ansi16_colors.out                 923 ->  918 (0.544% smaller)
./testdata/syntect/ansi256_colors.out               1232 -> 1122 (9.803% smaller)
./testdata/syntect/background.out                   1959 -> 1564 (25.25% smaller)
./testdata/syntect/default.out                      1451 -> 1264 (14.79% smaller)
./testdata/syntect/emoji.out                        1495 -> 1289 (15.98% smaller)
./testdata/syntect/emoji_bg.out                     2273 -> 1777 (27.91% smaller)
./testdata/syntect/empty_lines.out                  1705 -> 1518 (12.31% smaller)
./testdata/syntect/empty_lines_bg.out               2904 -> 2269 (27.98% smaller)
./testdata/syntect/first_only.out                   2837 -> 2235 (26.93% smaller)
./testdata/syntect/hard_tab.out                     2088 -> 1597 (30.74% smaller)
./testdata/syntect/japanese_background.out          2237 -> 1741 (28.48% smaller)
./testdata/syntect/japanese_default.out             1612 -> 1388 (16.13% smaller)
./testdata/syntect/long_line.out                    2338 -> 2096 (11.54% smaller)
./testdata/syntect/long_line_bg.out                 3172 -> 2578 (23.04% smaller)
./testdata/syntect/multi_chunks_bg.out              7824 -> 5171 (51.30% smaller)
./testdata/syntect/multi_chunks_default.out         4366 -> 3425 (27.47% smaller)
./testdata/syntect/multi_chunks_no_grid.out         3701 -> 2703 (36.92% smaller)
./testdata/syntect/multi_line_numbers.out           3338 -> 2815 (18.57% smaller)
./testdata/syntect/no_grid.out                       894 ->  688 (29.94% smaller)
./testdata/syntect/no_grid_background.out           1374 ->  960 (43.12% smaller)
./testdata/syntect/no_wrap_background.out           2500 -> 1890 (32.27% smaller)
./testdata/syntect/no_wrap_default.out              1920 -> 1582 (21.36% smaller)
./testdata/syntect/no_wrap_no_grid.out              1357 -> 1000 (35.69% smaller)
./testdata/syntect/region_at_end_of_line.out        1665 -> 1420 (17.25% smaller)
./testdata/syntect/region_at_end_of_line_bg.out     2239 -> 1722 (30.02% smaller)
./testdata/syntect/region_at_line_start.out         1581 -> 1375 (14.98% smaller)
./testdata/syntect/tab_width_2.out                  2098 -> 1607 (30.55% smaller)
./testdata/syntect/theme.out                        1453 -> 1304 (11.42% smaller)
./testdata/syntect/whole_line.out                   1286 -> 1191 (7.976% smaller)
./testdata/syntect/wide_char_region.out             1465 -> 1278 (14.63% smaller)
./testdata/syntect/wide_char_region_bg.out          1964 -> 1569 (25.17% smaller)
./testdata/syntect/wrap_3_lines_emoji.out           2253 -> 2029 (11.03% smaller)
./testdata/syntect/wrap_ansi256.out                 1646 -> 1448 (13.67% smaller)
./testdata/syntect/wrap_between_bg.out              2641 -> 2031 (30.03% smaller)
./testdata/syntect/wrap_between_text.out            2047 -> 1709 (19.77% smaller)
./testdata/syntect/wrap_break_wide_char.out         2252 -> 1876 (20.04% smaller)
./testdata/syntect/wrap_break_wide_char_bg.out      3081 -> 2257 (36.50% smaller)
./testdata/syntect/wrap_emoji.out                   2044 -> 1706 (19.81% smaller)
./testdata/syntect/wrap_emoji_zwj.out               2067 -> 1729 (19.54% smaller)
./testdata/syntect/wrap_japanese_after.out          2536 -> 2065 (22.80% smaller)
./testdata/syntect/wrap_japanese_before.out         2537 -> 2066 (22.79% smaller)
./testdata/syntect/wrap_japanese_louise.out         5738 -> 5532 (3.723% smaller)
./testdata/syntect/wrap_jp_louise_bg.out            6617 -> 6203 (6.674% smaller)
./testdata/syntect/wrap_jp_louise_no_grid.out       4996 -> 4771 (4.715% smaller)
./testdata/syntect/wrap_match_at_second_line.out    2207 -> 1793 (23.08% smaller)
./testdata/syntect/wrap_match_at_second_line_bg.out 2793 -> 2107 (32.55% smaller)
./testdata/syntect/wrap_middle_of_spaces.out        2051 -> 1713 (19.73% smaller)
./testdata/syntect/wrap_middle_of_tab.out           2029 -> 1691 (19.98% smaller)
./testdata/syntect/wrap_middle_of_text.out          2049 -> 1711 (19.75% smaller)
./testdata/syntect/wrap_middle_text_bg.out          2641 -> 2031 (30.03% smaller)
./testdata/syntect/wrap_no_grid.out                 1481 -> 1124 (31.76% smaller)
./testdata/syntect/wrap_region_accross_line.out     2219 -> 1805 (22.93% smaller)
./testdata/syntect/wrap_region_jp_accross_line.out  2225 -> 1811 (22.86% smaller)
./testdata/syntect/wrap_region_line_end.out         1838 -> 1577 (16.55% smaller)
./testdata/syntect/wrap_region_line_start.out       1820 -> 1575 (15.55% smaller)
./testdata/syntect/wrap_theme.out                   2045 -> 1783 (14.69% smaller)
./testdata/syntect/wrap_twice.out                   2656 -> 2186 (21.50% smaller)
./testdata/syntect/wrap_whole_3_lines.out           1763 -> 1650 (6.848% smaller)
```

CPU performance does not change.

Benchmark:

```
group                         after                                  before
-----                         -----                                  ------
syntect::package-lock.json    1.01   722.0±36.85ms        ? ?/sec    1.00   711.9±33.71ms        ? ?/sec
syntect::ripgrep-large        1.01    430.9±8.01ms        ? ?/sec    1.00    428.4±7.48ms        ? ?/sec
syntect::ripgrep-small        1.01    110.5±1.35ms        ? ?/sec    1.00    109.7±1.14ms        ? ?/sec
syntect::ripgrep-tiny         1.00     49.5±0.38ms        ? ?/sec    1.00     49.4±0.30ms        ? ?/sec
```

Command:

```
> hyperfine './before -p syntect Arg ../../clap-rs/clap/src' './after -p syntect Arg ../../clap-rs/clap/src'
Benchmark #1: ./before -p syntect Arg ../../clap-rs/clap/src
  Time (mean ± σ):     292.3 ms ±   3.7 ms    [User: 1.192 s, System: 0.040 s]
  Range (min … max):   285.2 ms … 299.8 ms    10 runs

Benchmark #2: ./after -p syntect Arg ../../clap-rs/clap/src
  Time (mean ± σ):     290.9 ms ±   3.2 ms    [User: 1.180 s, System: 0.041 s]
  Range (min … max):   285.1 ms … 294.6 ms    10 runs

Summary
  './after -p syntect Arg ../../clap-rs/clap/src' ran
    1.00 ± 0.02 times faster than './before -p syntect Arg ../../clap-rs/clap/src'
```

but writing the output to terminal seems improved by around 6~10%
  • Loading branch information
rhysd committed Oct 28, 2021
1 parent 416787e commit 3f95d9a
Show file tree
Hide file tree
Showing 59 changed files with 334 additions and 320 deletions.
18 changes: 16 additions & 2 deletions src/syntect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ struct Canvas<'file, W: Write> {
region_fg_color: Option<Color>,
region_bg_color: Option<Color>,
wrap: bool,
current_fg_color: Option<Color>,
current_bg_color: Option<Color>,
}

impl<'file, W: Write> Deref for Canvas<'file, W> {
Expand Down Expand Up @@ -189,6 +191,8 @@ impl<'file, W: Write> Canvas<'file, W> {

fn draw_newline(&mut self) -> Result<()> {
writeln!(self.out, "\x1b[0m")?; // Reset on newline to ensure to reset color
self.current_fg_color = None;
self.current_bg_color = None;
Ok(())
}

Expand All @@ -213,11 +217,19 @@ impl<'file, W: Write> Canvas<'file, W> {
}

fn set_bg(&mut self, c: Color) -> Result<()> {
self.set_color(40, c)
if self.current_bg_color != Some(c) {
self.set_color(40, c)?;
self.current_bg_color = Some(c);
}
Ok(())
}

fn set_fg(&mut self, c: Color) -> Result<()> {
self.set_color(30, c)
if self.current_fg_color != Some(c) {
self.set_color(30, c)?;
self.current_fg_color = Some(c);
}
Ok(())
}

fn set_default_bg(&mut self) -> Result<()> {
Expand Down Expand Up @@ -584,6 +596,8 @@ impl<'file, W: Write> Drawer<'file, W> {
wrap: opts.text_wrap == TextWrapMode::Char,
region_fg_color,
region_bg_color,
current_fg_color: None,
current_bg_color: None,
match_color: theme.settings.line_highlight.or(theme.settings.background),
out,
};
Expand Down
2 changes: 1 addition & 1 deletion testdata/syntect/ansi16_colors.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
 ./testdata/syntect/ansi16_colors.rs
───┬────────────────────────────────────────────────────────────────────────────
1 │ fn main() {
2 │ println!("*match to this line*"); 
2 │ println!("*match to this line*"); 
3 │ }
───┴────────────────────────────────────────────────────────────────────────────
6 changes: 3 additions & 3 deletions testdata/syntect/ansi256_colors.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
────────────────────────────────────────────────────────────────────────────────
 ./testdata/syntect/ansi256_colors.rs
───┬────────────────────────────────────────────────────────────────────────────
 1 │ fn main() {
 2 │  println!("*match to this line*"); 
 3 │ }
 1 │ fn main() {
 2 │  println!("*match to this line*"); 
 3 │ }
───┴────────────────────────────────────────────────────────────────────────────
6 changes: 3 additions & 3 deletions testdata/syntect/background.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
────────────────────────────────────────────────────────────────────────────────
 ./testdata/syntect/background.rs 
───┬────────────────────────────────────────────────────────────────────────────
 1 │ fn main() { 
 2 │  println!("*match to this line*"); 
 3 │ } 
 1 │ fn main() { 
 2 │  println!("*match to this line*"); 
 3 │ } 
───┴────────────────────────────────────────────────────────────────────────────
6 changes: 3 additions & 3 deletions testdata/syntect/default.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
────────────────────────────────────────────────────────────────────────────────
 ./testdata/syntect/default.rs
───┬────────────────────────────────────────────────────────────────────────────
 1 │ fn main() {
 2 │  println!("*match to this line*"); 
 3 │ }
 1 │ fn main() {
 2 │  println!("*match to this line*"); 
 3 │ }
───┴────────────────────────────────────────────────────────────────────────────
6 changes: 3 additions & 3 deletions testdata/syntect/emoji.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
────────────────────────────────────────────────────────────────────────────────
 ./testdata/syntect/emoji.rs
───┬────────────────────────────────────────────────────────────────────────────
 1 │ fn main() {
 2 │  println!("*match to this line* 🐶🐱 👨‍👩‍👧‍👦"); 
 3 │ }
 1 │ fn main() {
 2 │  println!("*match to this line* 🐶🐱 👨‍👩‍👧‍👦"); 
 3 │ }
───┴────────────────────────────────────────────────────────────────────────────
8 changes: 4 additions & 4 deletions testdata/syntect/emoji_bg.out
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
────────────────────────────────────────────────────────────────────────────────
 ./testdata/syntect/emoji_bg.rs 
───┬────────────────────────────────────────────────────────────────────────────
 1 │ fn main() { 
 2 │  // 🐶🐱 👨‍👩‍👧‍👦 
 3 │  println!("*match to this line* 🐶🐱 👨‍👩‍👧‍👦"); 
 4 │ } 
 1 │ fn main() { 
 2 │  // 🐶🐱 👨‍👩‍👧‍👦 
 3 │  println!("*match to this line* 🐶🐱 👨‍👩‍👧‍👦"); 
 4 │ } 
───┴────────────────────────────────────────────────────────────────────────────
6 changes: 3 additions & 3 deletions testdata/syntect/empty_lines.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
 ./testdata/syntect/empty_lines.rs
───┬────────────────────────────────────────────────────────────────────────────
 1 │ 
 2 │ fn main() {
 2 │ fn main() {
 3 │ 
 4 │  println!("*match to this line*"); 
 4 │  println!("*match to this line*"); 
 5 │ 
 6 │ }
 6 │ }
 7 │ 
 8 │ 
───┴────────────────────────────────────────────────────────────────────────────
16 changes: 8 additions & 8 deletions testdata/syntect/empty_lines_bg.out
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
────────────────────────────────────────────────────────────────────────────────
 ./testdata/syntect/empty_lines_bg.rs 
───┬────────────────────────────────────────────────────────────────────────────
 1 │  
 2 │ fn main() { 
 3 │  
 4 │  println!("*match to this line*"); 
 5 │  
 6 │ } 
 7 │  
 8 │  
 1 │  
 2 │ fn main() { 
 3 │  
 4 │  println!("*match to this line*"); 
 5 │  
 6 │ } 
 7 │  
 8 │  
───┴────────────────────────────────────────────────────────────────────────────
Loading

0 comments on commit 3f95d9a

Please sign in to comment.