fix: use WeakMap caching for ignoreList Set conversion to avoid O(n) creation cost

Co-authored-by: 's avatarshuding <3676859+shuding@users.noreply.github.com>
parent 3912af4e
...@@ -15,11 +15,66 @@ jest.mock('node:module', () => ({ ...@@ -15,11 +15,66 @@ jest.mock('node:module', () => ({
findSourceMap: jest.fn(), findSourceMap: jest.fn(),
})) }))
// Import the function we're testing - we'll need to access the internal logic // Test the WeakMap caching mechanism
// Since nativeTraceSource is not exported, we'll test the logic by creating a test function
// that uses the same pattern
describe('middleware-turbopack source map ignore list optimization', () => { describe('middleware-turbopack source map ignore list optimization', () => {
describe('WeakMap caching for ignore list Sets', () => {
it('should cache Set creation for the same ignoreList array', () => {
// Create a mock of the caching logic we implemented
const ignoreListSetCache = new WeakMap<number[], Set<number>>()
const ignoreList = [0, 2, 4]
// First access - Set should be created and cached
let cachedSet = ignoreListSetCache.get(ignoreList)
if (!cachedSet) {
cachedSet = new Set(ignoreList)
ignoreListSetCache.set(ignoreList, cachedSet)
}
const firstSet = cachedSet
// Second access - should retrieve the same Set from cache
let cachedSet2 = ignoreListSetCache.get(ignoreList)
if (!cachedSet2) {
cachedSet2 = new Set(ignoreList)
ignoreListSetCache.set(ignoreList, cachedSet2)
}
// Should be the exact same Set object (cached)
expect(cachedSet2).toBe(firstSet)
expect(cachedSet2.has(0)).toBe(true)
expect(cachedSet2.has(1)).toBe(false)
expect(cachedSet2.has(2)).toBe(true)
})
it('should create different Sets for different ignoreList arrays', () => {
const ignoreListSetCache = new WeakMap<number[], Set<number>>()
const ignoreList1 = [0, 2]
const ignoreList2 = [1, 3]
// Create Sets for different arrays
let set1 = ignoreListSetCache.get(ignoreList1)
if (!set1) {
set1 = new Set(ignoreList1)
ignoreListSetCache.set(ignoreList1, set1)
}
let set2 = ignoreListSetCache.get(ignoreList2)
if (!set2) {
set2 = new Set(ignoreList2)
ignoreListSetCache.set(ignoreList2, set2)
}
// Should be different Set objects
expect(set1).not.toBe(set2)
expect(set1.has(0)).toBe(true)
expect(set1.has(1)).toBe(false)
expect(set2.has(0)).toBe(false)
expect(set2.has(1)).toBe(true)
})
})
// Test the core logic that we're optimizing // Test the core logic that we're optimizing
describe('ignore list performance optimization', () => { describe('ignore list performance optimization', () => {
it('should handle empty ignore list correctly', () => { it('should handle empty ignore list correctly', () => {
......
...@@ -25,6 +25,9 @@ import { findSourceMap, type SourceMap } from 'node:module' ...@@ -25,6 +25,9 @@ import { findSourceMap, type SourceMap } from 'node:module'
import { fileURLToPath, pathToFileURL } from 'node:url' import { fileURLToPath, pathToFileURL } from 'node:url'
import { inspect } from 'node:util' import { inspect } from 'node:util'
// Cache for converting ignoreList arrays to Sets to avoid O(n) creation cost on every lookup
const ignoreListSetCache = new WeakMap<number[], Set<number>>()
function shouldIgnorePath(modulePath: string): boolean { function shouldIgnorePath(modulePath: string): boolean {
return ( return (
modulePath.includes('node_modules') || modulePath.includes('node_modules') ||
...@@ -244,16 +247,18 @@ async function nativeTraceSource( ...@@ -244,16 +247,18 @@ async function nativeTraceSource(
frame frame
) )
} else { } else {
// Optimize ignore list lookup from O(n^2) to O(1) using Set // Optimize ignore list lookup from O(n^2) to O(1) using cached Set
const sourceIndex = originalPosition.source const sourceIndex = originalPosition.source
? applicableSourceMap.sources.indexOf(originalPosition.source) ? applicableSourceMap.sources.indexOf(originalPosition.source)
: -1 : -1
if (sourceIndex !== -1 && applicableSourceMap.ignoreList) { if (sourceIndex !== -1 && applicableSourceMap.ignoreList) {
// For large ignore lists, Set lookup is more efficient than Array.includes // Use WeakMap cache to avoid O(n) Set creation on every lookup
// For very small lists (< 10 items), Array.includes might be faster due to Set creation overhead let ignoreSet = ignoreListSetCache.get(applicableSourceMap.ignoreList)
// But source maps can have hundreds or thousands of sources, making Set worthwhile if (!ignoreSet) {
const ignoreSet = new Set(applicableSourceMap.ignoreList) ignoreSet = new Set(applicableSourceMap.ignoreList)
ignoreListSetCache.set(applicableSourceMap.ignoreList, ignoreSet)
}
ignored = ignoreSet.has(sourceIndex) ignored = ignoreSet.has(sourceIndex)
} else { } else {
// When sourcemap is not available, fallback to checking `frame.file`. // When sourcemap is not available, fallback to checking `frame.file`.
......
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