зеркало из
https://github.com/glebtv/tui-textarea.git
synced 2026-09-08 16:35:49 +03:00
reduce overhead to build textarea instance using .clone() in benchmarks
Этот коммит содержится в:
@@ -21,13 +21,20 @@ impl Restore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(moves: &[CursorMove], restore: Restore, repeat: usize) -> (usize, usize) {
|
fn prepare_textarea() -> TextArea<'static> {
|
||||||
let mut lines = Vec::with_capacity(LOREM.len() * 2 + 1);
|
let mut lines = Vec::with_capacity(LOREM.len() * 2 + 1);
|
||||||
lines.extend(LOREM.iter().map(|s| s.to_string()));
|
lines.extend(LOREM.iter().map(|s| s.to_string()));
|
||||||
lines.push("".to_string());
|
lines.push("".to_string());
|
||||||
lines.extend(LOREM.iter().map(|s| s.to_string()));
|
lines.extend(LOREM.iter().map(|s| s.to_string()));
|
||||||
|
TextArea::new(lines)
|
||||||
|
}
|
||||||
|
|
||||||
let mut textarea = TextArea::new(lines);
|
fn run(
|
||||||
|
mut textarea: TextArea<'_>,
|
||||||
|
moves: &[CursorMove],
|
||||||
|
restore: Restore,
|
||||||
|
repeat: usize,
|
||||||
|
) -> (usize, usize) {
|
||||||
let mut term = dummy_terminal();
|
let mut term = dummy_terminal();
|
||||||
|
|
||||||
let mut prev = textarea.cursor();
|
let mut prev = textarea.cursor();
|
||||||
@@ -48,39 +55,100 @@ fn run(moves: &[CursorMove], restore: Restore, repeat: usize) -> (usize, usize)
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn move_char(c: &mut Criterion) {
|
fn move_char(c: &mut Criterion) {
|
||||||
|
let textarea = prepare_textarea();
|
||||||
c.bench_function("cursor::char::forward", |b| {
|
c.bench_function("cursor::char::forward", |b| {
|
||||||
b.iter(|| black_box(run(&[CursorMove::Forward], Restore::TopLeft, 1000)))
|
b.iter(|| {
|
||||||
|
black_box(run(
|
||||||
|
textarea.clone(),
|
||||||
|
&[CursorMove::Forward],
|
||||||
|
Restore::TopLeft,
|
||||||
|
1000,
|
||||||
|
))
|
||||||
|
})
|
||||||
});
|
});
|
||||||
c.bench_function("cursor::char::back", |b| {
|
c.bench_function("cursor::char::back", |b| {
|
||||||
b.iter(|| black_box(run(&[CursorMove::Back], Restore::BottomRight, 1000)))
|
b.iter(|| {
|
||||||
|
black_box(run(
|
||||||
|
textarea.clone(),
|
||||||
|
&[CursorMove::Back],
|
||||||
|
Restore::BottomRight,
|
||||||
|
1000,
|
||||||
|
))
|
||||||
|
})
|
||||||
});
|
});
|
||||||
c.bench_function("cursor::char::down", |b| {
|
c.bench_function("cursor::char::down", |b| {
|
||||||
b.iter(|| black_box(run(&[CursorMove::Down], Restore::TopLeft, 1000)))
|
b.iter(|| {
|
||||||
|
black_box(run(
|
||||||
|
textarea.clone(),
|
||||||
|
&[CursorMove::Down],
|
||||||
|
Restore::TopLeft,
|
||||||
|
1000,
|
||||||
|
))
|
||||||
|
})
|
||||||
});
|
});
|
||||||
c.bench_function("cursor::char::up", |b| {
|
c.bench_function("cursor::char::up", |b| {
|
||||||
b.iter(|| black_box(run(&[CursorMove::Up], Restore::BottomLeft, 1000)))
|
b.iter(|| {
|
||||||
|
black_box(run(
|
||||||
|
textarea.clone(),
|
||||||
|
&[CursorMove::Up],
|
||||||
|
Restore::BottomLeft,
|
||||||
|
1000,
|
||||||
|
))
|
||||||
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
fn move_word(c: &mut Criterion) {
|
fn move_word(c: &mut Criterion) {
|
||||||
|
let textarea = prepare_textarea();
|
||||||
c.bench_function("cursor::word::forward", |b| {
|
c.bench_function("cursor::word::forward", |b| {
|
||||||
b.iter(|| black_box(run(&[CursorMove::WordForward], Restore::TopLeft, 1000)))
|
b.iter(|| {
|
||||||
|
black_box(run(
|
||||||
|
textarea.clone(),
|
||||||
|
&[CursorMove::WordForward],
|
||||||
|
Restore::TopLeft,
|
||||||
|
1000,
|
||||||
|
))
|
||||||
|
})
|
||||||
});
|
});
|
||||||
c.bench_function("cursor::word::back", |b| {
|
c.bench_function("cursor::word::back", |b| {
|
||||||
b.iter(|| black_box(run(&[CursorMove::WordBack], Restore::BottomRight, 1000)))
|
b.iter(|| {
|
||||||
|
black_box(run(
|
||||||
|
textarea.clone(),
|
||||||
|
&[CursorMove::WordBack],
|
||||||
|
Restore::BottomRight,
|
||||||
|
1000,
|
||||||
|
))
|
||||||
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
fn move_paragraph(c: &mut Criterion) {
|
fn move_paragraph(c: &mut Criterion) {
|
||||||
|
let textarea = prepare_textarea();
|
||||||
c.bench_function("cursor::paragraph::down", |b| {
|
c.bench_function("cursor::paragraph::down", |b| {
|
||||||
b.iter(|| black_box(run(&[CursorMove::ParagraphForward], Restore::TopLeft, 1000)))
|
b.iter(|| {
|
||||||
|
black_box(run(
|
||||||
|
textarea.clone(),
|
||||||
|
&[CursorMove::ParagraphForward],
|
||||||
|
Restore::TopLeft,
|
||||||
|
1000,
|
||||||
|
))
|
||||||
|
})
|
||||||
});
|
});
|
||||||
c.bench_function("cursor::paragraph::up", |b| {
|
c.bench_function("cursor::paragraph::up", |b| {
|
||||||
b.iter(|| black_box(run(&[CursorMove::ParagraphBack], Restore::BottomLeft, 1000)))
|
b.iter(|| {
|
||||||
|
black_box(run(
|
||||||
|
textarea.clone(),
|
||||||
|
&[CursorMove::ParagraphBack],
|
||||||
|
Restore::BottomLeft,
|
||||||
|
1000,
|
||||||
|
))
|
||||||
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
fn move_edge(c: &mut Criterion) {
|
fn move_edge(c: &mut Criterion) {
|
||||||
|
let textarea = prepare_textarea();
|
||||||
c.bench_function("cursor::edge::head_end", |b| {
|
c.bench_function("cursor::edge::head_end", |b| {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
black_box(run(
|
black_box(run(
|
||||||
|
textarea.clone(),
|
||||||
&[CursorMove::End, CursorMove::Head],
|
&[CursorMove::End, CursorMove::Head],
|
||||||
Restore::None,
|
Restore::None,
|
||||||
500,
|
500,
|
||||||
@@ -90,6 +158,7 @@ fn move_edge(c: &mut Criterion) {
|
|||||||
c.bench_function("cursor::edge::top_bottom", |b| {
|
c.bench_function("cursor::edge::top_bottom", |b| {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
black_box(run(
|
black_box(run(
|
||||||
|
textarea.clone(),
|
||||||
&[CursorMove::Bottom, CursorMove::Top],
|
&[CursorMove::Bottom, CursorMove::Top],
|
||||||
Restore::None,
|
Restore::None,
|
||||||
500,
|
500,
|
||||||
|
|||||||
@@ -2,61 +2,46 @@ use criterion::{criterion_group, criterion_main, Criterion};
|
|||||||
use tui_textarea::TextArea;
|
use tui_textarea::TextArea;
|
||||||
use tui_textarea_bench::{dummy_terminal, TerminalExt, LOREM};
|
use tui_textarea_bench::{dummy_terminal, TerminalExt, LOREM};
|
||||||
|
|
||||||
fn forward(c: &mut Criterion) {
|
#[inline]
|
||||||
#[inline]
|
fn run(pat: &str, mut textarea: TextArea<'_>, forward: bool) {
|
||||||
fn run(pat: &str, lines: Vec<String>) {
|
|
||||||
let mut term = dummy_terminal();
|
let mut term = dummy_terminal();
|
||||||
let mut textarea: TextArea = TextArea::new(lines);
|
|
||||||
textarea.set_search_pattern(pat).unwrap();
|
textarea.set_search_pattern(pat).unwrap();
|
||||||
term.draw_textarea(&textarea);
|
term.draw_textarea(&textarea);
|
||||||
for _ in 0..100 {
|
for _ in 0..100 {
|
||||||
|
if forward {
|
||||||
textarea.search_forward(false);
|
textarea.search_forward(false);
|
||||||
|
} else {
|
||||||
|
textarea.search_back(false);
|
||||||
|
}
|
||||||
term.draw_textarea(&textarea);
|
term.draw_textarea(&textarea);
|
||||||
}
|
}
|
||||||
textarea.set_search_pattern(r"").unwrap();
|
textarea.set_search_pattern(r"").unwrap();
|
||||||
term.draw_textarea(&textarea);
|
term.draw_textarea(&textarea);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn short(c: &mut Criterion) {
|
||||||
|
let textarea = TextArea::from(LOREM.iter().map(|s| s.to_string()));
|
||||||
c.bench_function("search::forward_short", |b| {
|
c.bench_function("search::forward_short", |b| {
|
||||||
let lines: Vec<_> = LOREM.iter().map(|s| s.to_string()).collect();
|
b.iter(|| run(r"\w*i\w*", textarea.clone(), true))
|
||||||
b.iter(move || run(r"\w*i\w*", lines.clone()))
|
|
||||||
});
|
});
|
||||||
c.bench_function("search::forward_long", |b| {
|
c.bench_function("search::back_short", |b| {
|
||||||
|
b.iter(|| run(r"\w*i\w*", textarea.clone(), false))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn long(c: &mut Criterion) {
|
||||||
let mut lines = vec![];
|
let mut lines = vec![];
|
||||||
for _ in 0..10 {
|
for _ in 0..10 {
|
||||||
lines.extend(LOREM.iter().map(|s| s.to_string()));
|
lines.extend(LOREM.iter().map(|s| s.to_string()));
|
||||||
}
|
}
|
||||||
b.iter(move || run(r"[A-Z]\w*", lines.clone()))
|
let textarea = TextArea::new(lines);
|
||||||
});
|
c.bench_function("search::forward_long", |b| {
|
||||||
}
|
b.iter(|| run(r"[A-Z]\w*", textarea.clone(), true))
|
||||||
|
|
||||||
fn back(c: &mut Criterion) {
|
|
||||||
#[inline]
|
|
||||||
fn run(pat: &str, lines: Vec<String>) {
|
|
||||||
let mut term = dummy_terminal();
|
|
||||||
let mut textarea: TextArea = TextArea::new(lines);
|
|
||||||
textarea.set_search_pattern(pat).unwrap();
|
|
||||||
term.draw_textarea(&textarea);
|
|
||||||
for _ in 0..100 {
|
|
||||||
textarea.search_back(false);
|
|
||||||
term.draw_textarea(&textarea);
|
|
||||||
}
|
|
||||||
textarea.set_search_pattern(r"").unwrap();
|
|
||||||
term.draw_textarea(&textarea);
|
|
||||||
}
|
|
||||||
|
|
||||||
c.bench_function("search::back_short", |b| {
|
|
||||||
let lines: Vec<_> = LOREM.iter().map(|s| s.to_string()).collect();
|
|
||||||
b.iter(move || run(r"\w*i\w*", lines.clone()))
|
|
||||||
});
|
});
|
||||||
c.bench_function("search::back_long", |b| {
|
c.bench_function("search::back_long", |b| {
|
||||||
let mut lines = vec![];
|
b.iter(|| run(r"[A-Z]\w*", textarea.clone(), false))
|
||||||
for _ in 0..10 {
|
|
||||||
lines.extend(LOREM.iter().map(|s| s.to_string()));
|
|
||||||
}
|
|
||||||
b.iter(move || run(r"[A-Z]\w*", lines.clone()))
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
criterion_group!(search, forward, back);
|
criterion_group!(search, short, long);
|
||||||
criterion_main!(search);
|
criterion_main!(search);
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user