# Calculate buffer chunks lazily \(\#61523\) · gitcafe/zed

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

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

Visibility: public

Requested revision: 146a6bf9cd0d9d405e88998e86ab0f0357ab8607

Requested commit: 146a6bf9cd0d9d405e88998e86ab0f0357ab8607

Commit: 146a6bf9cd0d9d405e88998e86ab0f0357ab8607

Tree: f8c6edf91a7c972cd2515fe95f4de199626827e3

Author: Kirill Bulatov

Committer: GitHub

## Message

```
Calculate buffer chunks lazily (#61523)

<img width="1728" height="1084" alt="image"
src="https://github.com/user-attachments/assets/a560b10a-6e26-43ff-b830-c528e2dc6798"
/>

Before, each edit in a 50 MB plaintext file would trigger a lot of
anchor calculations (as chunk is 50 lines only) done on main thread +
did extra work when no language grammar or brackets were available.

The PR now moves all anchor calculations into `computed_chunks:
Mutex<HashMap<usize, RowChunk>>,` cache miss.
`TreeSitterData` is wrapped in `Arc` and shared as a part of the
snapshot, hence the need for `Mutex` and internal mutability here.

Trace after the changes:

<img width="1728" height="1084" alt="image"
src="https://github.com/user-attachments/assets/cb258980-8461-4559-b725-aef63c835e60"
/>


Release Notes:

- Improved input performance in large files

---------

Co-authored-by: Finn Evers <finn@zed.dev>
```

## Parents

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

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