Skip to content

Commit

Permalink
Use Map for the stat cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Sep 17, 2018
1 parent 4419e2d commit 38accbe
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
17 changes: 11 additions & 6 deletions src/fs/stat-fast.js
Expand Up @@ -18,18 +18,23 @@ function init() {

const cache = shared.moduleState.statFast

if (cache &&
Reflect.has(cache, thePath)) {
return cache[thePath]
let cached

if (cache) {
cached = cache.get(thePath)

if (cached) {
return cached
}
}

const result = statBase(thePath)
cached = statBase(thePath)

if (cache) {
cache[thePath] = result
cache.set(thePath, cached)
}

return result
return cached
}

function statBase(thePath) {
Expand Down
31 changes: 20 additions & 11 deletions src/fs/stat-sync.js
Expand Up @@ -13,32 +13,41 @@ function init() {
const { prototype } = Stats

function statSync(thePath) {
if (typeof thePath !== "string") {
return null
}

const cache = shared.moduleState.statSync

if (cache &&
Reflect.has(cache, thePath)) {
return cache[thePath]
}
let cached

if (cache) {
cached = cache.get(thePath)

let result = null
if (cached !== void 0) {
return cached
}
}

try {
result = _statSync(thePath)
cached = _statSync(thePath)

// Electron and Muon return a plain object for asar files.
// https://github.com/electron/electron/blob/master/lib/common/asar.js
// https://github.com/brave/muon/blob/master/lib/common/asar.js
if (ELECTRON &&
! (result instanceof Stats)) {
setPrototypeOf(result, prototype)
! (cached instanceof Stats)) {
setPrototypeOf(cached, prototype)
}
} catch {}
} catch {
cached = null
}

if (cache) {
cache[thePath] = result
cache.set(thePath, cached)
}

return result
return cached
}

return statSync
Expand Down
13 changes: 10 additions & 3 deletions src/module/proto/compile.js
Expand Up @@ -143,7 +143,14 @@ function compile(content, filename) {
const { moduleState } = shared
const noDepth = moduleState.requireDepth === 0
const req = makeRequireFunction(this)
const args = [exported, req, this, filename, dirname(filename)]

const args = [
exported,
req,
this,
filename,
dirname(filename)
]

if (ELECTRON) {
args.push(realProcess, shared.unsafeGlobal)
Expand All @@ -158,8 +165,8 @@ function compile(content, filename) {
}

if (noDepth) {
moduleState.statFast = { __proto__: null }
moduleState.statSync = { __proto__: null }
moduleState.statFast = new Map
moduleState.statSync = new Map
}

entry.state = STATE_EXECUTION_STARTED
Expand Down

0 comments on commit 38accbe

Please sign in to comment.