terminal: Match trait bounds with terminal input (#31441)

The core change here is the following:

```rust
fn write_to_pty(&self, input: impl Into<Vec<u8>>);

// into
fn write_to_pty(&self, input: impl Into<Cow<'static, [u8]>>);
```

This matches the trait bounds that's used by the Alacritty crate. We are
now allowed to effectively pass `&'static str` instead of always needing
a `String`.

The main benefit comes from making the `to_esc_str` function return a
`Cow<'static, str>` instead of `String`. We save an allocation in the
following instances:

- When the user presses any special key that isn't alphanumerical (in
the terminal)
- When the uses presses any key while a modifier is active (in the
terminal)
- When focusing/un-focusing the terminal
- When completing or undoing a terminal transaction
- When starting a terminal assist

This basically saves us an allocation on **every key** press in the
terminal.

NOTE: This same optimization can be done for **nearly all** keypresses
in the entirety of Zed by changing the signature of the `Keystroke`
struct in gpui. If the Zed team is interested in a PR for it, let me
know.

Release Notes:

- N/A
This commit is contained in:
tidely 2025-06-03 06:12:28 +03:00 committed by GitHub
parent 56d4c0af9f
commit 8ab7d44d51
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 162 additions and 196 deletions

View file

@ -724,12 +724,13 @@ impl Terminal {
// The terminal only supports pasting strings, not images.
Some(text) => format(text),
_ => format(""),
},
}
.into_bytes(),
)
}
AlacTermEvent::PtyWrite(out) => self.write_to_pty(out),
AlacTermEvent::PtyWrite(out) => self.write_to_pty(out.into_bytes()),
AlacTermEvent::TextAreaSizeRequest(format) => {
self.write_to_pty(format(self.last_content.terminal_bounds.into()))
self.write_to_pty(format(self.last_content.terminal_bounds.into()).into_bytes())
}
AlacTermEvent::CursorBlinkingChange => {
let terminal = self.term.lock();
@ -761,7 +762,7 @@ impl Terminal {
// followed by a color request sequence.
let color = self.term.lock().colors()[index]
.unwrap_or_else(|| to_alac_rgb(get_color_at_index(index, cx.theme().as_ref())));
self.write_to_pty(format(color));
self.write_to_pty(format(color).into_bytes());
}
AlacTermEvent::ChildExit(error_code) => {
self.register_task_finished(Some(error_code), cx);
@ -1227,11 +1228,11 @@ impl Terminal {
}
///Write the Input payload to the tty.
fn write_to_pty(&self, input: impl Into<Vec<u8>>) {
fn write_to_pty(&self, input: impl Into<Cow<'static, [u8]>>) {
self.pty_tx.notify(input.into());
}
pub fn input(&mut self, input: impl Into<Vec<u8>>) {
pub fn input(&mut self, input: impl Into<Cow<'static, [u8]>>) {
self.events
.push_back(InternalEvent::Scroll(AlacScroll::Bottom));
self.events.push_back(InternalEvent::SetSelection(None));
@ -1345,7 +1346,10 @@ impl Terminal {
// Keep default terminal behavior
let esc = to_esc_str(keystroke, &self.last_content.mode, alt_is_meta);
if let Some(esc) = esc {
self.input(esc);
match esc {
Cow::Borrowed(string) => self.input(string.as_bytes()),
Cow::Owned(string) => self.input(string.into_bytes()),
};
true
} else {
false
@ -1378,7 +1382,7 @@ impl Terminal {
text.replace("\r\n", "\r").replace('\n', "\r")
};
self.input(paste_text);
self.input(paste_text.into_bytes());
}
pub fn sync(&mut self, window: &mut Window, cx: &mut Context<Self>) {
@ -1487,13 +1491,13 @@ impl Terminal {
pub fn focus_in(&self) {
if self.last_content.mode.contains(TermMode::FOCUS_IN_OUT) {
self.write_to_pty("\x1b[I".to_string());
self.write_to_pty("\x1b[I".as_bytes());
}
}
pub fn focus_out(&mut self) {
if self.last_content.mode.contains(TermMode::FOCUS_IN_OUT) {
self.write_to_pty("\x1b[O".to_string());
self.write_to_pty("\x1b[O".as_bytes());
}
}
@ -1660,7 +1664,7 @@ impl Terminal {
MouseButton::Middle => {
if let Some(item) = _cx.read_from_primary() {
let text = item.text().unwrap_or_default().to_string();
self.input(text);
self.input(text.into_bytes());
}
}
_ => {}
@ -1832,7 +1836,7 @@ impl Terminal {
.map(|name| name.to_string_lossy().to_string())
.unwrap_or_default();
let argv = fpi.argv.clone();
let argv = fpi.argv.as_slice();
let process_name = format!(
"{}{}",
fpi.name,