Skip to content

Avoid quadratic file system queries in the text file collector scan - #263

Closed
Burgch wants to merge 2 commits into
boostorg:developfrom
Burgch:fix/file-collector-scan-quadratic
Closed

Burgch wants to merge 2 commits into
boostorg:developfrom
Burgch:fix/file-collector-scan-quadratic

Conversation

@Burgch

@Burgch Burgch commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Problem

file_collector::scan_for_files() compares every regular file in the scanned directory with every file the collector already tracks, using std::find_if with filesystem::equivalent(). Each equivalent() call stats both files on POSIX and opens both files on Windows. The comparison is done before the file name is matched against the pattern, so we pay for the stat / open even for files the scan would reject. A scan therefore makes O(D×T) equivalent() calls, where D is the number of files in the directory and T is the number of tracked files.

Collectors are shared per target directory. When more than one sink writes to the same directory, the first sink's scan fills the collector, then the second sink's scan compares every directory entry with every file the first one found. One log file typically builds up per process run, so D and T grow together and sink setup gets quadratically slower over the life of an installation. In an application with two sinks sharing a directory of ~790 log files, creating the second sink took over 60 s on Windows.

store_file() does the same on a smaller scale. When the file being stored is already in the target directory, every rotation, and every sink shutdown with an open file, calls equivalent() once per tracked file.

Fix

This is split into two commits, so the store_file() change is independent.

  1. Scan. Match the pattern first. Then sort the candidates and the tracked files by file name and merge the two lists. A candidate counts as already tracked if a tracked file with the same name has an identical path. equivalent() is called only for same-named files whose paths are spelled differently, for example when file::collector::scan_for_files() is called directly with a pattern like dir/./app_%N.log or where the directory path includes one or more symlinks but ends up in the same place.

    Through text_file_backend, both paths are built as storage_dir / file_name, so the normal case makes no file system queries for the duplicate check. The per-file status(), file_size() and last_write_time() queries are unchanged, so file system queries go from O(D×T) to O(D).

  2. store_file(). Compare file names first, and call equivalent() only for same-named entries whose paths are spelled differently.

Behavior change

A file that refers to an already tracked file under a different name is no longer recognized as already tracked. Examples are a hard link, a symlink, or a different case on a case-insensitive file system. Such a file is now tracked as a separate file.

The old code only caught this against files tracked before the scan started: two such names found in the same scan were already both tracked. Pattern matching is already name-based, so I think this is an acceptable tradeoff with the performance benefits.

Measurements

Linux, xfs, gcc 14, release build, current develop. The directory holds N files matching sink A's pattern and N/8 matching sink B's. Both sinks share one collector. The columns time sink B's first scan, a rescan by sink A, and one rotation by sink A. Each cell gives the time, then the number of equivalent() calls in brackets.

N sink B scan, before after rescan, before after 1 rotation, before after
1000 1697 ms (625,500) 0.9 ms (0) 1692 ms (633,375) 2.1 ms (0) 2.9 ms (1,126) 0.06 ms (1)
2000 7161 ms (2,501,000) 1.8 ms (0) 7040 ms (2,532,375) 4.2 ms (0) 6.6 ms (2,251) 0.11 ms (1)
4000 29597 ms (10,002,000) 3.6 ms (0) 30413 ms (10,127,250) 8.0 ms (0) 16.9 ms (4,501) 0.20 ms (1)

Before the fix, the scan time grows ×4 per doubling of N; after it, ×2. The one call left per rotation is the directory comparison in is_in_target_dir. Windows should gain more, since there each equivalent() call opens both files.

The equivalent() calls were counted by interposing boost::filesystem::detail::equivalent_v3/_v4 in a standalone program that uses only the public API. I can share it if you'd like to see it.

Testing

  • New test test/run/sink_text_file_collector_scan.cpp. It covers:

    • repeated scans;
    • two sinks sharing a collector;
    • the file counter returned by a scan;
    • scanning a directory spelled differently, which exercises the equivalent() fallback;
    • rotating a file found by a scan, with max_files, which exercises the duplicate check in store_file().

    The test passes both before and after the change. I checked that it fails when either duplicate check is disabled, and when the equivalent() fallback is removed.

  • The full libs/log/test suite passes with gcc 14 at C++17. The new test also passes at C++11 with warnings=extra, with no warnings in Boost.Log sources or tests.

  • Not yet tested on Windows.

🤖 Co-authored with Claude Code, but fully reviewed by me.

Burgch and others added 2 commits November 27, 2026 11:19
scan_for_files() called filesystem::equivalent() for every pair of found and
tracked files. Match file names against the pattern first, then match found
and tracked files by name in a sorted merge, calling filesystem::equivalent()
only for same-named files with differently spelled paths.

A file that refers to a tracked file under a different name, e.g. a hard link,
is no longer recognized as tracked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
store_file() now compares file names before calling filesystem::equivalent().
As with the scan, the same file under a different name is no longer recognized.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Lastique

Lastique commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

A file that refers to an already tracked file under a different name is no longer recognized as already tracked. Examples are a hard link, a symlink, or a different case on a case-insensitive file system. Such a file is now tracked as a separate file.

Hardlinks and symlinks are separate files (read: inodes), they are not and should not be treated as equivalent. Case-insensitive filesystems are a different story, you cannot have two files differing only in case. It is therefore possible for different file names to refer to the same file. It is also possible for the filesystem to not preserve the filename case when the file is created, so the filenames we get when iterating the directory may be different from what we saved in the internal file list.

The only definitive way to tell if two filenames refer to the same file is filesystem::equivalent. You could possibly assume that two paths refer to the same file if both paths are canonical, absolute, and are equal, but the reverse is not necessarily true. And canonicalizing paths is arguably more expensive than filesystem::equivalent anyway. So I don't think your approach relying on file names is correct.

Switching the order of matching the pattern and calling filesystem::equivalent is a valid optimization, but it only applies to the scan_matching case.

@Lastique

Copy link
Copy Markdown
Member

Ah, my bad, filesystem::equivalent returns true for symlinks and hardlinks. Still, the point about case-insensitive filesystems stands, which makes filename comparison incorrect.

@Lastique

Copy link
Copy Markdown
Member

I have switched the order of checks in scan_for_files and merged your test, but I'm not going to merge the other changes that rely on comparing filenames.

I can see a way to optimize the current code by caching the file identity of one of the files in scan_for_files and using that to search for duplicates instead of calling filesystem::equivalent. But file identities are platform-specific, which makes the implementation more complicated than I'd like for the 1.93 release. Maybe I'll implement it in the future. Or you could have a go at it in a separate PR, if you'd like.

In any case, thank you for the PR.

@Lastique Lastique closed this Nov 28, 2026
@Burgch

Burgch commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! I was trying to avoid bringing in a platform-specific file identity, but I think that may be necessary to handle case insensitive filesystems fully. What do you think about adding a platform-independent abstraction like filesystem_equivalence_identity (naming?) which could be sortable (and hence equality-comparable). It could be a nice addition to propose to filesystem with support for passing it as either or both arguments to filesystem::equivalent, and this could then migrate to using that.

@Lastique

Copy link
Copy Markdown
Member

I think, a file_identity type would be a nice addition to Boost.Filesystem. It could provide operator== that would return true if two identities refer to the same file. After that, Boost.Log could switch to that new API to optimize its scan_for_files implementation.

PS: Note that file_identity would have to keep the file open to keep the inode from being reused. This also means there can be no identity if a non-existing file, which is unlike a filesystem path. Boost.Log implementation would have to account for that.

@Lastique

Copy link
Copy Markdown
Member

Or maybe it is better to have a file_handle class, and use that as arguments for filesystem::equivalent. I'll have to think about it.

@Burgch

Burgch commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

Like all filesystem caches, the scan_for_files result is inherently outdated as soon as it returns, so I wonder if something like cached_file_identity might make sense, to make it explicit that it isn’t trying to be correct in the presence of concurrent filesystem modifications?

Alternatively, there are some interesting proposals in p1883, including path_handle.

@Lastique

Copy link
Copy Markdown
Member

Like all filesystem caches, the scan_for_files result is inherently outdated as soon as it returns, so I wonder if something like cached_file_identity might make sense, to make it explicit that it isn’t trying to be correct in the presence of concurrent filesystem modifications?

file_identity, as I see it, should be stable, meaning, external modifications to the filesystem should not invalidate it. However, the internal file list in file_collector should not need to store file_identity all the time. Rather, file_identity objects can be constructed for the purpose of optimizing scan_for_files and disposed of afterwards. Storing them permanently would be too resource intensive and unnecessary for the normal sink and collector operation. scan_for_files is not supposed to be called often.

Alternatively, there are some interesting proposals in p1883, including path_handle.

Yes, that looks interesting. Although the proposal is quite extensive, I'm not sure we need its entirety.

@Burgch

Burgch commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

file_identity, as I see it, should be stable, meaning, external modifications to the filesystem should not invalidate it. However, the internal file list in file_collector should not need to store file_identity all the time. Rather, file_identity objects can be constructed for the purpose of optimizing scan_for_files and disposed of afterwards. Storing them permanently would be too resource intensive and unnecessary for the normal sink and collector operation. scan_for_files is not supposed to be called often.

Oh absolutely - definitely no need to keep the identity objects around beyond the end of scan_for_files. The point I was trying to make is that the implementation already makes the (sensible) tradeoff of caching the result of scan_for_files and then operating log file naming & rotation on the basis of that cache, so does it really need to worry about the inode changing / gettting reused during the course of capturing the cache? That just feels like unnecessary complexity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants