Skip to content

Cache source directory listings when preprocessing - #12606

Open
Lotion-3 wants to merge 4 commits into
haskell:masterfrom
Lotion-3:perf/preprocess-dir-cache
Open

Lotion-3 wants to merge 4 commits into
haskell:masterfrom
Lotion-3:perf/preprocess-dir-cache

Conversation

@Lotion-3

Copy link
Copy Markdown
Contributor

Addresses #10489.

For every module, preprocessFile tries each preprocessor suffix in each source directory before the plain .hs file, which is about 20 failed doesFileExist calls per module. Following @Bodigrim's suggestion on #10534, preprocessComponent now caches directory listings and uses them only to rule files out (see Note [Source directory listing cache]):

The new test PreProcess/Hsc2HsRerun checks that hsc2hs is re-run after the .hsc changes. It fails with the #10534 behaviour reintroduced.

On a library with 5,000 modules, preprocessComponent went 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

  • Patches conform to the coding conventions.
  • Any changes that could be relevant to users have been recorded in the changelog.
    • Is the change significant? Left to reviewers.
  • The documentation has been updated, if necessary. (The design is in a Note.)
  • Manual QA notes have been included. (No change to cabal-install's behaviour.)
  • Tests have been added.

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.
@Lotion-3
Lotion-3 force-pushed the perf/preprocess-dir-cache branch from c9e4111 to 7bab838 Compare November 17, 2026 17:33
Left e
| isDoesNotExistError e -> Just Set.empty
| otherwise -> Nothing
modifyIORef' ref (Map.insert dir listing)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use atomicModifyIORef' instead please.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inner Set.Set String is semantically Set.Set FilePath, right? I'd prefer the latter to make the intention clearer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, changed in a77d04d.

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