Skip to content

Performance issue in git log commands to read last update metadata #11208

Open

Description

@slorber

Description

We need to read git log entries for:

  • Content plugins loadContent() lifecycle, to compute docs/blog/pages last update metadata when showLastUpdateTime or showLastUpdateAuthor is enabled
  • Sitemap postBuild() lifecycle to compute the value of a <lastmod> tag from the route?.metadata?.sourceFilePath (if provided by plugins, which may not be the case for third-party plugins)

As of Docusaurus 3.8, we queue commands like this one for each relevant file

git -c log.showSignature=false log --format=RESULT:%ct,%an --max-count=1 -- "i18n/fr/docusaurus-plugin-content-blog/releases/2.3/index.mdx"
Not exactly, here's a more accurate command:

In practice, we run this:

cd i18n/fr/docusaurus-plugin-content-blog/releases/2.3/
git -c log.showSignature=false log --format=RESULT:%ct,%an --max-count=1 -- "index.mdx"

We do this because it's possible that users have git submodules, and the Git repo at the root may not contain the history of that specific file.


Performance problems

There are various performance bottlenecks in our implementation.

Main case

The git log command queueing works, but it remains a bottleneck, and there's probably room to improve things.

On a large 11k docs site, I noticed that loadContent() takes 24s when docs lastUpdate options are enabled, but only 4s when they are disabled, and these options are the main bottleneck of the loadContent() lifecycle for large sites.

I'm not sure how to improve the performance of this, but we likely have a few options to explore.

Useful resources:

Suggested in:

Suggested again in:

We could also try to optimize the git commands. I think rev-parse and batching history read commands might work but we need to study that.

We could also try to create an incremental "last update" cache, reading the full history first, and then only reading from the newly added commits on rebuilds. This could also help to display the real last update data in dev. But maybe it would be slow to initialize for very large Git repos 🤷‍♂

i18n / codegen / untracked case

I've also noticed that the commands we issue are significantly slower for untracked Git files. This is usually the case:

  • when downloading i18n mdx files from Crowdin
  • when using codegen plugins creating untracked MDX files before running docusaurus build

This can be seen locally on our own website on a localized build after downloading French translation files:

[PERF] Build > en > Load site - 4.53 seconds!
[PERF] Build > en > postBuild() - 3.34 seconds!

[PERF] Build > fr > Load site - 6.15 seconds!
[PERF] Build > fr > postBuild() - 11.53 seconds!

It is unexpected that a French site build has a slower loadContent() and postBuild() compared to the English site.

The impact is even more significant on our Netlify CI, not as powerful as my mac M3 Pro

[PERF] Build > en > Load site - 5.71 seconds!
[PERF] Build > en > postBuild() - 12.78 seconds!

[PERF] Build > fr > Load site - 104.85 seconds!
[PERF] Build > fr > postBuild() - 203.77 seconds!

[PERF] Build > pt-BR > Load site - 191.19 seconds!
[PERF] Build > pt-BR > postBuild() - 298.34 seconds!

[PERF] Build > ko > Load site - 265.14 seconds!
[PERF] Build > ko > postBuild() - 372.63 seconds!

[PERF] Build > zh-CN > Load site - 352.54 seconds!
[PERF] Build > zh-CN > postBuild() - 449.76 seconds!

Note: there's likely a logging issue (or something worse 🤔), numbers look like they accumulate and are not expected to grow like that, but we can already see that loadSite() and postBuild are much slower on localized sites.

It seems that the postBuild() hook takes more time for subsequent locales in practices so I suspect something really weird is happening here, but I can't reproduce it locally yet.

Duplicated work

If a file is not tracked by Git and the history couldn't be read in loadContent(), then we try to read it again in postBuild(), which, as described above, is quite slow.

This should be easy to fix so I'll handle this right now in #11211


Design issue

There are cases where we know for sure there's no last update metadata available. It should be possible to configure the site or plugins to avoid reading the Git history entirely. It's probably possible today through parseFrontMatter and process.env.DOCUSAURUS_CURRENT_LOCALE, but it is clearly not intuitive and we should provide better APIs.

Examples include:

  • i18n sites: if translations are all downloaded from Crowdin, we likely don't want to read the last update metadata from Git for any file in the untracked i18n folder.
  • plugins using code-generation: we have various third-party plugins that generate MDX files that our content plugins will then read. An example is our own Changelog plugin. It should be possible to configure our content plugins so that they directly emit null values instead of running useless git log commands.

In general, we probably want to introduce a global siteConfig.readLastUpdateData callback hook so that it's possible to implement other strategies that are not related to Git.

Here's an example usage based on a pseudo-implementation:

const siteConfig = {
  readLastUpdateData: async (filePath, utils) => {
    if (filePath.startsWith("i18n")) {
      return null;
    }
    if (filePath.startsWith("changelog")) {
      return utils.readFromFS(filePath);
    }
    return utils.readFromGit(filePath);
  }
}

Activity

  1. added
    bug
    status: needs triage
    domain: performance
    and removed
    status: needs triage
    on May 28, 2025
  2. changed the title [-]Performance issues related to `git log` commands to read last update metadata[/-] [+]Performance issue in `git log` commands to read last update metadata[/+] on May 28, 2025
  3. slorber commented on May 28, 2025

    @slorber
    CollaboratorAuthor

    The git log issue seriously affects our CI build times.

    Those are times measured on Netlify on cold builds (no Rspack persistent cache)

    Before any optimization:

    After the duplicate call optimization:

    After the local i18n opt-out optimization:


    Our CI spends approximately 10min to setup things (initialization, building the monorepo, building and downloading Crowdin site translations...)

    So, optimizing these git log calls and removing useless ones can have a very impactful result on Docusaurus build times, depending on the site.

    In our case, docusaurus build went from 34min to 6min 🔥. We now need to figure out how to ship these improvements to the community, in a way that preferably doesn't require complex configuration optimizations.

  4. slorber commented on May 30, 2025

    @slorber
    CollaboratorAuthor

    I spent way too much time trying to figure out why git log calls are slow in our Netlify CI, and keep increasing as we build more locales. I'm not even sure this affects anyone apart us right now so I'm going to stop and move on for now, but please let me know if you are affected as well.

    Here's some notes from various experiments run from this PR: #11218

    To make feedback quicker, I versioned the i18n translations from Crowdin to avoid downloading them (slow).


    Git log benchmarks

    Here's a local git log command benchmark. Options we use are not super relevant, but we can clearly see that it's slower for an untracked file ignored/architecture.mdx.

    Note: I didn't run these commands on Netlify, that could be worth giving it a try if possible.

    hyperfine \
    'git log --max-count=1 -- "website/docs/advanced/architecture.mdx"' \
    'git log --max-count=1 -- "website/i18n/fr/docusaurus-plugin-content-docs/current/advanced/architecture.mdx"' \
    'git log --max-count=1 -- "website/i18n/zh-CN/docusaurus-plugin-content-docs/current/advanced/architecture.mdx"' \
    'git log --max-count=1 -- "website/i18n/deeply/nested/zh-XX/docusaurus-plugin-content-docs/current/advanced/architecture.mdx"' \
    'git log --max-count=1 -- "website/i18n/test/architecture.mdx"' \
    'git log --max-count=1 -- "ignored/architecture.mdx"'
    
    Benchmark 1: git log --max-count=1 -- "website/docs/advanced/architecture.mdx"
      Time (mean ± σ):      12.3 ms ±   3.4 ms    [User: 7.6 ms, System: 3.8 ms]
      Range (min … max):    10.5 ms …  39.7 ms    70 runs
    
     
    Benchmark 2: git log --max-count=1 -- "website/i18n/fr/docusaurus-plugin-content-docs/current/advanced/architecture.mdx"
      Time (mean ± σ):       6.4 ms ±   0.5 ms    [User: 3.5 ms, System: 2.5 ms]
      Range (min … max):     5.4 ms …   8.1 ms    273 runs
     
    Benchmark 3: git log --max-count=1 -- "website/i18n/zh-CN/docusaurus-plugin-content-docs/current/advanced/architecture.mdx"
      Time (mean ± σ):       6.3 ms ±   0.4 ms    [User: 3.5 ms, System: 2.4 ms]
      Range (min … max):     5.4 ms …   8.9 ms    293 runs
     
    Benchmark 4: git log --max-count=1 -- "website/i18n/deeply/nested/zh-XX/docusaurus-plugin-content-docs/current/advanced/architecture.mdx"
      Time (mean ± σ):       6.3 ms ±   0.4 ms    [User: 3.5 ms, System: 2.4 ms]
      Range (min … max):     5.3 ms …   7.5 ms    298 runs
     
    Benchmark 5: git log --max-count=1 -- "website/i18n/test/architecture.mdx"
      Time (mean ± σ):       6.4 ms ±   0.4 ms    [User: 3.5 ms, System: 2.4 ms]
      Range (min … max):     5.2 ms …   7.8 ms    316 runs
     
    Benchmark 6: git log --max-count=1 -- "ignored/architecture.mdx"
      Time (mean ± σ):      25.6 ms ±   2.5 ms    [User: 20.8 ms, System: 3.9 ms]
      Range (min … max):    23.5 ms …  44.4 ms    63 runs
     
    Summary
      git log --max-count=1 -- "website/i18n/deeply/nested/zh-XX/docusaurus-plugin-content-docs/current/advanced/architecture.mdx" ran
        1.01 ± 0.10 times faster than git log --max-count=1 -- "website/i18n/zh-CN/docusaurus-plugin-content-docs/current/advanced/architecture.mdx"
        1.02 ± 0.10 times faster than git log --max-count=1 -- "website/i18n/test/architecture.mdx"
        1.02 ± 0.10 times faster than git log --max-count=1 -- "website/i18n/fr/docusaurus-plugin-content-docs/current/advanced/architecture.mdx"
        1.97 ± 0.57 times faster than git log --max-count=1 -- "website/docs/advanced/architecture.mdx"
        4.09 ± 0.49 times faster than git log --max-count=1 -- "ignored/architecture.mdx"

    The problem doesn't seem related to the extra args we use.


    Tracked vs non-tracked

    Although git log commands are slower for untracked files, the difference should be in the order of less than 100ms, and doesn't explain why our CI would take up to 20 seconds to run a single command.

    Adding i18n to Git didn't change that, so the problem doesn't seem related to tracked vs non-tracked.

    Execa vs child_process, shell vs non-shell

    I tried removing Execa in favor of a simpler child_process integration:

    function execPromise(
      command: string,
      options: ExecOptions,
    ): Promise<{exitCode: number; stdout: string; stderr: string}> {
      // options.shell = '/bin/bash';
    
      return new Promise((resolve, reject) => {
        exec(command, options, (error, stdout, stderr) => {
          if (error) {
            reject(error);
            return;
          }
          resolve({exitCode: 0, stdout, stderr});
        });
      });
    }

    This didn't lead to any significant improvement.

    Note: we use execa with shell: true. It's possible (and even recommended by Execa) to not use a shell (because apparently it's slower and less secure), but from my tests I can clearly see it's faster locally to use the shell option (with both execa/child_process).


    Using git ls-files

    I tried running a git command upfront to check if the file is tracked, but it didn't improve anything

      const command = `(git ls-files --error-unmatch "${file}" || exit 42) && git log ${args} -- "${file}"`;
  5. slorber commented on May 30, 2025

    @slorber
    CollaboratorAuthor

    😢 this also affect GitHub Actions:
    https://github.com/facebook/docusaurus/actions/runs/15351301297/job/43199950972?pr=11218

    I tried running the exact same CI commands locally and didn't encounter this problem.

  6. slorber commented on Sep 8, 2025

    @slorber
    CollaboratorAuthor

    Apparently someone else encountered that problem: #11398

  7. markerikson commented on Sep 10, 2025

    @markerikson
  8. Josh-Cena commented on Sep 10, 2025

    @Josh-Cena
    Collaborator

    I agree, no need to spawn processes here.

  9. slorber commented on Sep 10, 2025

    @slorber
    CollaboratorAuthor

    Yes, those libraries are listed in the issue description already as solutions to consider.

    From what I remember:

    • Nodegit doesn't seem to support Node >= 18, so if it works we need to fix that as well
    • Couldn't get simple-git to work
    • I didn't try isomorphic-git, but I wonder if it will be as fast as native

    Definitively agree that we shouldn't spawn one process per file. Retrieving the full Git history with timestamps/authors is relatively fast even for a repo like ours that has 10000+ commits.

    But we also need to take into account:

    • the modular nature of Docusaurus, which may need to call the getLastUpdate() function in various plugins and plugin steps (we can't read everything upfront globally)
    • the fact that our users have esoteric setups involving git submodules or many independent git repositories
    • the fact that some files may be untracked
  10. fuma-nama commented on Sep 14, 2025

    @fuma-nama

    @slorber not sure if you've found other solutions, I found this is much faster on my machine:

    #!/bin/bash
    # Get the last updated time of files in a directory based on Git history
    git log --name-only --follow --format="COMMIT %h %ci" -- "$1" | awk '
      /^COMMIT/ {commit=$2; date=$3 " " $4 " " $5}
      !/^COMMIT/ && !/^$/ {files[$0]=date}
      END {for (file in files) print file ": " files[file]}
    '

    you may batch reads to optimize the performance

  11. slorber commented on Oct 24, 2025

    @slorber
    CollaboratorAuthor

    FYI, here's a follow-up issue where we'll see if the e18e community could be interested in helping us solve this perf bottleneck efficnetly once for all: e18e/ecosystem-issues#216

  12. Dharya-dev commented on May 25, 2026

    @Dharya-dev
    Contributor

    Been digging into this while working on the VCS code recently — thought I'd share some observations and concrete ideas.

    Where the bottleneck actually lives

    The eager strategy (getGitRepositoryFilesInfo) already does the right thing architecturally — one git log --name-status call vs N individual process spawns. But there are still a few places where perf leaks through:

    1. The stdout buffer problem

    Right now the full git log output gets buffered into a single string (maxBuffer: 20 * 1024 * 1024). For repos with 10k+ commits and lots of files per commit, that string can get massive. Parsing it with split('\n') then allocates another copy of the entire thing as an array. On CI runners with limited memory, this also risks GC pressure spikes.

    A streaming approach using execa's stdout stream + readline would let you process line-by-line without buffering the full output:

    import { createInterface } from 'readline';
    
    const child = execa('git', [...args], { cwd, encoding: 'utf-8' });
    const rl = createInterface({ input: child.stdout! });
    
    for await (const line of rl) {
      // same parsing logic, but line-by-line
      if (line.startsWith('t:')) { /* ... */ }
      // ...
    }
    await child; // ensure process exits cleanly

    This alone should cut peak memory usage roughly in half for large repos.

    2. The untracked file slowdown on CI

    @slorber already noted this — git log for untracked files (i18n downloads, codegen output) is disproportionately slow because Git has to scan the entire history to confirm the file was never committed. On CI environments with shallow clones or limited I/O, this compounds badly.

    The eager strategy sidesteps this by design (untracked files just won't appear in the map), but the ad-hoc fallback still hits it. One low-hanging fix: before calling git log for a file, check if it's tracked with git ls-files --error-unmatch <file>. That's essentially instantaneous and avoids the expensive full-history scan entirely. I know FileNotTrackedError exists but it fires after the slow git log completes.

    3. git last-modified (Git 2.52+)

    This is worth watching closely. The command is designed exactly for this use case — it reads the commit-graph directly instead of walking the full log, which makes it orders of magnitude faster for "what's the latest commit touching this file?" queries. Two caveats though:

    • It's still experimental in Git 2.52 and the CLI surface may change
    • It requires commit-graph to be built (usually automatic in modern Git, but not guaranteed on all CI)

    Could be a good candidate for a future VcsGitLastModified strategy — basically a drop-in for the ad-hoc approach but without the per-file process overhead, since git last-modified accepts multiple paths.

    4. In-process Git (isomorphic-git)

    Spawning processes is inherently expensive — each one costs a fork + exec + shell init. isomorphic-git reads the .git directory directly in JS, eliminating that overhead completely. The tradeoff is it's pure JS so raw git operations are slower than native git, but for the specific "walk commits and map files to timestamps" workload, the savings from not spawning 11k processes would almost certainly dominate.

    Quick sketch of what this could look like:

    import git from 'isomorphic-git';
    import fs from 'fs';
    
    const commits = await git.log({ fs, dir: repoRoot });
    for (const commit of commits) {
      // walk changed files per commit similar to --name-status
    }

    The catch: isomorphic-git doesn't have a direct --name-status equivalent, so you'd need to diff trees between commits. That's more code, but it's a one-time implementation cost.

    What I'd actually recommend doing (in order of effort/impact)

    1. Stream the git log output instead of buffering — easiest win, minimal code change, cuts memory in half
    2. Short-circuit untracked files with git ls-files before falling back to git log in the ad-hoc strategy
    3. Cache the file info map to disk between builds (especially for i18n rebuilds of the same repo) — a simple JSON file alongside the Rspack persistent cache would eliminate redundant git reads entirely
    4. Evaluate git last-modified once it stabilizes — could replace the ad-hoc strategy entirely without needing the full log parse

    The streaming fix (#1) is probably a ~20 line change and would help everyone. Happy to put up a PR for that if there's interest.

  13. slorber commented on Jul 10, 2026

    @slorber
    CollaboratorAuthor

    Note: simple-git recently added the APIs we'd need to implement a new ad-hoc strategy on top of it: https://x.com/Brooooook_lyn/status/2074827514275574153

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdomain: performance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions