# Do not eagerly load entire file contents into memory when decoding it \(\#45971\) · gitcafe/zed

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

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

Visibility: public

Requested revision: cda9eabafc5a0ab37730600d23d18fa8434dcf49

Requested commit: cda9eabafc5a0ab37730600d23d18fa8434dcf49

Commit: cda9eabafc5a0ab37730600d23d18fa8434dcf49

Tree: e5b3a168606300bec0c3d316ca0828ee141d576c

Author: Kirill Bulatov

Committer: GitHub

## Message

````
Do not eagerly load entire file contents into memory when decoding it (#45971)

When working on https://github.com/zed-industries/zed/pull/45969 I've
noticed that whenever I try to open a binary file

```
~/Desktop/svenska ❯ du -ha "Svenska А2B1. 07.09.2025 [u6qEIe9-COc].mkv"
456M	Svenska А2B1. 07.09.2025 [u6qEIe9-COc].mkv
```

Zed allocates all its size

<img width="214" height="104" alt="image"
src="https://github.com/user-attachments/assets/e67ad522-b8a4-4e8e-9961-13030a34df1c"
/>

<img width="345" height="154" alt="image"
src="https://github.com/user-attachments/assets/ea691020-0d02-4acc-88c3-476385f309bb"
/>

only to show me this:

<img width="979" height="677" alt="image"
src="https://github.com/user-attachments/assets/ff91cc9d-eb59-4cee-b06f-5758acd1bd5d"
/>

Given that our existing code checks first 1024 bytes to decide whether
to bail on a binary file or not, this seems very wasteful — hence,
adjusted the code to read the "header" and check that first, and only
continue reading the entire file after the checks are successful.

I suspect this should also help the project search, esp. the crashes and
memory usage during that when many binary files are present?

Release Notes:

- Improved Zed's memory usage when attempting to open binary files
````

## Parents

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

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