Skip to content

Add cache for some FS operations while compileΒ #825

Closed
@timocov

Description

@timocov

Hi there.

TypeScript makes a lot of identical FS operations such as:

  • checking that some file/directory exists (e.g. for the identification an extension of a file it can make up to 3 fileExists calls)
  • resolving original file path

To solve an issue described in microsoft/TypeScript#22576 in our project we have developed a small tool, which compiles all TypeScript files one-by-one as entry point and checks errors.
Now the project has grown to 1000+ TypeScript files and the test takes more than 20-30 minutes (sometimes it even fails because of timeout after 1 hour).
After investigating the problem we found out that the reason is a lot of FS operations made by TypeScript compiler.

(I believe that we can assume that compilation process is "atomic" operation and we can just add cache for some "heavy" syscall-operations like fileExists)

After adding cache for fileExists, realpath, directoryExists and getSourceFile (getSourceFile is specific for our tool) we get 20-30x decreasing of working time (to 1 minute).

I hope that this approach can be used in ts-loader too to speedup compilation time.

I roughly added some caches to fileExists, realpath and directoryExists functions and here are miss/hit values (for our project) in cold-compilation:

  • fileExists: miss=55847, hit=630450
  • directoryExists: miss=51, hit=1914
  • realpath: miss=39, hit=466

The compilation time is decreased from 850s to 330s.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions