Skip to content

Commit

Permalink
Make the benchmark a lot faster
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed Aug 12, 2023
1 parent 79ae481 commit 7f960c7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
3 changes: 2 additions & 1 deletion benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ def print_timings(binary: str, testdata_filename: str):
to_ms = deltas[-THROW_AWAY_AT_EACH_END - 1] * 1000
mid_ms = (from_ms + to_ms) / 2
spread_ms = to_ms - from_ms
print(f"{mid_ms:.1f}ms±{spread_ms:.1f}ms: {binary}")
plusminus_ms = spread_ms / 2
print(f"{mid_ms:.1f}ms±{plusminus_ms:.1f}ms: {binary}")


def time_binaries():
Expand Down
47 changes: 40 additions & 7 deletions src/line_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::sync::{Arc, Mutex};
use std::thread::{self, JoinHandle};

use crate::{constants::*, refiner};
use regex::Regex;
use threadpool::ThreadPool;

const HUNK_HEADER: &str = "\x1b[36m"; // Cyan
Expand All @@ -28,8 +27,6 @@ lazy_static! {
("+++ /dev/null", FAINT),
];

static ref ANSI_COLOR_REGEX: Regex = Regex::new("\x1b\\[[0-9;]*[^0-9;]").unwrap();

/// This is the `\ No newline at end of file` string. But since it can come
/// in not-English as well as English, we take it from the input and store it
/// in this variable. None means we don't know yet.
Expand Down Expand Up @@ -339,12 +336,48 @@ impl LineCollector {
self.consume_plain_line(NORMAL);
}

fn without_ansi_escape_codes(input: &'_ str) -> std::borrow::Cow<'_, str> {
if !input.contains('\x1b') {
return std::borrow::Cow::Borrowed(input);
fn without_ansi_escape_codes(input: &str) -> String {
enum State {
Normal,
Escape,
EscapeBracket,
}

let mut return_me = String::with_capacity(input.len());
let mut state = State::Normal;

for char in input.chars() {
match state {
State::Normal => {
if char == '\x1b' {
state = State::Escape;
} else {
return_me.push(char);
}
}
State::Escape => {
if char == '[' {
state = State::EscapeBracket;
} else {
// Not an ANSI sequence
state = State::Normal;

// Push the characters that we thought were the escape
// sequence's opening
return_me.push('\x1b');
return_me.push(char);
}
}
State::EscapeBracket => {
if !char.is_ascii_digit() && char != ';' {
// Neither digit nor semicolon, this marks the end of the sequence
state = State::Normal;
}
}
}
}

return ANSI_COLOR_REGEX.replace_all(input, "");
return return_me;
}

/// The line parameter is expected *not* to end in a newline
Expand Down

0 comments on commit 7f960c7

Please sign in to comment.