-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move content hashing to a child process
I noticed in profiles that this was actually a bottleneck for Jest tests (in #214) when running as a cluster. I wanted to use a worker thread for it, but it looks like there's an issue in vscode preventing that[1] for the moment. This cuts the time-to-first-breakpoint in half for the jest tests, which is fairly nice. The child process is killed after 30 seconds of inactivity. I may do an algorithmic optimization pass on the hash in the future. In particular, Node/V8 now has native bigint support, which is almost certainly faster than the `long` library. 1. microsoft/vscode#88386
- Loading branch information
1 parent
35d9a5d
commit 951c8f6
Showing
7 changed files
with
122 additions
and
14 deletions.
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
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,53 @@ | ||
/*--------------------------------------------------------- | ||
* Copyright (C) Microsoft Corporation. All rights reserved. | ||
*--------------------------------------------------------*/ | ||
|
||
import { ChildProcess, fork } from 'child_process'; | ||
import { join } from 'path'; | ||
import { HashRequest, HashResponse } from './hash'; | ||
import { debounce } from '../objUtils'; | ||
|
||
let instance: ChildProcess | undefined; | ||
let messageId = 0; | ||
|
||
const cleanup = debounce(30 * 1000, () => { | ||
instance?.kill(); | ||
instance = undefined; | ||
}); | ||
|
||
const create = () => { | ||
if (instance) { | ||
return instance; | ||
} | ||
|
||
instance = fork(join(__dirname, 'hash.js'), [], { env: {}, silent: true }); | ||
instance.setMaxListeners(Infinity); | ||
return instance; | ||
}; | ||
|
||
const send = (req: HashRequest): Promise<string | undefined> => { | ||
const cp = create(); | ||
cleanup(); | ||
|
||
return new Promise(resolve => { | ||
const listener = (res: HashResponse) => { | ||
if (res.id === req.id) { | ||
resolve(res.hash); | ||
cp.removeListener('message', listener); | ||
} | ||
}; | ||
|
||
cp.addListener('message', listener); | ||
cp.send(req); | ||
}); | ||
}; | ||
|
||
/** | ||
* Gets the Chrome content hash of script contents. | ||
*/ | ||
export const hashBytes = (data: string | Buffer) => send({ data, id: messageId++ }); | ||
|
||
/** | ||
* Gets the Chrome content hash of a file. | ||
*/ | ||
export const hashFile = (file: string) => send({ file, id: messageId++ }); |
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
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,4 @@ | ||
1d9f277f134f31935a286ff810acdf571af3498e | ||
1d9f277f134f31935a286ff810acdf571af3498e | ||
1d9f277f134f31935a286ff810acdf571af3498e | ||
1d9f277f134f31935a286ff810acdf571af3498e |
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