Skip to content

Commit

Permalink
Use handwritten ansi parser (#109)
Browse files Browse the repository at this point in the history
This removes the heavy `regex` dependency from the ANSI feature.

Co-authored-by: Armin Ronacher <armin.ronacher@active-4.com>
  • Loading branch information
CosmicHorrorDev and mitsuhiko committed Jan 23, 2022
1 parent a66421d commit 8a4d205
Show file tree
Hide file tree
Showing 5 changed files with 496 additions and 73 deletions.
40 changes: 34 additions & 6 deletions .github/workflows/ci.yml
Expand Up @@ -5,6 +5,34 @@ on:
pull_request:
branches: [master]
jobs:
build-msrv:
name: Build on MSRV (1.40)
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
rust: 1.40.0
- os: windows-latest
target: i686-pc-windows-msvc
rust: 1.40.0
runs-on: ${{ matrix.os }}
steps:
- name: Install rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}
override: true
- name: Checkout
uses: actions/checkout@v2
- name: Build
uses: actions-rs/cargo@v1
with:
command: check
args: --target ${{ matrix.target }}
test:
name: Tests
strategy:
Expand All @@ -13,19 +41,19 @@ jobs:
include:
- os: macos-latest
target: x86_64-apple-darwin
rust: 1.40.0
rust: 1.51.0
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
rust: 1.40.0
rust: 1.51.0
- os: ubuntu-latest
target: i686-unknown-linux-gnu
rust: 1.40.0
rust: 1.51.0
- os: windows-latest
target: i686-pc-windows-msvc
rust: 1.40.0
rust: 1.51.0
- os: windows-latest
target: x86_64-pc-windows-msvc
rust: 1.40.0
rust: 1.51.0
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
rust: stable
Expand Down Expand Up @@ -70,7 +98,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.40.0
toolchain: 1.51.0
target: ${{ matrix.target }}
override: true
- name: Checkout
Expand Down
13 changes: 11 additions & 2 deletions Cargo.toml
Expand Up @@ -13,8 +13,8 @@ readme = "README.md"

[features]
default = ["unicode-width", "ansi-parsing"]
windows-console-colors = ["ansi-parsing", "winapi-util"]
ansi-parsing = ["regex"]
windows-console-colors = ["ansi-parsing", "regex", "winapi-util"]
ansi-parsing = []

[dependencies]
once_cell = "1"
Expand All @@ -27,3 +27,12 @@ unicode-width = { version = "0.1", optional = true }
winapi = { version = "0.3", features = ["winbase", "winuser", "consoleapi", "processenv", "wincon"] }
winapi-util = { version = "0.1.3", optional = true }
encode_unicode = "0.3"

[dev-dependencies]
criterion = "0.3.5"
proptest = "1.0.0"
regex = "1.4.2"

[[bench]]
name = "ansi_parser"
harness = false
27 changes: 27 additions & 0 deletions benches/ansi_parser.rs
@@ -0,0 +1,27 @@
use console::{strip_ansi_codes, AnsiCodeIterator};
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};

use std::{fs, path::Path};

pub fn parse_throughput(c: &mut Criterion) {
let session_log_path = Path::new("tests")
.join("data")
.join("sample_zellij_session.log");
let session_log = fs::read_to_string(session_log_path).unwrap();

let mut group = c.benchmark_group("ansi-parsing");
group.throughput(Throughput::Bytes(session_log.len() as u64));
group.bench_function("parse", |b| {
b.iter(|| {
let v: Vec<_> = AnsiCodeIterator::new(&session_log).collect();
black_box(v);
})
});
group.bench_function("strip", |b| {
b.iter(|| black_box(strip_ansi_codes(&session_log)))
});
group.finish();
}

criterion_group!(throughput, parse_throughput);
criterion_main!(throughput);

0 comments on commit 8a4d205

Please sign in to comment.