Repository navigation
Conversation
For every module, `preprocessFile` looked for a file with each known preprocessor suffix in each source directory, and then for a plain Haskell source file, calling `doesFileExist` each time. For packages with many modules this is a very large number of failed file system lookups (haskell#10489). `preprocessComponent` now keeps a cache of directory listings, used to rule out files that certainly do not exist. Which files are found is unchanged: * Names that appear in a listing are still checked with `doesFileExist`. * Names are compared case-insensitively, so case-insensitive file systems behave as before; non-ASCII names bypass the cache. * The cache is cleared whenever a preprocessor has been run, since it may have created files. * The build directory, where preprocessor outputs are looked up to decide whether to re-run a preprocessor, is never looked up through the cache (cf. haskell#11810). Also add a test that hsc2hs is re-run when the .hsc file changes, which was broken by haskell#10534 and fixed by its revert (haskell#11820), and is not covered by any other test. On Windows, preprocessing a library with 5000 modules took 9-27s and now takes 0.3-1.2s.
c9e4111 to
7bab838
Compare
| Left e | ||
| | isDoesNotExistError e -> Just Set.empty | ||
| | otherwise -> Nothing | ||
| modifyIORef' ref (Map.insert dir listing) |
There was a problem hiding this comment.
Let's use atomicModifyIORef' instead please.
There was a problem hiding this comment.
Done, and clearSourceDirCache now uses atomicWriteIORef.
| result <- tryIO (listDirectory dir) | ||
| let listing = case result of | ||
| Right names | ||
| | all (all isAscii) names -> Just (Set.fromList (map foldName names)) |
There was a problem hiding this comment.
I'm a bit concerned that a single non-ASCII filename can poison the entire operation. Given that we are not going to lookup non-ASCII names in the cache (as guarded above), I think it's safe to do
Right names -> Just (Set.fromList (map foldName (filter (all isAscii) names).
There was a problem hiding this comment.
Done in 8c53793. One corner I can think of: a few non-ASCII characters are equivalent to ASCII ones on macOS (KELVIN SIGN normalises to K, and ſ case-folds to s), so a file named with one could still be found under an ASCII name. Happy to keep such a directory uncached if you think it's worth it.
| -- | Cached listings of source directories. The key is the directory path, | ||
| -- the value the case-folded names it contains, or 'Nothing' if it is not | ||
| -- cached (see Note [Source directory listing cache]). | ||
| newtype SourceDirCache = SourceDirCache (IORef (Map.Map FilePath (Maybe (Set.Set String)))) |
There was a problem hiding this comment.
The inner Set.Set String is semantically Set.Set FilePath, right? I'd prefer the latter to make the intention clearer.
Addresses #10489.
For every module,
preprocessFiletries each preprocessor suffix in each source directory before the plain.hsfile, which is about 20 faileddoesFileExistcalls per module. Following @Bodigrim's suggestion on #10534,preprocessComponentnow caches directory listings and uses them only to rule files out (seeNote [Source directory listing cache]):doesFileExist, so the files found are unchanged.The new test
PreProcess/Hsc2HsRerunchecks that hsc2hs is re-run after the.hscchanges. It fails with the #10534 behaviour reintroduced.On a library with 5,000 modules,
preprocessComponentwent from 9.1–12.8 s to 0.28–0.31 s (Windows 11, GHC 9.10.3, runs alternated). Lookups are cheaper on Linux, so the gain there will be smaller.Template Α: This PR modifies behaviour or interface