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.
75
Upvotes
6
u/misuo 6d 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.
12
u/TTachyon 6d 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.
22
u/HommeMusical 6d ago
Ouch!
24
u/teerre 6d ago
When you're trying to count file sizes?
24
u/azswcowboy 6d 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.
2
u/UnusualPace679 3d ago edited 3d ago
Sometimes I do want to check if a file exists without doing anything with it. For example, looking for the
.gitfile is a quick (and dirty) way to find the project root.It solves a problem that you don't have (checking if an element exists without accessing the element), but nonetheless a valid problem.
-7
u/sweetno 6d ago
You can avoid these and similar issues by not using the standard library.
-1
u/robstoon 5d ago
This isn't really file system performance traps in the standard library. This is file system performance traps on Windows.
0
u/Jardik2 5d ago
Larger trap is the UB that you cannot avoid if you factor in a user using his computer...