next.js
e1ee42f7 - Improve caching of next-app-loader to prevent more fs calls. (#67792)

Commit
2 years ago
Improve caching of next-app-loader to prevent more fs calls. (#67792) I noticed in some local profiles that a significant % of the build was being spent in the `metadataResolver` function of `next-app-loader`. It seems to be spending a good chunk of its time inside `fileExistsInDirectory`. It looks like this was already updated to have some level of caching (>99% of calls _do_ hit a cache already), but that a significant number of calls are reaching out to the filesystem but don't have to. For my build in particular, `36685` calls made it to the filesystem, of which `35545 (~97%)` _could_ have been cached. ~~Because the cache here isn't shared between instances, this change would only reduce the cache misses by `~29191 (~79%)`. I think this can be optimized further, but I'm wary of trying to make this change myself.~~ ^ I've updated to get us to the 97% case by stuffing compilation in a WeakMap. Some risk of something holding onto a compilation that shouldn't be as a side effect. I've also added a comment to the code to point this out. Additionally, I spotted that `getFilesInDir` returns an array, but is only ever used in a place that immediately converts it to a set. I've updated to return the set directly, 🤞 that we can save some gc cycles as well. Below are two 60 second profiles. In each, I've disabled webpack parallelism, and added a local cache that writes cache items directly to the filesystem. I've also preloaded the entire cache into memory _before_ the profiles so that I'm looking as closely at the raw CPU work as I can. **Before** <img width="1642" alt="image" src="https://github.com/user-attachments/assets/d27fea21-278f-4dbd-8b33-405fda3c9d08"> **After** ![image](https://github.com/user-attachments/assets/9edf3506-5417-4839-9a26-22f6b49fbb36) <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # --> --------- Co-authored-by: Zack Tanner <1939140+ztanner@users.noreply.github.com> Co-authored-by: JJ Kasper <jj@jjsweb.site>
Author
Parents
Loading