Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consolidate Server and Routing process into one process (#53523)
In the current version of Next.js there are 4 processes when running in production: - Server - Routing - Rendering Pages Router - Rendering App Router This setup was introduced in order to allow App Router and Pages Router to use different versions of React (i.e. Server Actions currently requires react@experimental to function). I wrote down more on why these processes exist in this comment: #49929 (comment) This PR combines the Server and Routing process into one handler, as the "Server" process was only proxying to the Routing process. In my testing this caused about ~2x the amount of memory per request as the response body was duplicated between the processes. This was especially visible in the case of that memory leak in Node.js 18.16 as it grew memory usage on both sides quickly. In the process of going through these changes I found a couple of other bugs like the propagation of values to the worker processes not being awaited ([link](https://github.com/vercel/next.js/pull/53523/files#diff-0ef09f360141930bb03263b378d37d71ad9432ac851679aeabc577923536df84R54)) and the dot syntax for propagation was not functioning. It also seemed there were a few cases where watchpack was used that would cause many more files to be watched than expected, for now I've removed those cases, specifically the "delete dir while running" and instrument.js hmr (instrument.js is experimental). Those tests have been skipped for now until we can revisit them to verfiy it I've also cleaned up the types a bit while I was looking into these changes. ### Improvement⚠️ Important preface to this, measuring memory usage / peak usage is not super reliable especially when JS gets garbage collected. These numbers are just to show the rough percentage of less memory usage. #### Baseline Old: ``` next-server: 44.8MB next-router-worker: 57.5MB next-render-worker-app: 39,6MB next-render-worker-pages: 39,1MB ``` New: ``` next-server: Removed next-router-worker: 64.4MB next-render-worker-app: 43.1MB (Note: no changes here, this shows what I meant by rough numbers) next-render-worker-pages: 42.4MB (Note: no changes here, this shows what I meant by rough numbers) ``` Overall win: ~40MB (process is removed) #### Peak usage Old: ``` next-server: 118.6MB next-router-worker: 223.7MB next-render-worker-app: 32.8MB (I ran the test on a pages application in this case) next-render-worker-pages: 101.1MB ``` New: ``` next-server: Removed next-router-worker: 179.1MB next-render-worker-app: 33.4MB next-render-worker-pages: 117.5MB ``` Overall win: ~100MB (but it scales with requests so it was about ~50% of next-router-worker constantly) Related: #53523 --------- Co-authored-by: JJ Kasper <jj@jjsweb.site>
- Loading branch information
1 parent
fef6f82
commit c5a8e09
Showing
21 changed files
with
452 additions
and
972 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export function checkIsNodeDebugging() { | ||
let isNodeDebugging: 'brk' | boolean = !!( | ||
process.execArgv.some((localArg) => localArg.startsWith('--inspect')) || | ||
process.env.NODE_OPTIONS?.match?.(/--inspect(=\S+)?( |$)/) | ||
) | ||
|
||
if ( | ||
process.execArgv.some((localArg) => localArg.startsWith('--inspect-brk')) || | ||
process.env.NODE_OPTIONS?.match?.(/--inspect-brk(=\S+)?( |$)/) | ||
) { | ||
isNodeDebugging = 'brk' | ||
} | ||
return isNodeDebugging | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export type PropagateToWorkersField = | ||
| 'actualMiddlewareFile' | ||
| 'actualInstrumentationHookFile' | ||
| 'reloadMatchers' | ||
| 'loadEnvConfig' | ||
| 'appPathRoutes' | ||
| 'middleware' |
Oops, something went wrong.