# Optimize editor rendering when clipped by parent containers \(\#44995\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/914b0117fb5a23469af85e567d5723eca6b53635)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: 914b0117fb5a23469af85e567d5723eca6b53635

Requested commit: 914b0117fb5a23469af85e567d5723eca6b53635

Commit: 914b0117fb5a23469af85e567d5723eca6b53635

Tree: 60f5a8600b12e1018f2703c3d25b67e52ce978d1

Author: Antonio Scandurra

Committer: GitHub

## Message

```
Optimize editor rendering when clipped by parent containers (#44995)

Fixes #44997

## Summary

Optimizes editor rendering when an editor is partially clipped by a
parent container (e.g., a `List`). The editor now only lays out and
renders lines that are actually visible within the viewport, rather than
all lines in the document.

## Problem

When an `AutoHeight` editor with thousands of lines is placed inside a
scrollable `List` (such as in the Agent Panel thread view), the editor
would lay out **all** lines during prepaint, even though only a small
portion was visible. Profiling showed that ~50% of frame time was spent
in `EditorElement::prepaint` → `LineWithInvisibles::from_chunks`,
processing thousands of invisible lines.

## Solution

Calculate the intersection of the editor's bounds with the current
content mask (which represents the visible viewport after all parent
clipping). Use this to determine:
1. `clipped_top_in_lines` - how many lines are clipped above the
viewport
2. `visible_height_in_lines` - how many lines are actually visible

Then adjust `start_row` and `end_row` to only include visible lines. The
parent container handles positioning, so `scroll_position` remains
unchanged for paint calculations.

## Example

For a 3000-line editor where only 50 lines are visible:
- **Before**: Lay out and render 3000 lines
- **After**: Lay out and render ~50 lines

## Testing

Verified the following scenarios work correctly:
- Editor fully visible (no clipping)
- Editor clipped from top
- Editor clipped from bottom
- Editor completely outside viewport (renders nothing)
- Fractional line clipping at boundaries
- Scrollable editors with internal scroll state inside a clipped
container

Release Notes:

- Improved agent panel performance when rendering large diffs.
```

## Parents

- [005a85e57b14aed31e7a55dca9efe2b28f158d36](https://git.cafe/gitcafe/zed/commit/005a85e57b14aed31e7a55dca9efe2b28f158d36?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/914b0117fb5a23469af85e567d5723eca6b53635?format=markdown)
