Skip to content

Commit

Permalink
perf: use sha1 instead of sha256 for hashing (#13421)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchhentgesspotify authored Oct 15, 2022
1 parent 55c5536 commit 1f6c4e9
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### Performance

- `[*]` Use sha1 instead of sha256 for hashing [#13421](https://github.com/facebook/jest/pull/13421)

## 29.2.0

### Features
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-jest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function getCacheKeyFromConfig(

const configPath = [babelOptions.config ?? '', babelOptions.babelrc ?? ''];

return createHash('sha256')
return createHash('sha1')
.update(THIS_FILE)
.update('\0', 'utf8')
.update(JSON.stringify(babelOptions.options))
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-circus/src/__mocks__/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface Result extends ExecaSyncReturnValue {
}

export const runTest = (source: string) => {
const filename = createHash('sha256')
const filename = createHash('sha1')
.update(source)
.digest('hex')
.substring(0, 32);
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/__tests__/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ afterEach(() => {

it('picks an id based on the rootDir', async () => {
const rootDir = '/root/path/foo';
const expected = createHash('sha256')
const expected = createHash('sha1')
.update('/root/path/foo')
.update(String(Infinity))
.digest('hex')
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ const normalizeMissingOptions = (
projectIndex: number,
): Config.InitialOptionsWithRootDir => {
if (!options.id) {
options.id = createHash('sha256')
options.id = createHash('sha1')
.update(options.rootDir)
// In case we load config from some path that has the same root dir
.update(configPath || '')
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-create-cache-key-function/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function getGlobalCacheKey(files: Array<string>, values: Array<string>) {
]
.reduce(
(hash, chunk) => hash.update('\0', 'utf8').update(chunk || ''),
createHash('sha256'),
createHash('sha1'),
)
.digest('hex')
.substring(0, 32);
Expand All @@ -62,7 +62,7 @@ function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction {
const inferredOptions = options || configString;
const {config, instrument} = inferredOptions;

return createHash('sha256')
return createHash('sha1')
.update(globalCacheKey)
.update('\0', 'utf8')
.update(sourceText)
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-haste-map/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ class HasteMap extends EventEmitter implements IHasteMap {
}

private async setupCachePath(options: Options): Promise<void> {
const rootDirHash = createHash('sha256')
const rootDirHash = createHash('sha1')
.update(options.rootDir)
.digest('hex')
.substring(0, 32);
Expand Down Expand Up @@ -344,7 +344,7 @@ class HasteMap extends EventEmitter implements IHasteMap {
id: string,
...extra: Array<string>
): string {
const hash = createHash('sha256').update(extra.join(''));
const hash = createHash('sha1').update(extra.join(''));
return path.join(
tmpdir,
`${id.replace(/\W/g, '-')}-${hash.digest('hex').substring(0, 32)}`,
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ class ScriptTransformer {
transformerCacheKey: string | undefined,
): string {
if (transformerCacheKey != null) {
return createHash('sha256')
return createHash('sha1')
.update(transformerCacheKey)
.update(CACHE_VERSION)
.digest('hex')
.substring(0, 32);
}

return createHash('sha256')
return createHash('sha1')
.update(fileData)
.update(transformOptions.configString)
.update(transformOptions.instrument ? 'instrument' : '')
Expand Down Expand Up @@ -882,7 +882,7 @@ const stripShebang = (content: string) => {
* could get corrupted, out-of-sync, etc.
*/
function writeCodeCacheFile(cachePath: string, code: string) {
const checksum = createHash('sha256')
const checksum = createHash('sha1')
.update(code)
.digest('hex')
.substring(0, 32);
Expand All @@ -901,7 +901,7 @@ function readCodeCacheFile(cachePath: string): string | null {
return null;
}
const code = content.substring(33);
const checksum = createHash('sha256')
const checksum = createHash('sha1')
.update(code)
.digest('hex')
.substring(0, 32);
Expand Down

0 comments on commit 1f6c4e9

Please # to comment.