From 053d91cb14b5fd138f221ec02bd76efb15e7b2af Mon Sep 17 00:00:00 2001 From: Parker Loflin Date: Mon, 16 Nov 2026 01:11:44 +0000 Subject: [PATCH] agent_ui: Check for an empty message without copying the text (#67020) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary `MessageEditor::is_empty` copied the whole draft with `text(cx)` to check whether it was blank. It now reads characters from the buffer until one isn't whitespace. `str::trim` removes exactly the characters that `char::is_whitespace` matches, so the result is the same. The thread view calls it on every render, through `render_send_button`. The thread view's observer on the message editor also copied the text each time the editor notified, to check for an empty draft. It now uses `Editor::is_empty`, which reads the buffer length. Both `is_empty` checks timed on a `rope::Rope` in a release build, median per call: | Draft size | Before | After | Speedup | |---|---|---|---| | 1 KB | 112 ns | 22 ns | 5x | | 16 KB | 1.1 µs | 24 ns | 46x | | 256 KB | 16.6 µs | 28 ns | 590x | | 4 MB | 271 µs | 33 ns | 8,200x | ## Testing - `cargo test -p agent_ui --lib message_editor` passes. - `cargo test -p agent_ui --lib conversation_view` passes. - `./script/clippy -p agent_ui` passes. ## Self-review - [x] I've reviewed my diff for quality, security, reliability, and performance. - [x] UI changes follow the [checklist](https://zed.dev/docs/development/ui-checklist). - [x] Tests cover the new or changed behavior. Release Notes: - N/A --- crates/agent_ui/src/conversation_view/thread_view.rs | 2 +- crates/agent_ui/src/message_editor.rs | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/agent_ui/src/conversation_view/thread_view.rs b/crates/agent_ui/src/conversation_view/thread_view.rs index dcab1c71efd..824e4e199bd 100644 --- a/crates/agent_ui/src/conversation_view/thread_view.rs +++ b/crates/agent_ui/src/conversation_view/thread_view.rs @@ -1229,7 +1229,7 @@ impl ThreadView { this._draft_resolve_task.take(); return; } - let is_empty = editor.read(cx).text(cx).is_empty(); + let is_empty = editor.read(cx).editor().read(cx).is_empty(cx); let draft_contents_task = if is_empty { None } else { diff --git a/crates/agent_ui/src/message_editor.rs b/crates/agent_ui/src/message_editor.rs index 33c5f8de8a9..ba660d3e402 100644 --- a/crates/agent_ui/src/message_editor.rs +++ b/crates/agent_ui/src/message_editor.rs @@ -759,7 +759,13 @@ impl MessageEditor { } pub fn is_empty(&self, cx: &App) -> bool { - self.editor.read(cx).text(cx).trim().is_empty() + self.editor + .read(cx) + .buffer() + .read(cx) + .read(cx) + .chars_at(MultiBufferOffset(0)) + .all(char::is_whitespace) } pub fn is_completions_menu_visible(&self, cx: &App) -> bool {