Posted on

Table of Contents

I’m writing a Rust command line app in my spare time to learn the language. It involves some file system checks where I use fs::metadata. As everyone knows, accessing the disk is an expensive operation and must be kept to a minimum. I was thinking of using a HashMap::<Path, Metadata> to cache the results for paths.

I then came across the cached crate. It caches the results of functions for memoization. This is exactly what I need, I thought. Internally, it does what I was planning to do.

Later, I noticed the possible bugs that could arise. I’m thinking of using the function in a short-running process, so the metadata is not expected to change during the run. For some reason, suppose the runtime of the process began to get longer, or I decided to add a web server on top of it. At that time, probably many moons from now, I’ll have forgotten the decision I made about caches and my assumption that the metadata won’t change during the run. It will cause some weird bugs when file timestamp changes aren’t detected.

No one will notice that I’m fixing bugs if they never appear, but I believe this is the best kind of software engineering.

Commentary (2022-08-01)

  • It looks like my assumption that RAM is significantly faster than disk access may also be wrong. SSDs are fast, and for parallel access, they may perform as fast as RAM.