Unverified Commit 05ec5674 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[sourcemaps] Improve ignore-listing performance (#81311)

This improves performance of finding the relevant section in Index Source Maps by using binary search (O(N) -> O(log N)).

This allows us to improve the performance of the fast path for checking if a source is ignore-listed in Index Source Maps:
Instead of checking if every section in Index Source Maps ignores everything, we just find the relevant section (now O(log N) instead of O(N)) and check if that section ignores everything.
parent 3a41715c
...@@ -5,7 +5,9 @@ import { SourceMapConsumer } from 'next/dist/compiled/source-map08' ...@@ -5,7 +5,9 @@ import { SourceMapConsumer } from 'next/dist/compiled/source-map08'
import type { StackFrame } from 'next/dist/compiled/stacktrace-parser' import type { StackFrame } from 'next/dist/compiled/stacktrace-parser'
import { getSourceMapFromFile } from './get-source-map-from-file' import { getSourceMapFromFile } from './get-source-map-from-file'
import { import {
findApplicableSourceMapPayload,
sourceMapIgnoreListsEverything, sourceMapIgnoreListsEverything,
type BasicSourceMapPayload,
type ModernSourceMapPayload, type ModernSourceMapPayload,
} from '../lib/source-maps' } from '../lib/source-maps'
import { openFileInEditor } from '../../next-devtools/server/launch-editor' import { openFileInEditor } from '../../next-devtools/server/launch-editor'
...@@ -50,13 +52,13 @@ type SourceAttributes = { ...@@ -50,13 +52,13 @@ type SourceAttributes = {
type Source = type Source =
| { | {
type: 'file' type: 'file'
sourceMap: ModernSourceMapPayload sourceMap: BasicSourceMapPayload
ignoredSources: IgnoredSources ignoredSources: IgnoredSources
moduleURL: string moduleURL: string
} }
| { | {
type: 'bundle' type: 'bundle'
sourceMap: ModernSourceMapPayload sourceMap: BasicSourceMapPayload
ignoredSources: IgnoredSources ignoredSources: IgnoredSources
compilation: webpack.Compilation compilation: webpack.Compilation
moduleId: string moduleId: string
...@@ -284,11 +286,16 @@ async function getSourceMapFromCompilation( ...@@ -284,11 +286,16 @@ async function getSourceMapFromCompilation(
} }
async function getSource( async function getSource(
sourceURL: string, frame: {
file: string | null
lineNumber: number | null
column: number | null
},
options: { options: {
getCompilations: () => webpack.Compilation[] getCompilations: () => webpack.Compilation[]
} }
): Promise<Source | undefined> { ): Promise<Source | undefined> {
let sourceURL = frame.file ?? ''
const { getCompilations } = options const { getCompilations } = options
// Rspack is now using file:// URLs for source maps. Remove the rsc prefix to produce the file:/// url. // Rspack is now using file:// URLs for source maps. Remove the rsc prefix to produce the file:/// url.
...@@ -308,7 +315,11 @@ async function getSource( ...@@ -308,7 +315,11 @@ async function getSource(
const sourceMapPayload = nativeSourceMap.payload const sourceMapPayload = nativeSourceMap.payload
return { return {
type: 'file', type: 'file',
sourceMap: sourceMapPayload, sourceMap: findApplicableSourceMapPayload(
frame.lineNumber ?? 0,
frame.column ?? 0,
sourceMapPayload
)!,
ignoredSources: getIgnoredSources( ignoredSources: getIgnoredSources(
// @ts-expect-error -- TODO: Support IndexSourceMap // @ts-expect-error -- TODO: Support IndexSourceMap
...@@ -435,7 +446,7 @@ async function getOriginalStackFrame({ ...@@ -435,7 +446,7 @@ async function getOriginalStackFrame({
rootDirectory: string rootDirectory: string
}): Promise<OriginalStackFrameResponse> { }): Promise<OriginalStackFrameResponse> {
const filename = frame.file ?? '' const filename = frame.file ?? ''
const source = await getSource(filename, { const source = await getSource(frame, {
getCompilations: () => { getCompilations: () => {
const compilations: webpack.Compilation[] = [] const compilations: webpack.Compilation[] = []
...@@ -658,23 +669,31 @@ export function getSourceMapMiddleware(options: { ...@@ -658,23 +669,31 @@ export function getSourceMapMiddleware(options: {
let source: Source | undefined let source: Source | undefined
try { try {
source = await getSource(filename, { source = await getSource(
getCompilations: () => { {
const compilations: webpack.Compilation[] = [] file: filename,
// Webpack doesn't use Index Source Maps
for (const stats of [ lineNumber: null,
clientStats(), column: null,
serverStats(), },
edgeServerStats(), {
]) { getCompilations: () => {
if (stats?.compilation) { const compilations: webpack.Compilation[] = []
compilations.push(stats.compilation)
for (const stats of [
clientStats(),
serverStats(),
edgeServerStats(),
]) {
if (stats?.compilation) {
compilations.push(stats.compilation)
}
} }
}
return compilations return compilations
}, },
}) }
)
} catch (error) { } catch (error) {
return middlewareResponse.internalServerError(res, error) return middlewareResponse.internalServerError(res, error)
} }
......
...@@ -18,7 +18,7 @@ interface IndexSourceMap { ...@@ -18,7 +18,7 @@ interface IndexSourceMap {
} }
/** https://tc39.es/ecma426/#sec-source-map-format */ /** https://tc39.es/ecma426/#sec-source-map-format */
interface BasicSourceMapPayload { export interface BasicSourceMapPayload {
version: number version: number
// TODO: Move to https://github.com/jridgewell/sourcemaps which is actively maintained // TODO: Move to https://github.com/jridgewell/sourcemaps which is actively maintained
/** WARNING: `file` is optional. */ /** WARNING: `file` is optional. */
...@@ -34,17 +34,9 @@ interface BasicSourceMapPayload { ...@@ -34,17 +34,9 @@ interface BasicSourceMapPayload {
export type ModernSourceMapPayload = BasicSourceMapPayload | IndexSourceMap export type ModernSourceMapPayload = BasicSourceMapPayload | IndexSourceMap
// TODO: This should take the BasicSourceMapPayload. Only the relevant section
// needs to ignore list everything making this check effectively O(1) multiplied
// by the complexity of finding the section.
export function sourceMapIgnoreListsEverything( export function sourceMapIgnoreListsEverything(
sourceMap: ModernSourceMapPayload sourceMap: BasicSourceMapPayload
): boolean { ): boolean {
if ('sections' in sourceMap) {
return sourceMap.sections.every((section) => {
return sourceMapIgnoreListsEverything(section.map)
})
}
return ( return (
sourceMap.ignoreList !== undefined && sourceMap.ignoreList !== undefined &&
sourceMap.sources.length === sourceMap.ignoreList.length sourceMap.sources.length === sourceMap.ignoreList.length
...@@ -56,26 +48,40 @@ export function sourceMapIgnoreListsEverything( ...@@ -56,26 +48,40 @@ export function sourceMapIgnoreListsEverything(
* Equal to the input unless an Index Source Map is used. * Equal to the input unless an Index Source Map is used.
*/ */
export function findApplicableSourceMapPayload( export function findApplicableSourceMapPayload(
lineNumber: number, line: number,
columnNumber: number, column: number,
payload: ModernSourceMapPayload payload: ModernSourceMapPayload
): BasicSourceMapPayload | undefined { ): BasicSourceMapPayload | undefined {
if ('sections' in payload) { if ('sections' in payload) {
if (payload.sections.length === 0) {
return undefined
}
// Sections must not overlap and must be sorted: https://tc39.es/source-map/#section-object // Sections must not overlap and must be sorted: https://tc39.es/source-map/#section-object
// Therefore the last section that has an offset less than or equal to the frame is the applicable one. // Therefore the last section that has an offset less than or equal to the frame is the applicable one.
// TODO(veil): Binary search const sections = payload.sections
let section: IndexSourceMapSection | undefined = payload.sections[0] let left = 0
for ( let right = sections.length - 1
let i = 0; let result: IndexSourceMapSection | null = null
i < payload.sections.length &&
payload.sections[i].offset.line <= lineNumber && while (left <= right) {
payload.sections[i].offset.column <= columnNumber; // fast Math.floor
i++ const middle = ~~((left + right) / 2)
) { const section = sections[middle]
section = payload.sections[i] const offset = section.offset
if (
offset.line < line ||
(offset.line === line && offset.column <= column)
) {
result = section
left = middle + 1
} else {
right = middle - 1
}
} }
return section === undefined ? undefined : section.map return result === null ? undefined : result.map
} else { } else {
return payload return payload
} }
......
...@@ -208,7 +208,14 @@ function getSourcemappedFrameIfPossible( ...@@ -208,7 +208,14 @@ function getSourcemappedFrameIfPossible(
line: frame.lineNumber ?? 1, line: frame.lineNumber ?? 1,
}) })
let ignored = sourceMapIgnoreListsEverything(sourceMapPayload) const applicableSourceMap = findApplicableSourceMapPayload(
frame.lineNumber ?? 0,
frame.column ?? 0,
sourceMapPayload
)
let ignored =
applicableSourceMap !== undefined &&
sourceMapIgnoreListsEverything(applicableSourceMap)
if (sourcePosition.source === null) { if (sourcePosition.source === null) {
return { return {
stack: { stack: {
...@@ -223,11 +230,6 @@ function getSourcemappedFrameIfPossible( ...@@ -223,11 +230,6 @@ function getSourcemappedFrameIfPossible(
} }
} }
const applicableSourceMap = findApplicableSourceMapPayload(
frame.lineNumber ?? 0,
frame.column ?? 0,
sourceMapPayload
)
// TODO(veil): Upstream a method to sourcemap consumer that immediately says if a frame is ignored or not. // TODO(veil): Upstream a method to sourcemap consumer that immediately says if a frame is ignored or not.
if (applicableSourceMap === undefined) { if (applicableSourceMap === undefined) {
console.error('No applicable source map found in sections for frame', frame) console.error('No applicable source map found in sections for frame', frame)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment