TLDR - If you are reading files serially in go, and likely other languages too, from a modernish NVMe or Sata SSD and are choosing a buffer size just pick 2-4MB and you’ll be close to optimal. There is such a thing as too big and the drop off is steep.

f, err := os.Open(path)
if err != nil {
	return err
}
defer f.Close()

buf := make([]byte, 2 << 20)
for {
		n, rerr := f.Read(buf)
		//do stuff with n and buf and handle errors
}

The experiment

I was interested to know what buffer size I should be selecting on a reasonably modern desktop computer. I was looking into the impact of extents and file fragmentation on NVMe SSDs (Part 2 on that to come) and found some interesting performance characteristics interacting between buffer and file size.

The hardware the tests were run on:

I tested various sizes of files from 100MB up to 100GB with 3 runs per file taking the average. The files were written before being read and about 20GB is available to Windows 11 for file caching.

This is not actually the best way to get performance out of an individual file read nowadays, this is the wrong way to read a very big file as it is only 1 queue reading and to max out a drive you need to use offsets to parallelise reads. But this approach is how the majority of files are read in practice in a lot of applications.

The results

All the data

It is worth keeping in mind since the machine has 32GB of RAM all the data below that point is cached by the file system. Only the 100GB files are mostly not cached.

First up lets look at the data for PCI-E 4.0

PCI-E 4.0

Buffer 100MB 1GB 16GB 100GB
1K 744.8 735.6 675.2 585.2
4K 2197.1 2095.8 1596.7 1204.3
8K 3678.9 3310.5 2569.3 1184.2
16K 5243.8 4743.4 4019.3 1227.4
32K 7918.5 6630.6 3974.3 1240.2
64K 9665.3 8268.1 7378.1 1438.6
128K 11312.1 9202.7 4859.0 2043.5
256K 11364.9 9561.5 8766.8 2740.1
512K 12020.1 9739.3 9029.7 3539.6
1MB 12963.6 10149.4 9693.7 3986.1
2MB 12829.4 10253.1 9609.9 4393.8
4MB 12852.3 10228.9 10101.1 4283.1
8MB 12885.6 10298.0 10032.5 4234.5
16MB 10217.0 10028.4 9967.1 4087.3
32MB 9175.2 9996.6 10057.0 3475.1
50MB 7991.9 9359.4 9696.2 2884.0

Not surprisingly at file sizes up to 16GB the files are cached. Low buffer sizes do quite poorly in all circumstances and the area of best performance starts around 256k for smaller files and peaks at 2MB. But as we come to the 100GB file we see the drives characteristics more than the Windows cache system. One thing I found interesting is performance drops sharply after a point, and that will be clearer a little bit furhter on. Being a bit too small on the buffer size hurts less than too big and wastes less resources but things have moved on from the 4-64KB days. I wasn’t expecting things to drop past 8MB, I was expecting a plateau.

The PCI-E 3.0 results do not differ much other than a much lower peak for the 100GB results.

PCI-E 3.0

Buffer 100MB 1GB 16GB 100GB
1K 786.3 787.5 641.9 633.3
4K 2300.6 1100.3 1945.9 1156.6
8K 3874.3 2519.1 3174.4 1246.2
16K 5085.2 2714.3 4374.7 999.4
32K 7373.9 4005.2 6166.0 1390.8
64K 9682.9 6519.0 7289.6 1380.5
128K 10626.7 8211.8 8508.4 1859.5
256K 10893.5 8521.9 8355.1 2243.8
512K 10899.9 8714.5 9092.9 2403.1
1MB 11240.1 9142.7 8633.5 2538.0
2MB 11221.6 8967.7 9519.8 2641.7
4MB 12815.6 9050.4 7650.6 2681.9
8MB 9205.0 8582.2 8803.4 2810.6
16MB 10755.5 8631.2 8335.5 2736.9
32MB 6785.7 7036.1 8839.3 2476.8
50MB 8509.9 7020.1 8290.3 2143.1

Measured as MB/s

The peak is flattened and more options work reasonably, 1MB would be fine all the way through 16MB without changing performance much.

The SATA benefits just as much from the caching as the other drives

SATA

Buffer 100MB 1GB 16GB 100GB
1K 624.0 617.1 412.6 389.3
4K 1740.6 1781.6 434.2 436.3
8K 3141.3 2953.3 442.1 435.2
16K 4511.5 3901.4 446.0 445.0
32K 6810.0 5508.6 445.8 430.4
64K 8202.8 7169.2 424.6 438.0
128K 10731.6 7732.7 468.4 465.1
256K 8874.5 8208.4 488.7 484.4
512K 9287.5 7742.8 499.4 499.6
1MB 10171.9 9454.4 448.5 486.2
2MB 11309.0 9482.5 414.6 468.8
4MB 8619.9 8762.8 435.7 436.5
8MB 7613.2 8323.0 450.1 445.3
16MB 9183.2 8427.0 457.3 459.2
32MB 5407.8 8115.5 481.4 485.3
50MB 7445.0 7866.8 484.1 490.0

Measured as MB/s

This also has the widest set of options, 128KB works quite well all the way up to 16MB, it is not as sensitive as the PCI-E drives to buffer size.

100GB mostly uncached

The most interesting picture is what happens in the 100GB file when we expecting the cache has the least impact. This is the picture that differs between the different eras of technology the most.

100GB Buffer Comparison

A PCI-E 3.0 and 4.0 drive look remarkably similar on sequential reads until you use more than 128KB of buffer to read them then we see the faster drive go much faster. The peak for PCI-E 4.0 is 2MB and PCI-E 3.0 is 8MB which is not the way I would expect this worked. This leaves me with more questions than answers really, drive technology and manufacturer has a much bigger impact on the optimal buffer size than I anticipated. The sharp drop of performance past 16MB on both drives suggests that is the absolute highest you want to use. The sweet spot lies in the 2MB to 4MB to 8MB range and you lose little throughput at 2MB.

The SATA picture is curious

100GB SATA

There are all sorts of oddities in this data but mostly the performance is all within 20% past the initial minimum established from 4K. If your target machine is SATA based and squeezing every last MB is the goal it is going to be tricky to get it optimal without testing it. I doubt they all behave the same way. You can get ~85% of the performance here for just 4k of buffer as well.

Conclusions

So what should you choose as the buffer size if you aren’t clear which of these technologies your application will be accessing?

Drive Cached Uncached Safe general pick
PCI-E 4.0 256K-8MB 2-8MB 4MB
PCI-E 3.0 1-16MB 2-8MB 4MB
SATA 128K-16MB flat 1MB (matters little)

AI Declaration

Alas all these words are mine which is why its full of errors. To err is to human.

Locally run AI was used in the running of the tools that did these test as well some of the program around the test code.