# markdown: Optimize \`paint_search_highlights\` \(\#59473\) · gitcafe/zed

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

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

Visibility: public

Requested revision: c7ad65e46817b8bdc71c79ba3f15c898c0bc5678

Requested commit: c7ad65e46817b8bdc71c79ba3f15c898c0bc5678

Commit: c7ad65e46817b8bdc71c79ba3f15c898c0bc5678

Tree: 83eb5dc5f9e5f32f211630e1444ade67e0a4c0a3

Author: Bennet Bo Fenner

Committer: GitHub

## Message

```
markdown: Optimize `paint_search_highlights` (#59473)

Follow up to #52502

This fixes a severe hang in the markdown preview when searching for text
caused by `paint_search_highlights`. We were iterating over all lines
for each highlight, and for each highlight we called
`layout.line_layout_for_index` which internally would iterate over all
the lines of the text layout.

With this optimisation we paint all highlights in one pass. We sort the
highlight ranges, iterate through the lines and only access
`WrappedLineSegments` when we actually have a highlight in that line.

E.g. if you copy this text
(https://gist.github.com/bennetbo/e313f3023da9fec42e090177a5373152) into
the editor, open the markdown preview and search for `codex` Zed Nightly
will completely freeze. After this optimisation it is totally smooth
even in debug mode.



Release Notes:

- Improved performance when searching in markdown preview
```

## Parents

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

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