Skip to content

Commit

Permalink
perf: lazyload the edge runtime in NextServer (#41322)
Browse files Browse the repository at this point in the history
## Context

I'm investigating cold boot perf

## This PR

Did some manual analysis on next cold boot for the "regular" node target
and found out that we are spending a good 10ms evaluating the edge
runtime module when we shouldn't be in most cases.

This is a quick fix to only load it when actually required.

## Test plan

I have a simple script which I'll push in a separate PR to measure the
cold boot.

Results:


before:
<img width="230" alt="image"
src="https://user-images.githubusercontent.com/11064311/195073994-c3d28961-5d22-401f-a4b0-026c791ff2bd.png">

after:
<img width="275" alt="image"
src="https://user-images.githubusercontent.com/11064311/195073941-a0897532-6e9f-482d-a1f0-8b9c9231c6b0.png">

it appears to be a 10~15ms win on cold boot 

## Following up

We probably want a separate NextMinimalServer type of build?

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
  • Loading branch information
feedthejim committed Oct 11, 2022
1 parent 701e8d2 commit ba08576
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/next/server/next-server.ts
Expand Up @@ -54,7 +54,7 @@ import HttpProxy from 'next/dist/compiled/http-proxy'
import { getPathMatch } from '../shared/lib/router/utils/path-match'
import { createHeaderRoute, createRedirectRoute } from './server-route-utils'
import getRouteFromAssetPath from '../shared/lib/router/utils/get-route-from-asset-path'
import { run } from './web/sandbox'

import { detectDomainLocale } from '../shared/lib/i18n/detect-domain-locale'

import { NodeNextRequest, NodeNextResponse } from './base-http/node'
Expand All @@ -68,7 +68,6 @@ import { ParsedUrl, parseUrl } from '../shared/lib/router/utils/parse-url'
import { parse as nodeParseUrl } from 'url'
import * as Log from '../build/output/log'
import loadRequireHook from '../build/webpack/require-hook'
import { consumeUint8ArrayReadableStream } from 'next/dist/compiled/edge-runtime'

import BaseServer, {
Options,
Expand Down Expand Up @@ -1766,6 +1765,7 @@ export default class NextNodeServer extends BaseServer {
}

const method = (params.request.method || 'GET').toUpperCase()
const { run } = require('./web/sandbox') as typeof import('./web/sandbox')

const result = await run({
distDir: this.distDir,
Expand Down Expand Up @@ -2089,6 +2089,7 @@ export default class NextNodeServer extends BaseServer {
)
}

const { run } = require('./web/sandbox') as typeof import('./web/sandbox')
const result = await run({
distDir: this.distDir,
name: edgeInfo.name,
Expand Down Expand Up @@ -2129,6 +2130,8 @@ export default class NextNodeServer extends BaseServer {
if (result.response.body) {
// TODO(gal): not sure that we always need to stream
const nodeResStream = (params.res as NodeNextResponse).originalResponse
const { consumeUint8ArrayReadableStream } =
require('next/dist/compiled/edge-runtime') as typeof import('next/dist/compiled/edge-runtime')
try {
for await (const chunk of consumeUint8ArrayReadableStream(
result.response.body
Expand Down

0 comments on commit ba08576

Please sign in to comment.