r/cpp • • 1d ago

4 filesystem performance traps in the standard library · Mathieu Ropert

https://mropert.github.io/2026/11/24/filesystem_traps/

I've been optimizing startup times on my project lately. Some of it came down to file I/O and some bits of the standard library I consider to be potential traps. Here's a writeup.

54 Upvotes

16 comments sorted by

5

u/misuo 20h ago

Checking for file existence as the first thing is probably also used to avoid special error handling. Say the file operation fails, you will need to know whether it was due to the file not existing (thus perhaps pass through code as it was really not an error), or actually a file operation error that you most likely want to handle (report back, log, whatever).

But I agree that checking on file existence also introduce TOCTOU problems/vulnerabilities.

3

u/mropert 16h ago

As I mentioned in the article, file open system cal already handle file not found with ENOENT or equivalent. If you handle all values from open in one place you will get all error cases including file not found. If you check if it exists before you'll have to handle this case twice.

-1

u/gimpwiz 20h ago

Yep. Tradeoff of code clarity, error clarity, convenience, and speed.

It is very easy to write, read, and report errors for:

if (file not exists) print error file not found;
if (file not readable) print error file permissions;
... check other conditions ...
open file;
if (file not good) print other errors;
do stuff with file;

Now the question from OP basically is, is it worth the performance tradeoff to do checks like this before opening a file? Is this code clear, legible, easy to understand and debug, full of good error reporting, to make tradeoffs even worth considering?

Do you instead do the file open and then check for all the errors? Is it clearer or as clear, etc?

If you run the code once I might suggest that the naive approach is more than adequate.

If you run the code and pay to overhead and then read a huge file, ie, the overhead is proportionally tiny, then I might also suggest it's fine.

If you're reading small files regularly then you pay real penalties.

7

u/UndefinedDefined 17h ago

It makes no sense - when you open a file, literally all of the checks above would happen at kernel level. If you want to open a file, just open a file and process a failure when it happens.

As mentioned above TOCTOU could be a big issue in some cases.

7

u/TTachyon 22h ago

The filesystem impl on Windows assumes that any (const) char* you pass to it is ANSI, which means it goes through the usual ANSI to WTF16 translation, which will result in broken paths on any system that uses anything but ASCII.

The "solution" is to use the char8_t*/std::u8string(_view) types when interacting with it, which basically no codebase uses natively, and people are against the change in general.

5

u/mropert 22h ago

Sadly for filesystem on Windows most paths are better off being wchar/utf16. std::filesystem::path already handles that natively (the underlying char type depends on platform and it's wide chars on Windows).
If you make your own abstractions usually you assume UTF8 and convert to wide char behind the scenes when talking to Windows APIs. This does imply allocations, but usually one temporary malloc for holding the converted path is negligible compared to the a file operation system call.

3

u/TTachyon 21h ago

Going wchar_t for a Windows app might be sensible, but this means you'll have to do conversions on all the other OSes, and waste memory because wchar_t is 4 bytes everywhere else.

For me std::filesystem is just an unusable API given this. At best I might try to make a PAL layer over it to make it reasonable.

The only way to make it work well is if you're targeting Windows 10+ where you can designate your whole process as ANSI=UTF8, but I can't do that myself.

4

u/LB-- Professional+Hobbyist 9h ago

Not anymore. Starting with Windows 10 you can add a manifest entry that turns all the -A APIs into UTF-8 APIs: https://learn.microsoft.com/en-us/windows/apps/design/globalizing/use-utf8-code-page

EDIT: Just saw your other comment about not being able to do that, unfortunate.

20

u/HommeMusical 1d ago

This is not a new thing. There’s a bug report open since 2020. I suspect the underlying issue might be older since MSVC guarantees ABI compatibility with objects built with VS2015. On my i7 10700 with an M.2 SSD, this makes reads 3 times slower for large files (I found it in my texture loader).

Ouch!

16

u/teerre 21h ago

But again as with the previous trap, how likely it is that we will get a file size and then not open it?

When you're trying to count file sizes?

21

u/azswcowboy 1d ago

These aren’t traps, they are things to be aware of in using the library. Removing the ‘exists( file )’ function because some coder might not realize file open is just as fast most of the time ignores the group that actually has this as a valid use case and doesn’t need to open the file. Maybe try this on a network mounted file system and see if there’s a difference? Maybe we should ask rust and python to remove this function as well?

> … Microsoft bug…I guess the only solution is to complain even more at Microsoft.

The library is open source submit a patch.

This is much ado about nothing.

4

u/donalmacc Game Developer 20h ago

From the article though,

Actually, it’s worse than pointless: it’s wrong. Because even if exists() returns true, the subsequent call might still fail because the file was removed in between the calls.

It’s a race condition, and IME it’s almost always wrong.

3

u/inco100 18h ago

Well, what if n opened file got dismounted while working with it? What if one just wants to know if a file is just over there, but does not want to read or modify its content? In the real world, there are countless scenarios, so we got to draw the line somewhere when we are designing.

2

u/azswcowboy 18h ago

Things can be deployed such that a race condition is basically impossible. Just because the author lacks enough imagination to see the valid use cases doesn’t mean they don’t exist.

-7

u/sweetno 1d ago

You can avoid these and similar issues by not using the standard library.

-2

u/robstoon 11h ago

This isn't really file system performance traps in the standard library. This is file system performance traps on Windows.