# rescanMac: Avoid overallocating/overreading by millions of bytes · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/bbd13ab961ab49e01d5699eec195929cae7bf25a)

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

Visibility: public

Requested revision: bbd13ab961ab49e01d5699eec195929cae7bf25a

Requested commit: bbd13ab961ab49e01d5699eec195929cae7bf25a

Commit: bbd13ab961ab49e01d5699eec195929cae7bf25a

Tree: 0ca7c34c7e58e61e1be7d1ebbcc058023d0266a7

Author: Ryan Liptak

Committer: Alex Rønne Petersen

## Message

```
rescanMac: Avoid overallocating/overreading by millions of bytes

readAtLeast is greedy and will read the entire length of the buffer if it can. However, reading past the end of the cert in this case is useless, so reading the full length of the buffer just puts an increasingly large (due to the growth algorithm of ArrayList) collection of wasted bytes after each cert in cb.bytes.

In practical terms, this ends up saving potentially millions of bytes of wasted reads/allocations. In my testing, after reading the keychain files on my machine, cb.bytes ends up with these capacities:

- Before: cb.bytes.capacity = 32720747
- After: cb.bytes.capacity = 251937

That's a decrease of 99.2%

Additionally, swaps to readNoEof since it should be an error to hit EOF without reading the full cert size.

```

## Parents

- [253e641ea3ee9f6f1ba14abc5615b861cbc2e347](https://git.cafe/gitcafe/zig/commit/253e641ea3ee9f6f1ba14abc5615b861cbc2e347?format=markdown)

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