From fb62376caa2fdaeb383e1b89ffa5810fb41c4dbd Mon Sep 17 00:00:00 2001 From: Parker Loflin Date: Sun, 15 Nov 2026 09:13:10 +0000 Subject: [PATCH] agent: Read buffer length without copying the text (#66981) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary `get_buffer_content_or_outline` chooses between a file's full text and its outline by size. It measured the size with `buffer.text().len()`, which copies the whole buffer into a `String`. `buffer.len()` reads the same length from the rope's summary. This runs on the main thread when the read file tool reads a file without a line range and when a file is mentioned as context. Both expressions timed on a `text::Buffer` in a release build: | Buffer size | `text().len()` | `len()` | |---|---|---| | 16 KB | 1.8 µs | < 0.1 µs | | 256 KB | 25 µs | < 0.1 µs | | 4 MB | 398 µs | < 0.1 µs | | 64 MB | 22 ms | < 0.1 µs | ## Testing `cargo test -p agent --lib outline` 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/src/outline.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/agent/src/outline.rs b/crates/agent/src/outline.rs index 82592355295..3bb2e6997e8 100644 --- a/crates/agent/src/outline.rs +++ b/crates/agent/src/outline.rs @@ -30,7 +30,7 @@ pub async fn get_buffer_content_or_outline( path: Option<&str>, cx: &AsyncApp, ) -> Result { - let file_size = buffer.read_with(cx, |buffer, _| buffer.text().len()); + let file_size = buffer.read_with(cx, |buffer, _| buffer.len()); if file_size > AUTO_OUTLINE_SIZE { // For large files, use outline instead of full content