Repository navigation
Conversation
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>
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 Switching the order of matching the pattern and calling |
|
Ah, my bad, |
|
I have switched the order of checks in I can see a way to optimize the current code by caching the file identity of one of the files in In any case, thank you for the PR. |
|
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 |
|
I think, a PS: Note that |
|
Or maybe it is better to have a |
|
Like all filesystem caches, the Alternatively, there are some interesting proposals in p1883, including |
Yes, that looks interesting. Although the proposal is quite extensive, I'm not sure we need its entirety. |
Oh absolutely - definitely no need to keep the identity objects around beyond the end of |
Problem
file_collector::scan_for_files()compares every regular file in the scanned directory with every file the collector already tracks, usingstd::find_ifwithfilesystem::equivalent(). Eachequivalent()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, callsequivalent()once per tracked file.Fix
This is split into two commits, so the
store_file()change is independent.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 whenfile::collector::scan_for_files()is called directly with a pattern likedir/./app_%N.logor where the directory path includes one or more symlinks but ends up in the same place.Through
text_file_backend, both paths are built asstorage_dir / file_name, so the normal case makes no file system queries for the duplicate check. The per-filestatus(),file_size()andlast_write_time()queries are unchanged, so file system queries go from O(D×T) to O(D).store_file(). Compare file names first, and callequivalent()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 ofequivalent()calls in brackets.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 eachequivalent()call opens both files.The
equivalent()calls were counted by interposingboost::filesystem::detail::equivalent_v3/_v4in 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:equivalent()fallback;max_files, which exercises the duplicate check instore_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/testsuite passes with gcc 14 at C++17. The new test also passes at C++11 withwarnings=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.