-
Notifications
You must be signed in to change notification settings - Fork 149
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
styfle
wants to merge
3
commits into
main
Choose a base branch
from
fix-issue-314
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
return JSON.stringify(obj, replacer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
|
||
|
@@ -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), | ||
}, | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
1 change: 1 addition & 0 deletions
1
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = { file: 'file' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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