Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: implement NODE_COMPILE_CACHE for automatic on-disk code caching #52535

Closed
wants to merge 3 commits into from

Commits on Apr 14, 2024

  1. deps: V8: cherry-pick cd10ad7cdbe5

    Original commit message:
    
        [compiler] reset script details in functions deserialized from code cache
    
        During the serialization of the code cache, V8 would wipe out the
        host-defined options, so after a script id deserialized from the
        code cache, the host-defined options need to be reset on the script
        using what's provided by the embedder when doing the deserializing
        compilation, otherwise the HostImportModuleDynamically callbacks
        can't get the data it needs to implement dynamic import().
    
        Change-Id: I33cc6a5e43b6469d3527242e083f7ae6d8ed0c6a
        Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/5401780
        Reviewed-by: Leszek Swirski <[email protected]>
        Commit-Queue: Joyee Cheung <[email protected]>
        Cr-Commit-Position: refs/heads/main@{#93323}
    
    Refs: v8/v8@cd10ad7
    joyeecheung committed Apr 14, 2024
    Configuration menu
    Copy the full SHA
    a65d01d View commit details
    Browse the repository at this point in the history

Commits on Apr 15, 2024

  1. module: implement NODE_COMPILE_CACHE for automatic on-disk code caching

    This patch implements automatic on-disk code caching that can be enabled
    via an environment variable NODE_COMPILE_CACHE.
    
    When set, whenever Node.js compiles a CommonJS or a ECMAScript Module,
    it will use on-disk [V8 code cache][] persisted in the specified directory
    to speed up the compilation. This may slow down the first load of a
    module graph, but subsequent loads of the same module graph may get
    a significant speedup if the contents of the modules do not change.
    Locally, this speeds up loading of test/fixtures/snapshot/typescript.js
    from ~130ms to ~80ms.
    
    To clean up the generated code cache, simply remove the directory.
    It will be recreated the next time the same directory is used for
    `NODE_COMPILE_CACHE`.
    
    Compilation cache generated by one version of Node.js may not be used
    by a different version of Node.js. Cache generated by different versions
    of Node.js will be stored separately if the same directory is used
    to persist the cache, so they can co-exist.
    
    Caveat: currently when using this with V8 JavaScript code coverage, the
    coverage being collected by V8 may be less precise in functions that are
    deserialized from the code cache. It's recommended to turn this off when
    running tests to generate precise coverage.
    
    Implementation details:
    
    There is one cache file per module on disk. The directory layout
    is:
    
    - Compile cache directory (from NODE_COMPILE_CACHE)
      - 8b23c8fe: CRC32 hash of CachedDataVersionTag + NODE_VERESION
      - 2ea3424d:
         - 10860e5a: CRC32 hash of filename + module type
         - 431e9adc: ...
         - ...
    
    Inside the cache file, there is a header followed by the actual
    cache content:
    
    ```
    [uint32_t] code size
    [uint32_t] code hash
    [uint32_t] cache size
    [uint32_t] cache hash
    ... compile cache content ...
    ```
    
    When reading the cache file, we'll also check if the code size
    and code hash match the code that the module loader is loading
    and whether the cache size and cache hash match the file content
    read. If they don't match, or if V8 rejects the cache passed,
    we'll ignore the mismatch cache, and regenerate the cache after
    compilation succeeds and rewrite it to disk.
    joyeecheung committed Apr 15, 2024
    Configuration menu
    Copy the full SHA
    f7806b3 View commit details
    Browse the repository at this point in the history

Commits on Apr 17, 2024

  1. Configuration menu
    Copy the full SHA
    b45062b View commit details
    Browse the repository at this point in the history