Why a Second File Read Can Be Faster

An operating system can cache recently accessed file contents in memory, making demand on the underlying storage depend on earlier reads.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Compare two observations
  2. Measure the intended situation
  3. Isolate the storage work an operating system may reuse
  4. Distinguish shared cache from data stored by the process

Compare two observations

The first read of a large data file might wait for the storage device. The next could reuse cached pages. Both calls can return the same bytes with different timing. The page cache explains this intermediate layer.

Measure the intended situation

A repeated benchmark often reflects warm cache behavior. For a useful comparison, record which state the workload represents and keep the operation itself the same. See empirical scaling.[1]

Isolate the storage work an operating system may reuse

A first pass through a file can populate the filesystem page cache. A second pass can serve many pages from memory and finish faster. The application may read the full file in both runs, so work at the API boundary does not reveal device activity alone. See cold and warm build caches.

Run A: process reads bytes, device may supply pages
Run B: process reads bytes again, pages may already be cached
Note which caches the experiment controls

Distinguish shared cache from data stored by the process

Restarting the reader may leave the system cache warm. Timing can also change with CPU warm-up, drive caches and background activity. Report the measurement conditions and run sizes.

References

  1. Remzi H. Arpaci-Dusseau and Andrea C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces ↩