Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: concat performance #422

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/utils/safe-stringify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const replacer = (key: string, value: unknown) => {
if (typeof value === 'object') {
if (key === 'globalThis') {
return '[globalThis]';
}
if (key === 'global') {
return '[global]';
}
if (key === 'GLOBAL') {
return '[GLOBAL]';
}
if (key === 'process') {
return '[process]';
}
if (key === 'win32') {
// path.win32
return '[win32]';
}
if (key === 'posix') {
// path.posix
return '[posix]';
}
}
if (typeof value === 'function') {
return '[Function]';
}
if (typeof value === 'bigint') {
return '[bigint]';
}
return value;
};
export function safeStringify(obj: unknown) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a more comprehensive stringify to handle all possible inputs

return JSON.stringify(obj, replacer);
}
27 changes: 18 additions & 9 deletions src/utils/static-eval.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
import { safeStringify } from './safe-stringify';
import { EvaluatedValue, StaticValue, ConditionalValue, Node } from './types';
import { URL } from 'url';
type Walk = (node: Node) => EvaluatedValue;
type Walk = (node: Node) => Promise<EvaluatedValue>;
type State = { computeBranches: boolean; vars: Record<string, any> };

const walkCache = new Map<string, EvaluatedValue>();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably use a LRU cache to avoid running out of memory. Right now, the number of entries is unbounded.


export async function evaluate(
ast: Node,
vars = {},
computeBranches = true,
ast: Readonly<Node>,
vars: Readonly<Record<string, string>> = {},
computeBranches: Readonly<boolean> = true,
): Promise<EvaluatedValue> {
const state: State = {
computeBranches,
vars,
};
const stateCacheKey = safeStringify(state);
return walk(ast);

// walk returns:
// 1. Single known value: { value: value }
// 2. Conditional value: { test, then, else }
// 3. Unknown value: undefined
function walk(node: Node) {
async function walk(node: Node) {
const walkCacheKey = stateCacheKey + safeStringify(node);
let result = walkCache.get(walkCacheKey);
if (result) {
return result;
}
const visitor = visitors[node.type];
if (visitor) {
return visitor.call(state, node, walk);
result = await visitor.call(state, node, walk);
}
return undefined;
walkCache.set(walkCacheKey, result);
return result;
}
}

Expand Down Expand Up @@ -359,7 +368,7 @@ const visitors: Record<
if (typeof obj.value === 'string' && node.property.name === 'concat') {
return {
value: {
[FUNCTION]: (...args: string[]) => obj.value.concat(args),
[FUNCTION]: (...args: string[]) => obj.value.concat(...args),
},
};
}
Expand Down
6 changes: 6 additions & 0 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const stat = gracefulFS.promises.stat;
const readlink = gracefulFS.promises.readlink;
const readFile = gracefulFS.promises.readFile;

jest.setTimeout(5_000);

global._unit = true;

const nodeGypTests = [
Expand Down Expand Up @@ -129,6 +131,7 @@ for (const { testName, isRoot } of unitTests) {
inputFileNames.push('input-2.js', 'input-3.js', 'input-4.js');
}

const startTime = Date.now();
const { fileList, reasons } = await nodeFileTrace(
inputFileNames.map((file) => join(unitPath, file)),
{
Expand All @@ -155,6 +158,9 @@ for (const { testName, isRoot } of unitTests) {
},
);

const totalTime = Date.now() - startTime;
expect(totalTime).toBeLessThan(1000);

const normalizeFilesRoot = (f) =>
(isRoot ? relative(join('./', __dirname, '..'), f) : f).replace(
/\\/g,
Expand Down
24 changes: 24 additions & 0 deletions test/unit/string-concat-chain/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var str = './lib/'
.concat('1')
.concat('_', '2')
.concat('_', '3')
.concat('_', '4')
.concat('_', '5')
.concat('_', '6')
.concat('_', '7')
.concat('_', '8')
.concat('_', '9')
.concat('_', '10')
.concat('_', '11')
.concat('_', '12')
.concat('_', '13')
.concat('_', '14')
.concat('_', '15')
.concat('_', '16')
.concat('_', '17')
.concat('_', '18')
.concat('_', '19')
.concat('_', '20')
.concat('.js');

require(str);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { file: 'file' }
5 changes: 5 additions & 0 deletions test/unit/string-concat-chain/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"package.json",
"test/unit/string-concat-chain/input.js",
"test/unit/string-concat-chain/lib/1_2_3_4_5_6_7_8_9_10_11_12_13_14_15_16_17_18_19_20.js"
]
Loading