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

feat(atomics): add environment variable to disable atomics #163

Merged
merged 3 commits into from
Dec 9, 2021
Merged
Changes from all 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -303,7 +303,8 @@ This class extends [`EventEmitter`][] from Node.js.
to the task. Keep in mind that Worker threads are generally not built for
handling I/O in parallel.
* `useAtomics`: (`boolean`) Use the [`Atomics`][] API for faster communication
between threads. This is on by default.
between threads. This is on by default. You can disable `Atomics` globally by
setting the environment variable `PISCINA_DISABLE_ATOMICS` to `1`.
* `resourceLimits`: (`object`) See [Node.js new Worker options][]
* `maxOldGenerationSizeMb`: (`number`) The maximum size of each worker threads
main heap in MB.
8 changes: 4 additions & 4 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ commonState.isWorkerThread = true;
commonState.workerData = workerData;

const handlerCache : Map<string, Function> = new Map();
let useAtomics : boolean = true;
let useAtomics : boolean = process.env.PISCINA_DISABLE_ATOMICS !== '1';

// Get `import(x)` as a function that isn't transpiled to `require(x)` by
// TypeScript for dual ESM/CJS support.
@@ -26,8 +26,8 @@ let useAtomics : boolean = true;
let importESMCached : (specifier : string) => Promise<any> | undefined;
function getImportESM () {
if (importESMCached === undefined) {
// eslint-disable-next-line no-eval
importESMCached = eval('(specifier) => import(specifier)');
// eslint-disable-next-line no-new-func
importESMCached = new Function('specifier', 'return import(specifier)') as typeof importESMCached;
}
return importESMCached;
}
@@ -74,7 +74,7 @@ async function getHandler (filename : string, name : string) : Promise<Function
// communication using Atomics, and the name of the default filename for tasks
// (so we can pre-load and cache the handler).
parentPort!.on('message', (message : StartupMessage) => {
useAtomics = message.useAtomics;
useAtomics = process.env.PISCINA_DISABLE_ATOMICS === '1' ? false : message.useAtomics;
const { port, sharedBuffer, filename, name, niceIncrement } = message;
(async function () {
try {