# fs: allocate backing storage once in Fs::load \(\#9020\) · gitcafe/zed

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

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

Visibility: public

Requested revision: 7b73e2824b9f1f4585f4ac6d7e3518745a648244

Requested commit: 7b73e2824b9f1f4585f4ac6d7e3518745a648244

Commit: 7b73e2824b9f1f4585f4ac6d7e3518745a648244

Tree: 43eaf3588b2e9f0440ef5bd7a2a9f2ac5db99225

Author: Piotr Osiewicz

Committer: GitHub

## Message

```
fs: allocate backing storage once in Fs::load (#9020)

`futures_lite::AsyncReadExt::read_to_string` (that we use in
`RealFs::load`) explicitly does not allocate memory for String contents
up front, which leads to excessive reallocations. That reallocation time
is a significant contributor to the time we spend loading files (esp
large ones). For example, out of ~1s that it takes to open up a 650Mb
ASCII buffer on my machine (after changes related to fingerprinting from
#9007), 350ms is spent in `RealFs::load`.
This change slashes that figure to ~110ms, which is still *a lot*. About
60ms out of 110ms remaining is spent zeroing memory. Sadly,
`AsyncReadExt` API forces us to zero a buffer we're reading into
(whether it's via read_to_string or read_exact), but at the very least
this commit alleviates unnecessary reallocations.

We could probably use something like
[simdutf8](https://docs.rs/simdutf8/latest/simdutf8/) to speed up UTF8
validation in this method as well, though that takes only about ~18ms
out of 110ms, so while it is significant, I've left that out for now.
Memory zeroing is a bigger problem at this point.

Before:

![image](https://github.com/zed-industries/zed/assets/24362066/5e53c004-8a02-47db-bc75-04cb4113a6bc)

After:

![image](https://github.com/zed-industries/zed/assets/24362066/00099032-d647-4683-b290-eaeb969cac4a)

/cc @as-cii 

Release Notes:

- Improved performance when loading large files.
```

## Parents

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

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