Skip to content

Commit

Permalink
perf: no AST nodes in caches
Browse files Browse the repository at this point in the history
  • Loading branch information
benmosher committed Apr 10, 2018
1 parent 83c85fc commit 7bd2775
Showing 1 changed file with 44 additions and 27 deletions.
71 changes: 44 additions & 27 deletions src/ExportMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,20 +265,14 @@ ExportMap.get = function (source, context) {
const path = resolve(source, context)
if (path == null) return null

return ExportMap.for(path, context)
return ExportMap.for(childContext(path, context))
}

ExportMap.for = function (path, context) {
let exportMap
ExportMap.for = function (context) {
const { path } = context

const cacheKey = hashObject({
settings: context.settings,
parserPath: context.parserPath,
parserOptions: context.parserOptions,
path,
}).digest('hex')

exportMap = exportCache.get(cacheKey)
const cacheKey = hashObject(context).digest('hex')
let exportMap = exportCache.get(cacheKey)

// return cached ignore
if (exportMap === null) return null
Expand Down Expand Up @@ -307,6 +301,7 @@ ExportMap.for = function (path, context) {
return null
}

log('cache miss', cacheKey, 'for path', path)
exportMap = ExportMap.parse(path, content, context)

// ambiguous modules return null
Expand Down Expand Up @@ -355,14 +350,14 @@ ExportMap.parse = function (path, content, context) {

const namespaces = new Map()

function remotePath(node) {
return resolve.relative(node.source.value, path, context.settings)
function remotePath(value) {
return resolve.relative(value, path, context.settings)
}

function resolveImport(node) {
const rp = remotePath(node)
function resolveImport(value) {
const rp = remotePath(value)
if (rp == null) return null
return ExportMap.for(rp, context)
return ExportMap.for(childContext(rp, context))
}

function getNamespace(identifier) {
Expand All @@ -385,12 +380,20 @@ ExportMap.parse = function (path, content, context) {
function captureDependency(declaration) {
if (declaration.source == null) return null

const p = remotePath(declaration)
if (p == null || m.imports.has(p)) return p

const getter = () => ExportMap.for(p, context)
m.imports.set(p, { getter, source: declaration.source })
return p
const p = remotePath(declaration.source.value)
if (p == null) return null
const existing = m.imports.get(p)
if (existing != null) return existing.getter

const getter = () => ExportMap.for(childContext(p, context))
m.imports.set(p, {
getter,
source: { // capturing actual node reference holds full AST in memory!
value: declaration.source.value,
loc: declaration.source.loc,
},
})
return getter
}


Expand All @@ -406,8 +409,8 @@ ExportMap.parse = function (path, content, context) {
}

if (n.type === 'ExportAllDeclaration') {
const p = captureDependency(n)
if (p) m.dependencies.add(m.imports.get(p).getter)
const getter = captureDependency(n)
if (getter) m.dependencies.add(getter)
return
}

Expand All @@ -416,7 +419,7 @@ ExportMap.parse = function (path, content, context) {
captureDependency(n)
let ns
if (n.specifiers.some(s => s.type === 'ImportNamespaceSpecifier' && (ns = s))) {
namespaces.set(ns.local.name, n)
namespaces.set(ns.local.name, n.source.value)
}
return
}
Expand All @@ -443,6 +446,7 @@ ExportMap.parse = function (path, content, context) {
}
}

const nsource = n.source && n.source.value
n.specifiers.forEach((s) => {
const exportMeta = {}
let local
Expand All @@ -454,7 +458,7 @@ ExportMap.parse = function (path, content, context) {
break
case 'ExportNamespaceSpecifier':
m.namespace.set(s.exported.name, Object.defineProperty(exportMeta, 'namespace', {
get() { return resolveImport(n) },
get() { return resolveImport(nsource) },
}))
return
case 'ExportSpecifier':
Expand All @@ -469,7 +473,7 @@ ExportMap.parse = function (path, content, context) {
}

// todo: JSDoc
m.reexports.set(s.exported.name, { local, getImport: () => resolveImport(n) })
m.reexports.set(s.exported.name, { local, getImport: () => resolveImport(nsource) })
})
}
})
Expand Down Expand Up @@ -505,3 +509,16 @@ export function recursivePatternCapture(pattern, callback) {
break
}
}

/**
* don't hold full context object in memory, just grab what we need.
*/
function childContext(path, context) {
const { settings, parserOptions, parserPath } = context
return {
settings,
parserOptions,
parserPath,
path,
}
}

0 comments on commit 7bd2775

Please sign in to comment.