Skip to content

Commit

Permalink
perf: analyzer process module logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Mar 20, 2024
1 parent 4232b82 commit ef913f0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 34 deletions.
35 changes: 3 additions & 32 deletions src/server/analyzer-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,6 @@ export class AnalyzerNode {
imports.forEach((imp) => this.imports.add(imp))
}

private traverse(nodes: Array<GroupWithNode>, handler: (node: GroupWithNode) => void) {
for (const node of nodes) {
if (node.groups && node.groups.length) {
this.traverse(node.groups, handler)
handler(node)
}
if (node.groups && node.groups.length === 0) {
delete (node as any).groups
}
}
}

async setup(bundle: WrappedChunk, pluginContext: PluginContext, compress: ReturnType<typeof createGzip>, workspaceRoot: string) {
const { imports, dynamicImports, map, moduleIds } = bundle
this.addImports(...imports, ...dynamicImports)
Expand Down Expand Up @@ -144,27 +132,10 @@ export class AnalyzerNode {
}

stats.mergePrefixSingleDirectory()
stats.walk(stats.root, (id, n, p) => {
const { meta, groups } = pick(n, ['groups', 'meta'])
p.groups.push({ id, label: id, ...meta, groups })
})

stats.walk(stats.root, (c, p) => p.groups.push(c))
sources.mergePrefixSingleDirectory()
sources.walk(sources.root, (id, n, p) => {
const { meta, groups } = pick(n, ['groups', 'meta'])
p.groups.push({ id, label: id, ...meta, groups })
})
this.traverse(stats.root.groups, (node) => {
node.statSize = node.groups.reduce((acc, cur) => acc + cur.statSize, 0)
})
this.traverse(sources.root.groups, (node) => {
const size = node.groups.reduce((acc, cur) => {
acc.parsedSize += cur.parsedSize
acc.gzipSize += cur.gzipSize
return acc
}, { parsedSize: 0, gzipSize: 0 })
Object.assign(node, size)
})
sources.walk(sources.root, (c, p) => p.groups.push(c))

this.stats = stats.root.groups
this.source = sources.root.groups
// Fix correect size
Expand Down
23 changes: 21 additions & 2 deletions src/server/trie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,30 @@ export class FileSystemTrie<T> {
}
}

walk(node: Node<T>, handler: (id: string, node: Node<T>, parent: GroupWithNode) => void) {
walk(node: Node<T>, handler: (child: GroupWithNode, parent: GroupWithNode) => any) {
if (!node.children.size) return
for (const [id, childNode] of node.children.entries()) {
handler(id, childNode, node as unknown as GroupWithNode)
const child: GroupWithNode = { ...childNode.meta, id, label: id, groups: childNode.groups }
if (childNode.isEndOfPath) {
delete (child as any).groups
}
handler(child, node as unknown as GroupWithNode)
this.walk(childNode, handler)
if (child.groups && child.groups.length) {
switch (node.kind) {
case 'stat':
child.statSize = child.groups.reduce((acc, cur) => acc + cur.statSize, 0)
break
case 'source':{
const size = child.groups.reduce((acc, cur) => {
acc.parsedSize += cur.parsedSize
acc.gzipSize += cur.gzipSize
return acc
}, { parsedSize: 0, gzipSize: 0 })
Object.assign(child, size)
}
}
}
}
// memory free
node.children.clear()
Expand Down

0 comments on commit ef913f0

Please sign in to comment.