Skip to content

Commit 30ebcaf

Browse files
alexander-akaitclaude
authored andcommitted
perf: load the scopes encoder only when the option asks for it
Requiring it from the two map assembly helpers put the encoder and its lookup table in the module graph of every caller, which the memory benchmarks measured as a constant cost on sources that never emit the field.
1 parent b0db193 commit 30ebcaf

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎lib/helpers/getFromStreamChunks.js‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"use strict";
77

88
const { createMappingsWriter } = require("./createMappingsSerializer");
9-
const { createScopesWriter } = require("./scopes");
109

1110
/** @typedef {import("../Source").RawSourceMap} RawSourceMap */
1211
/** @typedef {import("../Source").SourceAndMap} SourceAndMap */
@@ -15,6 +14,20 @@ const { createScopesWriter } = require("./scopes");
1514

1615
/** @typedef {{ streamChunks: StreamChunksFunction }} SourceLikeWithStreamChunks */
1716

17+
/** @type {typeof import("./scopes") | undefined} */
18+
let scopesHelpers;
19+
20+
/**
21+
* Loads the scopes helpers on first use. They are only reachable through the
22+
* `scopes` option, so requiring them eagerly would put the encoder in the
23+
* module graph of every caller that never asks for the field.
24+
* @returns {ReturnType<typeof import("./scopes").createScopesWriter>} writer
25+
*/
26+
const createScopesWriter = () => {
27+
if (scopesHelpers === undefined) scopesHelpers = require("./scopes");
28+
return scopesHelpers.createScopesWriter();
29+
};
30+
1831
/**
1932
* Assign `value` at index `i` of `arr`, padding earlier slots with `null`.
2033
* Hoisted helper so `getMap` and `getSourceAndMap` share one

‎lib/helpers/streamAndGetSourceAndMap.js‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"use strict";
77

88
const { createMappingsWriter } = require("./createMappingsSerializer");
9-
const { createScopesWriter } = require("./scopes");
109
const streamChunks = require("./streamChunks");
1110

1211
/** @typedef {import("../Source").RawSourceMap} RawSourceMap */
@@ -17,6 +16,20 @@ const streamChunks = require("./streamChunks");
1716
/** @typedef {import("./streamChunks").Options} Options */
1817
/** @typedef {import("./streamChunks").SourceMaybeWithStreamChunksFunction} SourceMaybeWithStreamChunksFunction */
1918

19+
/** @type {typeof import("./scopes") | undefined} */
20+
let scopesHelpers;
21+
22+
/**
23+
* Loads the scopes helpers on first use. They are only reachable through the
24+
* `scopes` option, so requiring them eagerly would put the encoder in the
25+
* module graph of every caller that never asks for the field.
26+
* @returns {ReturnType<typeof import("./scopes").createScopesWriter>} writer
27+
*/
28+
const createScopesWriter = () => {
29+
if (scopesHelpers === undefined) scopesHelpers = require("./scopes");
30+
return scopesHelpers.createScopesWriter();
31+
};
32+
2033
/**
2134
* @param {SourceMaybeWithStreamChunksFunction} inputSource input source
2235
* @param {Options} options options

0 commit comments

Comments
 (0)