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
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.
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.
20
u/HommeMusical 1d ago
Ouch!
16
u/teerre 21h ago
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.
-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.