Skip to content

Commit

Permalink
Remove debug memory printout code
Browse files Browse the repository at this point in the history
  • Loading branch information
mknichel committed May 22, 2024
1 parent a651a98 commit ed9bcf3
Showing 1 changed file with 0 additions and 103 deletions.
103 changes: 0 additions & 103 deletions packages/next/src/build/webpack-build/impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import type { UnwrapPromise } from '../../lib/coalesced-function'

import origDebug from 'next/dist/compiled/debug'
import { Telemetry } from '../../telemetry/storage'
import prettyBytes from '../../lib/pretty-bytes'

const debug = origDebug('next:build:webpack-build')

Expand All @@ -66,105 +65,6 @@ function isTraceEntryPointsPlugin(
return plugin instanceof TraceEntryPointsPlugin
}

/**
* Prints out information about duplicate memory in Webpack that could be
* deduplicated to save memory.
*/
function printMemorySavings(
label: string,
stats: webpack.Stats | undefined
): void {
if (!stats) {
return
}
const allModules = [...(stats.compilation.modules ?? [])]
const modulesWithDuplicateContent = new Map<string, typeof allModules>()
for (const module of allModules) {
// @ts-expect-error: `module.resource` is defined on NormalModule
const resource = module.resource
if (!resource) {
continue
}
if (!modulesWithDuplicateContent.has(resource)) {
modulesWithDuplicateContent.set(resource, [module])
} else {
modulesWithDuplicateContent.get(resource)!.push(module)
}
}
let duplicateStringAndBufferMemory = 0
const sourcesOfDuplicateStringAndBufferMemory = new Map<string, number>()
for (let i = 0; i < allModules.length; i++) {
const module = allModules[i]
// @ts-expect-error: `module._source` is fine
if (module._source?._valueAsString && module._source?._valueAsBuffer) {
// @ts-expect-error: `module._source` is fine
duplicateStringAndBufferMemory += module._source._valueAsString.length
// @ts-expect-error: `module._source` is fine
const sourceConstructor = module._source?.constructor.name
if (!sourcesOfDuplicateStringAndBufferMemory.has(sourceConstructor)) {
sourcesOfDuplicateStringAndBufferMemory.set(sourceConstructor, 0)
}
sourcesOfDuplicateStringAndBufferMemory.set(
sourceConstructor,
(sourcesOfDuplicateStringAndBufferMemory.get(sourceConstructor) ?? 0) +
1
)
}
}
console.log(sourcesOfDuplicateStringAndBufferMemory.entries())
const filesWithDuplicateContent = new Set<string>()
let approxStringMemorysavings = 0
let approxBufferMemorySavings = 0
for (const [resource, modules] of modulesWithDuplicateContent) {
if (modules.length > 1) {
const firstModule = modules[0]
for (let i = 1; i < modules.length; i++) {
const module = modules[i]
if (
// @ts-expect-error: `module._source` is fine
module._source?._valueAsString === firstModule._source?._valueAsString
) {
approxStringMemorysavings +=
// @ts-expect-error: `module._source` is fine
module._source?._valueAsString?.length ?? 0
filesWithDuplicateContent.add(resource)
}
if (
// @ts-expect-error: `module._source` is fine
module._source?._valueAsBuffer?.equals(
// @ts-expect-error: `module._source` is fine
firstModule._source?._valueAsBuffer
) &&
// @ts-expect-error: `module._source` is fine
module._source?._valueAsBuffer !== firstModule._source?._valueAsBuffer
) {
approxBufferMemorySavings +=
// @ts-expect-error: `module._source` is fine
module._source?._valueAsBuffer?.length ?? 0
filesWithDuplicateContent.add(resource)
}
}
}
}
console.log('')
console.log(`------- Estimated Memory Savings (${label}) -------`)
console.log(
` - ${prettyBytes(duplicateStringAndBufferMemory)} is duplicated by defining both string and buffer in a Webpack Module.`
)
console.log(
` - ${prettyBytes(approxStringMemorysavings)} is duplicated by defining the same string in multiple Webpack NormalModules.`
)
console.log(
` - ${prettyBytes(approxBufferMemorySavings)} is duplicated by defining the same Buffer in multiple Webpack NormalModules.`
)
console.log(
` = ${prettyBytes(duplicateStringAndBufferMemory + approxStringMemorysavings + approxBufferMemorySavings)} approximate memory savings`
)
console.log(`------- Estimated Memory Savings (${label}) -------`)
console.log('')
debugger
}

export async function webpackBuildImpl(
compilerName: keyof typeof COMPILER_INDEXES | null
): Promise<{
Expand Down Expand Up @@ -313,7 +213,6 @@ export async function webpackBuildImpl(
inputFileSystem,
})
debug(`server compiler finished ${Date.now() - start}ms`)
printMemorySavings('server', serverResult.stats)
}

if (!compilerName || compilerName === 'edge-server') {
Expand All @@ -323,7 +222,6 @@ export async function webpackBuildImpl(
? await runCompiler(edgeConfig, { runWebpackSpan, inputFileSystem })
: [null]
debug(`edge-server compiler finished ${Date.now() - start}ms`)
printMemorySavings('edge-server', edgeServerResult?.stats)
}

// Only continue if there were no errors
Expand Down Expand Up @@ -359,7 +257,6 @@ export async function webpackBuildImpl(
inputFileSystem,
})
debug(`client compiler finished ${Date.now() - start}ms`)
printMemorySavings('client', clientResult.stats)
}
}

Expand Down

0 comments on commit ed9bcf3

Please sign in to comment.