The Cache that Did Nothing

emmtrix Tech Posts: Work & Fun with C++ Standard
Category: Knowledge

When analyzing source files, the file contents should be stored in a cache to minimize access times. The project it was implemented for didn’t have many files, but many accesses to the same files, so a cache should be really beneficial. But after a first implementation, there was no real benefit visible at runtime.

Looking at the source code (see image) showed a simple implementation: if the data is not in the cache, it is loaded and placed in the cache, otherwise it is returned directly. Digging deeper through debugging revealed why it didn’t work as intended: loadFile was reading the text file using UTF8 encoding. However, since the source file contained comments with German umlauts, it threw an exception about wrong encoding. Now comes the wrong part: since the readFile function was already marked as throwing IOExceptions, calling it was already protected with a try-catch, but it caught more generic exceptions and not just IOExceptions. So these errors were never really reported correctly, but caught with only a small warning. The lesson to learn from this: never make error handling too generic, as it might hide unexpected errors, and always expect the input you get to have some exotic content, even if it is just a plain C file.

The Cache that Did Nothing
Cookie Consent with Real Cookie Banner