Skip to content

Commit

Permalink
Use maps for memoize caches.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Sep 16, 2018
1 parent d0a33f6 commit 96c8257
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/builtin-entries.js
Expand Up @@ -126,7 +126,7 @@ const cache = shared.memoize.builtinEntries

for (const id of builtinIds) {
setDeferred(builtinEntries, id, () => {
const cached = cache[id]
const cached = cache.get(id)

if (cached) {
return cached
Expand All @@ -135,7 +135,7 @@ for (const id of builtinIds) {
const entry = createEntry(id)

if (id !== "module") {
cache[id] = entry
cache.set(id, entry)
}

return entry
Expand Down
4 changes: 2 additions & 2 deletions src/builtin-modules.js
Expand Up @@ -34,7 +34,7 @@ function getExports(id) {

for (const id of builtinIds) {
setDeferred(builtinModules, id, () => {
const cached = cache[id]
const cached = cache.get(id)

if (cached) {
return cached
Expand All @@ -46,7 +46,7 @@ for (const id of builtinIds) {
mod.loaded = true

if (id !== "module") {
cache[id] = mod
cache.set(id, mod)
}

return mod
Expand Down
8 changes: 6 additions & 2 deletions src/fs/realpath.js
Expand Up @@ -28,7 +28,8 @@ function init() {
}

const cache = shared.memoize.fsRealpath
const cached = cache[thePath]

let cached = cache.get(thePath)

if (cached) {
return cached
Expand All @@ -44,9 +45,12 @@ function init() {
typeof binding.fs.realpath === "function"
}

return cache[thePath] = useBinding
cached = useBinding
? realpathBinding(thePath)
: realpathFallback(thePath)

cache.set(thePath, cached)
return cached
}

function realpathBinding(thePath) {
Expand Down
21 changes: 11 additions & 10 deletions src/module/esm/resolve-filename.js
Expand Up @@ -91,22 +91,21 @@ function resolveFilename(request, parent, isMain, options) {
fromPath = ""
}

const cache = shared.memoize.moduleESMResolveFilename

let cacheKey

if (isObject(options)) {
cacheKey = ""
} else {
if (! isObject(options)) {
cacheKey =
request + "\0" +
fromPath + "\0" +
(isMain ? "1" : "")
}

const cache = shared.memoize.moduleESMResolveFilename
const cached = cacheKey && cache[cacheKey]
const cached = cache.get(cacheKey)

if (cached) {
return cached
if (cached) {
return cached
}
}

if (isAbs) {
Expand Down Expand Up @@ -172,7 +171,8 @@ function resolveFilename(request, parent, isMain, options) {

if (! foundPath &&
Reflect.has(builtinLookup, decoded)) {
return cache[cacheKey] = decoded
cache.set(cacheKey, decoded)
return decoded
}
}

Expand All @@ -183,7 +183,8 @@ function resolveFilename(request, parent, isMain, options) {
isJS(foundPath) ||
isMJS(foundPath) ||
Reflect.has(strictExtsLookup, extname(foundPath))) {
return cache[cacheKey] = foundPath
cache.set(cacheKey, foundPath)
return foundPath
}

throw new ERR_UNKNOWN_FILE_EXTENSION(foundPath)
Expand Down
5 changes: 3 additions & 2 deletions src/module/internal/find-path.js
Expand Up @@ -55,7 +55,7 @@ function findPath(request, paths, isMain, fields, exts) {
}

const cache = shared.memoize.moduleInternalFindPath
const cached = cache[cacheKey]
const cached = cache.get(cacheKey)

if (cached !== void 0) {
return cached
Expand Down Expand Up @@ -172,7 +172,8 @@ function findPath(request, paths, isMain, fields, exts) {
}

if (filename) {
return cache[cacheKey] = filename
cache.set(cacheKey, filename)
return filename
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/module/internal/read-package.js
@@ -1,4 +1,5 @@
import GenericArray from "../../generic/array.js"
import GenericObject from "../../generic/object.js"
import SafeJSON from "../../safe/json.js"

import readFileFast from "../../fs/read-file-fast.js"
Expand All @@ -18,8 +19,10 @@ function readPackage(dirPath, fields) {
cacheKey += "\0" + (fieldsLength === 1 ? fields[0] : GenericArray.join(fields))
}

if (Reflect.has(cache, cacheKey)) {
return cache[cacheKey]
let cached = cache.get(cacheKey)

if (cached) {
return cached
}

const jsonPath = dirPath + sep + "package.json"
Expand All @@ -33,12 +36,17 @@ function readPackage(dirPath, fields) {
}

try {
return cache[cacheKey] = SafeJSON.parse(jsonString)
cached =
SafeJSON.parse(jsonString) ||
GenericObject.create()
} catch (e) {
e.path = jsonPath
e.message = "Error parsing " + jsonPath + ": " + toString(e.message)
throw e
}

cache.set(cacheKey, cached)
return cached
}

export default readPackage
23 changes: 12 additions & 11 deletions src/module/static/resolve-filename.js
Expand Up @@ -61,22 +61,21 @@ function resolveFilename(request, parent, isMain, options) {
fromPath = ""
}

const cache = shared.memoize.moduleStaticResolveFilename

let cacheKey

if (isObject(options)) {
cacheKey = ""
} else {
if (! isObject(options)) {
cacheKey =
request + "\0" +
fromPath + "\0" +
(isMain ? "1" : "")
}

const cache = shared.memoize.moduleStaticResolveFilename
const cached = cacheKey && cache[cacheKey]
const cached = cache.get(cacheKey)

if (cached) {
return cached
if (cached) {
return cached
}
}

if (isAbs) {
Expand All @@ -102,9 +101,11 @@ function resolveFilename(request, parent, isMain, options) {
const foundPath = Module._findPath(request, paths, isMain)

if (foundPath) {
return cacheKey
? cache[cacheKey] = foundPath
: foundPath
if (cacheKey) {
cache.set(cacheKey, foundPath)
}

return foundPath
}

throw new MODULE_NOT_FOUND(request)
Expand Down
20 changes: 10 additions & 10 deletions src/shared.js
Expand Up @@ -62,21 +62,21 @@ function init() {
external: __external__,
inited: false,
memoize: {
builtinEntries: { __proto__: null },
builtinModules: { __proto__: null },
fsRealpath: { __proto__: null },
moduleESMResolveFilename: { __proto__: null },
moduleInternalFindPath: { __proto__: null },
moduleInternalReadPackage: { __proto__: null },
moduleStaticResolveFilename: { __proto__: null },
builtinEntries: new Map,
builtinModules: new Map,
fsRealpath: new Map,
moduleESMResolveFilename: new Map,
moduleInternalFindPath: new Map,
moduleInternalReadPackage: new Map,
moduleStaticResolveFilename: new Map,
shimFunctionPrototypeToString: new WeakMap,
shimProcessBindingUtilGetProxyDetails: new WeakMap,
utilGetProxyDetails: new WeakMap,
utilMaskFunction: new WeakMap,
utilMaxSatisfying: { __proto__: null },
utilParseURL: { __proto__: null },
utilMaxSatisfying: new Map,
utilParseURL: new Map,
utilProxyExports: new WeakMap,
utilSatisfies: { __proto__: null },
utilSatisfies: new Map,
utilUnwrapOwnProxy: new WeakMap,
utilUnwrapProxy: new WeakMap
},
Expand Down
12 changes: 8 additions & 4 deletions src/util/max-satisfying.js
Expand Up @@ -16,11 +16,15 @@ function init() {
range

const cache = shared.memoize.utilMaxSatisfying
const cached = cache[cacheKey]

return cached === void 0
? cache[cacheKey] = _maxSatisfying(versions, range)
: cached
let cached = cache.get(cacheKey)

if (cached === void 0) {
cached = _maxSatisfying(versions, range)
cache.set(cacheKey, cached)
}

return cached
}

return maxSatisfying
Expand Down
9 changes: 8 additions & 1 deletion src/util/parse-url.js
Expand Up @@ -27,7 +27,14 @@ function init() {
const cacheKey = typeof url === "string" ? url : ""
const cache = shared.memoize.utilParseURL

return cache[cacheKey] || (cache[cacheKey] = parse(url))
let cached = cache.get(cacheKey)

if (cached === void 0) {
cached = parse(url)
cache.set(cacheKey, cached)
}

return cached
}

return parseURL
Expand Down
12 changes: 8 additions & 4 deletions src/util/satisfies.js
Expand Up @@ -11,11 +11,15 @@ function init() {

const cacheKey = version + "\0" + range
const cache = shared.memoize.utilSatisfies
const cached = cache[cacheKey]

return cached === void 0
? cache[cacheKey] = _satisfies(stripPrereleaseTag(version), range)
: cached
let cached = cache.get(cacheKey)

if (cached === void 0) {
cached = _satisfies(stripPrereleaseTag(version), range)
cache.set(cacheKey, cached)
}

return cached
}

return satisfies
Expand Down

0 comments on commit 96c8257

Please sign in to comment.