agent_ui: Check for an empty message without copying the text (#67020)

## 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
This commit is contained in:
Parker Loflin
2026-11-16 01:11:44 +00:00
committed by GitHub
parent 125ca8672e
commit 053d91cb14
2 changed files with 8 additions and 2 deletions
@@ -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 {
+7 -1
View File
@@ -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 {