agent: Read buffer length without copying the text (#66981)

## 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
This commit is contained in:
Parker Loflin
2026-11-15 09:13:10 +00:00
committed by GitHub
parent e2f00a41ac
commit fb62376caa
+1 -1
View File
@@ -30,7 +30,7 @@ pub async fn get_buffer_content_or_outline(
path: Option<&str>,
cx: &AsyncApp,
) -> Result<BufferContent> {
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