perf: add ghostty-bench and bring terminal throughput to Zig parity
Adds crates/ghostty-bench, a port of ZIG/src/benchmark and src/synthetic: the ghostty-bench binary (terminal-stream, terminal-parser, codepoint-width, grapheme-break, is-symbol, osc-parser, screen-clone with Zig's CLI) and ghostty-synthetic data generators (+ascii, +utf8, +osc, plus a Rust-only +sgr mode). Measured against Ghostty's own ghostty-bench built with Zig 0.15.2 (ReleaseFast) vs --profile production, the hot paths were optimized without behaviour changes: - Terminal::print single-lookup fast path for width-1 chars mid-line - page grapheme allocator: bitmap first-fit instead of a linear scan (was 38% of utf8 stream time; randomized equivalence test added) - plain cells skip per-cell work in clear_cells and row cloning - ASCII fast path + strict hand-written UTF-8 decoder (exhaustive equivalence test against std) - parser/OSC parser split into inlined fast and state-change paths - RenderState allocates grapheme vectors only for grapheme rows - Page::assert_integrity no-op stub so the production profile builds terminal-stream is now at parity (ascii 1.08x, utf8 0.97x, SGR 1.04x of Zig's time). Still slower: screen-clone (~2.6x, zero-filling fresh cell Vecs vs Zig's lazily-zeroed mmap), osc-parser (1.9x), and the raw parser on SGR data (1.5x). Full table in docs/rewrite/notes/perf-P1.md. Test Plan: - cargo test --workspace (no failures) - cargo run --profile production -p ghostty-bench -- +terminal-stream --data=<file> and compare with Zig's ghostty-bench
This commit is contained in:
1 parent
8048ee6a05
commit
0c4f0ee412
18 files changed
+2195
-82
No files matched your search
@@ -301,9 +301,30 @@ impl Parser {
|
||||
/// Next consumes the next character c and returns the actions to execute.
|
||||
/// Up to 3 actions may need to be executed -- in order -- representing
|
||||
/// the state exit, transition, and entry actions.
|
||||
#[inline]
|
||||
pub fn next(&mut self, c: u8) -> [Option<Action>; 3] {
|
||||
let effect = TABLE[c as usize][self.state as usize];
|
||||
|
||||
// Fast path: staying in the same state means there are no exit or
|
||||
// entry actions, only the transition action.
|
||||
if self.state == effect.state {
|
||||
return match effect.action {
|
||||
TransitionAction::Print => [None, Some(Action::Print(u32::from(c))), None],
|
||||
TransitionAction::Execute => [None, Some(Action::Execute(c)), None],
|
||||
action => [None, self.do_action(action, c), None],
|
||||
};
|
||||
}
|
||||
|
||||
self.next_state_change(c, effect)
|
||||
}
|
||||
|
||||
/// The general path of [`Parser::next`] when the state changes.
|
||||
#[inline(never)]
|
||||
fn next_state_change(
|
||||
&mut self,
|
||||
c: u8,
|
||||
effect: crate::parse_table::Transition,
|
||||
) -> [Option<Action>; 3] {
|
||||
let next_state = effect.state;
|
||||
let action = effect.action;
|
||||
|
||||
@@ -313,58 +334,49 @@ impl Parser {
|
||||
// 2. transition action
|
||||
// 3. entry action to new state
|
||||
|
||||
// Exit depends on current state
|
||||
let exit = if self.state == next_state {
|
||||
None
|
||||
} else {
|
||||
match self.state {
|
||||
State::OscString => self.osc_parser.end(Some(c)).map(Action::OscDispatch),
|
||||
State::DcsPassthrough => Some(Action::DcsUnhook),
|
||||
State::SosPmApcString => Some(Action::ApcEnd),
|
||||
_ => None,
|
||||
}
|
||||
};
|
||||
// The actions are written straight into the result array: building
|
||||
// the three `Option<Action>` values separately and then moving them
|
||||
// into the array costs a store-forwarding stall per byte.
|
||||
let mut out: [Option<Action>; 3] = [None, None, None];
|
||||
|
||||
let transition = self.do_action(action, c);
|
||||
// Exit depends on current state (we always change state here).
|
||||
match self.state {
|
||||
State::OscString => out[0] = self.osc_parser.end(Some(c)).map(Action::OscDispatch),
|
||||
State::DcsPassthrough => out[0] = Some(Action::DcsUnhook),
|
||||
State::SosPmApcString => out[0] = Some(Action::ApcEnd),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
out[1] = self.do_action(action, c);
|
||||
|
||||
// Entry depends on new state
|
||||
let entry = if self.state == next_state {
|
||||
None
|
||||
} else {
|
||||
match next_state {
|
||||
State::Escape | State::DcsEntry | State::CsiEntry => {
|
||||
self.clear();
|
||||
None
|
||||
match next_state {
|
||||
State::Escape | State::DcsEntry | State::CsiEntry => self.clear(),
|
||||
State::OscString => self.osc_parser.reset(),
|
||||
State::DcsPassthrough => 'dcs_hook: {
|
||||
// Ignore too many parameters
|
||||
if self.params_idx as usize >= MAX_PARAMS {
|
||||
break 'dcs_hook;
|
||||
}
|
||||
State::OscString => {
|
||||
self.osc_parser.reset();
|
||||
None
|
||||
// Finalize parameters
|
||||
if self.param_acc_idx > 0 {
|
||||
self.params[self.params_idx as usize] = self.param_acc;
|
||||
self.params_idx += 1;
|
||||
}
|
||||
State::DcsPassthrough => 'dcs_hook: {
|
||||
// Ignore too many parameters
|
||||
if self.params_idx as usize >= MAX_PARAMS {
|
||||
break 'dcs_hook None;
|
||||
}
|
||||
// Finalize parameters
|
||||
if self.param_acc_idx > 0 {
|
||||
self.params[self.params_idx as usize] = self.param_acc;
|
||||
self.params_idx += 1;
|
||||
}
|
||||
Some(Action::DcsHook(Dcs::new(
|
||||
&self.intermediates[..self.intermediates_idx as usize],
|
||||
&self.params[..self.params_idx as usize],
|
||||
c,
|
||||
)))
|
||||
}
|
||||
State::SosPmApcString => Some(Action::ApcStart),
|
||||
_ => None,
|
||||
out[2] = Some(Action::DcsHook(Dcs::new(
|
||||
&self.intermediates[..self.intermediates_idx as usize],
|
||||
&self.params[..self.params_idx as usize],
|
||||
c,
|
||||
)));
|
||||
}
|
||||
};
|
||||
State::SosPmApcString => out[2] = Some(Action::ApcStart),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// After generating the actions, we set our next state.
|
||||
self.state = next_state;
|
||||
|
||||
[exit, transition, entry]
|
||||
out
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@@ -378,7 +390,7 @@ impl Parser {
|
||||
self.intermediates_idx += 1;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[inline(always)]
|
||||
fn do_action(&mut self, action: TransitionAction, c: u8) -> Option<Action> {
|
||||
match action {
|
||||
TransitionAction::None | TransitionAction::Ignore => None,
|
||||
@@ -445,8 +457,12 @@ impl Parser {
|
||||
params_sep: self.params_sep,
|
||||
final_: c,
|
||||
};
|
||||
csi.params_buf[..self.params_idx as usize]
|
||||
.copy_from_slice(&self.params[..self.params_idx as usize]);
|
||||
// A fixed-size masked copy (vectorizes) instead of a
|
||||
// variable-length `copy_from_slice`, which is a libc call.
|
||||
let n = usize::from(self.params_idx);
|
||||
for (i, (dst, src)) in csi.params_buf.iter_mut().zip(&self.params).enumerate() {
|
||||
*dst = if i < n { *src } else { 0 };
|
||||
}
|
||||
|
||||
// We only allow colon or mixed separators for the 'm' command.
|
||||
if c != b'm' && self.params_sep.count() > 0 {
|
||||
|
||||
Reference in new issue
Block a user