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
Performance: Cache regular expression instead of creating anew for every file in ScriptTransformer. #8235
Merged
scotthovestadt
merged 5 commits into
jestjs:master
from
scotthovestadt:optimize-script-transformer
Mar 29, 2019
Merged
Performance: Cache regular expression instead of creating anew for every file in ScriptTransformer. #8235
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6f8e9b2
Cache regular expression instead of creating anew for every file in s…
scotthovestadt 3e9741e
Update CHANGELOG.md
scotthovestadt 28a1a99
Minor optimization on hot path.
scotthovestadt 6107b50
Merge branch 'master' into optimize-script-transformer
scotthovestadt e0d3f8c
Merge branch 'master' into optimize-script-transformer
scotthovestadt File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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 |
---|---|---|
|
@@ -31,7 +31,8 @@ import enhanceUnexpectedTokenMessage from './enhanceUnexpectedTokenMessage'; | |
|
||
type ProjectCache = { | ||
configString: string; | ||
ignorePatternsRegExp: RegExp | null; | ||
ignorePatternsRegExp?: RegExp; | ||
transformRegExp?: Array<[RegExp, string]>; | ||
transformedFiles: Map<string, TransformResult>; | ||
}; | ||
|
||
|
@@ -64,7 +65,8 @@ export default class ScriptTransformer { | |
if (!projectCache) { | ||
projectCache = { | ||
configString: stableStringify(this._config), | ||
ignorePatternsRegExp: calcIgnorePatternRegexp(this._config), | ||
ignorePatternsRegExp: calcIgnorePatternRegExp(this._config), | ||
transformRegExp: calcTransformRegExp(this._config), | ||
transformedFiles: new Map(), | ||
}; | ||
|
||
|
@@ -131,12 +133,18 @@ export default class ScriptTransformer { | |
} | ||
|
||
private _getTransformPath(filename: Config.Path) { | ||
for (let i = 0; i < this._config.transform.length; i++) { | ||
if (new RegExp(this._config.transform[i][0]).test(filename)) { | ||
return this._config.transform[i][1]; | ||
const transformRegExp = this._cache.transformRegExp; | ||
if (!transformRegExp) { | ||
return undefined; | ||
} | ||
|
||
for (let i = 0; i < transformRegExp.length; i++) { | ||
if (transformRegExp[i][0].test(filename)) { | ||
return transformRegExp[i][1]; | ||
} | ||
} | ||
return null; | ||
|
||
return undefined; | ||
} | ||
|
||
private _getTransformer(filename: Config.Path) { | ||
|
@@ -371,21 +379,19 @@ export default class ScriptTransformer { | |
options: Options, | ||
fileSource?: string, | ||
): TransformResult { | ||
let scriptCacheKey = null; | ||
let scriptCacheKey = undefined; | ||
let instrument = false; | ||
let result: TransformResult | undefined; | ||
|
||
if (!options.isCoreModule) { | ||
instrument = shouldInstrument(filename, options, this._config); | ||
scriptCacheKey = getScriptCacheKey(filename, instrument); | ||
result = this._cache.transformedFiles.get(scriptCacheKey); | ||
} | ||
|
||
if (result) { | ||
return result; | ||
const result = this._cache.transformedFiles.get(scriptCacheKey); | ||
if (result) { | ||
return result; | ||
} | ||
} | ||
|
||
result = this._transformAndBuildScript( | ||
const result = this._transformAndBuildScript( | ||
filename, | ||
options, | ||
instrument, | ||
|
@@ -539,19 +545,33 @@ const getScriptCacheKey = (filename: Config.Path, instrument: boolean) => { | |
return filename + '_' + mtime.getTime() + (instrument ? '_instrumented' : ''); | ||
}; | ||
|
||
const calcIgnorePatternRegexp = ( | ||
config: Config.ProjectConfig, | ||
): RegExp | null => { | ||
const calcIgnorePatternRegExp = (config: Config.ProjectConfig) => { | ||
if ( | ||
!config.transformIgnorePatterns || | ||
config.transformIgnorePatterns.length === 0 | ||
) { | ||
return null; | ||
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. |
||
return; | ||
} | ||
|
||
return new RegExp(config.transformIgnorePatterns.join('|')); | ||
}; | ||
|
||
const calcTransformRegExp = (config: Config.ProjectConfig) => { | ||
if (!config.transform.length) { | ||
return; | ||
} | ||
|
||
const transformRegexp: Array<[RegExp, string]> = []; | ||
for (let i = 0; i < config.transform.length; i++) { | ||
transformRegexp.push([ | ||
new RegExp(config.transform[i][0]), | ||
config.transform[i][1], | ||
]); | ||
} | ||
|
||
return transformRegexp; | ||
}; | ||
|
||
const wrap = (content: string, ...extras: Array<string>) => { | ||
const globals = new Set([ | ||
'module', | ||
|
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.
When can this be falsy?
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.
When the array passed from config with transforms is 0-length.
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.
Ah,
transform: {}
? GotchaThere 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.
calcTransformRegExp
returnsundefined
ifconfig.transform.length === 0
.I try not to return empty arrays to loop through when the function should actually be telling you "nothing to do here". I personally find it makes the code more readable and sometimes make optimizations in the future possible.