Skip to content

Commit 805b35e

Browse files
committed
Use more maps.
1 parent 16035f6 commit 805b35e

11 files changed

+148
-139
lines changed

esm.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,13 @@ shared = loadESM()
161161
if (cachePath !== "") {
162162
const { dir } = shared.package
163163

164-
if (! has(dir, cachePath)) {
165-
dir[cachePath] = {
164+
let cache = dir.get(cachePath)
165+
166+
if (cache === void 0) {
167+
cache = {
166168
buffer: cachedData,
167-
compile: {
168-
__proto__: null,
169-
esm: {
169+
compile: new Map([
170+
["esm", {
170171
circular: 0,
171172
code: null,
172173
codeWithTDZ: null,
@@ -177,16 +178,24 @@ if (cachePath !== "") {
177178
sourceType: 1,
178179
transforms: 0,
179180
yieldIndex: -1
180-
}
181-
},
182-
map: { __proto__: null }
181+
}]
182+
]),
183+
meta: new Map
183184
}
185+
186+
dir.set(cachePath, cache)
184187
}
185188

186-
shared.pendingScripts[cachePath] = {
187-
__proto__: null,
188-
esm: script
189+
const { pendingScripts } = shared
190+
191+
let scripts = pendingScripts.get(cachePath)
192+
193+
if (scripts === void 0) {
194+
scripts = new Map
195+
pendingScripts.set(cachePath, scripts)
189196
}
197+
198+
scripts.set("esm", script)
190199
}
191200

192201
// The legacy symbol used for `esm` export detection.

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "esm",
3-
"version": "3.2.5",
3+
"version": "3.2.6-pre",
44
"description": "Tomorrow's ECMAScript modules today!",
55
"keywords": "commonjs, ecmascript, export, import, modules, node, require",
66
"repository": "standard-things/esm",

src/caching-compiler.js

+71-72
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ function init() {
4949
return compile(code, options)
5050
},
5151
from(entry) {
52-
const { cache, cachePath } = entry.package
52+
const pkg = entry.package
53+
const { cache } = pkg
5354
const { cacheName } = entry
54-
const { map } = cache
55-
const meta = map[cacheName]
55+
const meta = cache.meta.get(cacheName)
5656

5757
if (meta === void 0) {
5858
return null
@@ -88,7 +88,7 @@ function init() {
8888
let filename = meta[6]
8989

9090
if (filename) {
91-
filename = resolve(cachePath, filename)
91+
filename = resolve(pkg.cachePath, filename)
9292
}
9393

9494
result.filename = filename
@@ -113,7 +113,7 @@ function init() {
113113
}
114114

115115
entry.compileData = result
116-
cache.compile[cacheName] = result
116+
cache.compile.set(cacheName, result)
117117

118118
return result
119119
}
@@ -143,11 +143,15 @@ function init() {
143143

144144
const { pendingWrites } = shared
145145

146-
if (! Reflect.has(pendingWrites, cachePath)) {
147-
pendingWrites[cachePath] = { __proto__: null }
146+
let compileDatas = pendingWrites.get(cachePath)
147+
148+
if (compileDatas === void 0) {
149+
compileDatas = new Map
150+
pendingWrites.set(cachePath, compileDatas)
148151
}
149152

150-
pendingWrites[cachePath][cacheName] = result
153+
compileDatas.set(cacheName, result)
154+
151155
return result
152156
}
153157

@@ -157,12 +161,11 @@ function init() {
157161
const { pendingScripts, pendingWrites } = shared
158162
const { dir } = shared.package
159163

160-
for (const cachePath in dir) {
164+
dir.forEach((cache, cachePath) => {
161165
if (cachePath === "") {
162-
continue
166+
return
163167
}
164168

165-
const cache = dir[cachePath]
166169
const noCacheDir = ! mkdirp(cachePath)
167170

168171
let { dirty } = cache
@@ -179,39 +182,37 @@ function init() {
179182

180183
if (dirty ||
181184
noCacheDir) {
182-
Reflect.deleteProperty(dir, cachePath)
183-
Reflect.deleteProperty(pendingScripts, cachePath)
184-
Reflect.deleteProperty(pendingWrites, cachePath)
185+
dir.delete(cachePath)
186+
pendingScripts.delete(cachePath)
187+
pendingWrites.delete(cachePath)
185188
}
186189

187190
if (noCacheDir) {
188-
continue
191+
return
189192
}
190193

191194
if (dirty) {
192195
writeMarker(cachePath + sep + ".dirty")
193196
removeFile(cachePath + sep + ".data.blob")
194197
removeFile(cachePath + sep + ".data.json")
195198

196-
for (const cacheName in cache.compile) {
199+
cache.compile.forEach((compileData, cacheName) => {
197200
removeFile(cachePath + sep + cacheName)
198-
}
201+
})
199202
}
200-
}
203+
})
201204

202-
const pendingScriptDatas = { __proto__: null }
205+
const pendingScriptDatas = new Map
203206
const useCreateCachedData = shared.support.createCachedData
204207

205-
for (const cachePath in pendingScripts) {
206-
const cache = dir[cachePath]
208+
pendingScripts.forEach((scripts, cachePath) => {
209+
const cache = dir.get(cachePath)
207210
const compileDatas = cache.compile
208-
const { map } = cache
209-
const scripts = pendingScripts[cachePath]
211+
const metas = cache.meta
210212

211-
for (const cacheName in scripts) {
212-
const compileData = compileDatas[cacheName]
213-
const cachedData = compileData ? compileData.scriptData : null
214-
const script = scripts[cacheName]
213+
scripts.forEach((script, cacheName) => {
214+
const compileData = compileDatas.get(cacheName)
215+
const cachedData = compileData != null ? compileData.scriptData : null
215216

216217
let scriptData
217218
let changed = false
@@ -234,7 +235,7 @@ function init() {
234235
script.cachedDataRejected) {
235236
changed = true
236237

237-
const meta = map[cacheName]
238+
const meta = metas.get(cacheName)
238239

239240
if (meta !== void 0) {
240241
meta[0] = -1
@@ -248,33 +249,35 @@ function init() {
248249

249250
if (changed &&
250251
cacheName !== "") {
251-
if (! Reflect.has(pendingScriptDatas, cachePath)) {
252-
pendingScriptDatas[cachePath] = { __proto__: null }
252+
let scriptDatas = pendingScriptDatas.get(cachePath)
253+
254+
if (scriptDatas === void 0) {
255+
scriptDatas = new Map
256+
pendingScriptDatas.set(cachePath, scriptDatas)
253257
}
254258

255-
pendingScriptDatas[cachePath][cacheName] = scriptData
259+
scriptDatas.set(cacheName, scriptData)
256260
}
257-
}
258-
}
261+
})
262+
})
259263

260-
for (const cachePath in pendingScriptDatas) {
261-
const cache = dir[cachePath]
262-
const { buffer, map } = cache
264+
pendingScriptDatas.forEach((scriptDatas, cachePath) => {
265+
const cache = dir.get(cachePath)
263266
const compileDatas = cache.compile
264-
const scriptDatas = pendingScriptDatas[cachePath]
267+
const metas = cache.meta
265268

266-
for (const cacheName in scriptDatas) {
267-
let meta = map[cacheName]
269+
scriptDatas.forEach((scriptData, cacheName) => {
270+
let meta = metas.get(cacheName)
268271

269272
if (meta !== void 0) {
270-
continue
273+
return
271274
}
272275

273276
meta = [-1, -1]
274277

275-
const compileData = compileDatas[cacheName]
278+
const compileData = compileDatas.get(cacheName)
276279

277-
if (compileData) {
280+
if (compileData != null) {
278281
const {
279282
filename,
280283
firstAwaitOutsideFunction,
@@ -315,27 +318,23 @@ function init() {
315318
}
316319
}
317320

318-
map[cacheName] = meta
319-
}
321+
metas.set(cacheName, meta)
322+
})
320323

324+
const { buffer } = cache
321325
const buffers = []
326+
const jsonMeta = {}
322327

323328
let offset = 0
324329

325-
for (const cacheName in map) {
326-
const meta = map[cacheName]
327-
328-
if (meta === void 0) {
329-
continue
330-
}
331-
332-
const compileData = compileDatas[cacheName]
333-
const [offsetStart, offsetEnd] = meta
334-
335-
let scriptData = scriptDatas[cacheName]
330+
metas.forEach((meta, cacheName) => {
331+
let scriptData = scriptDatas.get(cacheName)
336332

337333
if (scriptData === void 0) {
338-
if (compileData) {
334+
const compileData = compileDatas.get(cacheName)
335+
const [offsetStart, offsetEnd] = meta
336+
337+
if (compileData != null) {
339338
scriptData = compileData.scriptData
340339
} else if (offsetStart !== -1 &&
341340
offsetEnd !== -1) {
@@ -348,40 +347,40 @@ function init() {
348347
meta[1] = offset += scriptData.length
349348
buffers.push(scriptData)
350349
}
351-
}
350+
351+
jsonMeta[cacheName] = meta
352+
})
352353

353354
writeFile(cachePath + sep + ".data.blob", GenericBuffer.concat(buffers))
354355
writeFile(cachePath + sep + ".data.json", JSON.stringify({
355-
map,
356+
meta: jsonMeta,
356357
version: PACKAGE_VERSION
357358
}))
358-
}
359-
360-
for (const cachePath in pendingWrites) {
361-
const contents = pendingWrites[cachePath]
362-
363-
for (const cacheName in contents) {
364-
const { code } = contents[cacheName]
359+
})
365360

361+
pendingWrites.forEach((compileDatas, cachePath) => {
362+
compileDatas.forEach(({ code }, cacheName) => {
366363
if (writeFile(cachePath + sep + cacheName, code)) {
367364
removeExpired(cachePath, cacheName)
368365
}
369-
}
370-
}
366+
})
367+
})
371368
}
372369

373370
function removeExpired(cachePath, cacheName) {
374-
const cache = shared.package.dir[cachePath]
371+
const cache = shared.package.dir.get(cachePath)
372+
const compileDatas = cache.compile
373+
const metas = cache.meta
375374
const pathHash = getCachePathHash(cacheName)
376375

377-
for (const otherCacheName in cache) {
376+
compileDatas.forEach((compileData, otherCacheName) => {
378377
if (otherCacheName !== cacheName &&
379378
otherCacheName.startsWith(pathHash)) {
380-
Reflect.deleteProperty(cache.compile, otherCacheName)
381-
Reflect.deleteProperty(cache.map, otherCacheName)
379+
compileDatas.delete(otherCacheName)
380+
metas.delete(otherCacheName)
382381
removeFile(cachePath + sep + otherCacheName)
383382
}
384-
}
383+
})
385384
}
386385

387386
function toCompileOptions(options = {}) {

src/hook/vm.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ function hook(vm) {
7171
const compileDatas = entry.package.cache.compile
7272
const { runtimeName } = entry
7373

74-
let compileData = Reflect.has(compileDatas, cacheName)
75-
? compileDatas[cacheName]
76-
: null
74+
let compileData = compileDatas.get(cacheName)
75+
76+
if (compileData === void 0) {
77+
compileData = null
78+
}
7779

7880
if (compileData === null) {
7981
const compilerOptions = {
@@ -86,7 +88,7 @@ function hook(vm) {
8688
}
8789

8890
compileData = tryWrapper(CachingCompiler.compile, [content, compilerOptions], content)
89-
compileDatas[cacheName] = compileData
91+
compileDatas.set(cacheName, compileData)
9092
} else if (compileData.scriptData !== null &&
9193
scriptOptions.produceCachedData &&
9294
! Reflect.has(scriptOptions, "cachedData")) {

src/module/internal/compile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function compile(caller, entry, content, filename, fallback) {
113113
compileData.scriptData = scriptData
114114

115115
entry.compileData = compileData
116-
pkg.cache.compile[cacheName] = compileData
116+
pkg.cache.compile.set(cacheName, compileData)
117117

118118
if (compileData.sourceType === SOURCE_TYPE_MODULE) {
119119
entry.type = TYPE_ESM

src/module/proto/compile.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,16 @@ const compile = maskFunction(function (content, filename) {
127127
const script = new realVM.Script(Module.wrap(preparedContent), scriptOptions)
128128

129129
if (cachePath !== "") {
130-
const { cacheName } = entry
131130
const { pendingScripts } = shared
132131

133-
if (! Reflect.has(pendingScripts, cachePath)) {
134-
pendingScripts[cachePath] = { __proto__: null }
132+
let scripts = pendingScripts.get(cachePath)
133+
134+
if (scripts === void 0) {
135+
scripts = new Map
136+
pendingScripts.set(cachePath, scripts)
135137
}
136138

137-
pendingScripts[cachePath][cacheName] = script
139+
scripts.set(entry.cacheName, script)
138140
}
139141

140142
if (useRunInContext === void 0) {

0 commit comments

Comments
 (0)