-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Eliminate unnecessary decode operations in `node-web-streams-helpers.…
…ts` (#63427) This PR is strictly a performance improvement. It should not change implementation behavior in anyway. This PR replaces `decoder.decode()` operations by operating with the encoded `Uint8Array` instances directly. I added some utility functions to make things a bit easier to understand. Ideally, this change also maintains a fair amount of code readability. Will measure estimate performance improvement shortly. Closes NEXT-2848
- Loading branch information
1 parent
1439503
commit 229cb14
Showing
3 changed files
with
141 additions
and
52 deletions.
There are no files selected for viewing
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,21 @@ | ||
export const ENCODED_TAGS = { | ||
// opening tags do not have the closing `>` since they can contain other attributes such as `<body className=''>` | ||
OPENING: { | ||
// <html | ||
HTML: new Uint8Array([60, 104, 116, 109, 108]), | ||
// <body | ||
BODY: new Uint8Array([60, 98, 111, 100, 121]), | ||
}, | ||
CLOSED: { | ||
// </head> | ||
HEAD: new Uint8Array([60, 47, 104, 101, 97, 100, 62]), | ||
// </body> | ||
BODY: new Uint8Array([60, 47, 98, 111, 100, 121, 62]), | ||
// </html> | ||
HTML: new Uint8Array([60, 47, 104, 116, 109, 108, 62]), | ||
// </body></html> | ||
BODY_AND_HTML: new Uint8Array([ | ||
60, 47, 98, 111, 100, 121, 62, 60, 47, 104, 116, 109, 108, 62, | ||
]), | ||
}, | ||
} as const |
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
59 changes: 59 additions & 0 deletions
59
packages/next/src/server/stream-utils/uint8array-helpers.ts
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,59 @@ | ||
/** | ||
* Find the starting index of Uint8Array `b` within Uint8Array `a`. | ||
*/ | ||
export function indexOfUint8Array(a: Uint8Array, b: Uint8Array) { | ||
if (b.length === 0) return 0 | ||
if (a.length === 0 || b.length > a.length) return -1 | ||
|
||
// start iterating through `a` | ||
for (let i = 0; i <= a.length - b.length; i++) { | ||
let completeMatch = true | ||
// from index `i`, iterate through `b` and check for mismatch | ||
for (let j = 0; j < b.length; j++) { | ||
// if the values do not match, then this isn't a complete match, exit `b` iteration early and iterate to next index of `a`. | ||
if (a[i + j] !== b[j]) { | ||
completeMatch = false | ||
break | ||
} | ||
} | ||
|
||
if (completeMatch) { | ||
return i | ||
} | ||
} | ||
|
||
return -1 | ||
} | ||
|
||
/** | ||
* Check if two Uint8Arrays are strictly equivalent. | ||
*/ | ||
export function isEquivalentUint8Arrays(a: Uint8Array, b: Uint8Array) { | ||
if (a.length !== b.length) return false | ||
|
||
for (let i = 0; i < a.length; i++) { | ||
if (a[i] !== b[i]) return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
/** | ||
* Remove Uint8Array `b` from Uint8Array `a`. | ||
* | ||
* If `b` is not in `a`, `a` is returned unchanged. | ||
* | ||
* Otherwise, the function returns a new Uint8Array instance with size `a.length - b.length` | ||
*/ | ||
export function removeFromUint8Array(a: Uint8Array, b: Uint8Array) { | ||
const tagIndex = indexOfUint8Array(a, b) | ||
if (tagIndex === 0) return a.subarray(b.length) | ||
if (tagIndex > -1) { | ||
const removed = new Uint8Array(a.length - b.length) | ||
removed.set(a.slice(0, tagIndex)) | ||
removed.set(a.slice(tagIndex + b.length), tagIndex) | ||
return removed | ||
} else { | ||
return a | ||
} | ||
} |
229cb14
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stats from current release
Default Build (Increase detected⚠️ )
General
Client Bundles (main, webpack) Overall increase⚠️
Legacy Client Bundles (polyfills)
Client Pages
Client Build Manifests
Rendered Page Sizes
Edge SSR bundle Size Overall increase⚠️
Middleware size Overall increase⚠️
Next Runtimes Overall increase⚠️
build cache Overall increase⚠️
Diff details
Diff for page.js
Diff too large to display
Diff for middleware.js
Diff too large to display
Diff for edge-ssr.js
Diff too large to display
Diff for dynamic-HASH.js
Diff for image-HASH.js
Diff for link-HASH.js
Diff for 3f784ff6-HASH.js
Diff too large to display
Diff for 6433-HASH.js
Diff too large to display
Diff for 8201-HASH.js
Diff for main-HASH.js
Diff too large to display
Diff for polyfills-HASH.js
Diff too large to display
Diff for webpack-HASH.js
Diff for app-page-exp..ntime.dev.js
Diff for app-page-exp..time.prod.js
Diff too large to display
Diff for app-page-tur..time.prod.js
Diff too large to display
Diff for app-page-tur..time.prod.js
Diff too large to display
Diff for app-page.runtime.dev.js
Diff for app-page.runtime.prod.js
Diff too large to display
Diff for app-route-ex..ntime.dev.js
Diff too large to display
Diff for app-route-ex..time.prod.js
Diff too large to display
Diff for app-route-tu..time.prod.js
Diff too large to display
Diff for app-route-tu..time.prod.js
Diff too large to display
Diff for app-route.runtime.dev.js
Diff too large to display
Diff for app-route.ru..time.prod.js
Diff too large to display
Diff for pages-api-tu..time.prod.js
Diff too large to display
Diff for pages-api.runtime.dev.js
Diff too large to display
Diff for pages-api.ru..time.prod.js
Diff too large to display
Diff for pages-turbo...time.prod.js
Diff too large to display
Diff for pages.runtime.dev.js
Diff too large to display
Diff for pages.runtime.prod.js
Diff too large to display
Diff for server.runtime.prod.js
Diff too large to display