Skip to content

Commit fd7a83f

Browse files
authored
refactor: dispatch async work queue in shared memory (#201)
1 parent cd45112 commit fd7a83f

11 files changed

Lines changed: 541 additions & 185 deletions

File tree

packages/bench/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"rebuild": "emcmake cmake -DCMAKE_BUILD_TYPE=Release -H. -B.build && cmake --build .build"
77
},
88
"devDependencies": {
9-
"node-addon-api": "^7.0.0",
9+
"node-addon-api": "^8.6.0",
1010
"benchmark": "^2.1.4"
1111
}
1212
}

packages/core/src/emnapi/index.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ export declare interface NapiModule {
3939
}
4040

4141
init (options: InitOptions): any
42-
initWorker (arg: number): void
43-
executeAsyncWork (work: number): void
42+
initWorker (arg: number, func: [number, number]): void
4443
postMessage?: (msg: any) => any
4544

4645
waitThreadStart: boolean | number

packages/core/src/worker.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ export class MessageHandler extends ThreadMessageHandler {
6464
try {
6565
if (type === 'async-worker-init') {
6666
this.handleAfterLoad(e, () => {
67-
this.napiModule!.initWorker(payload.arg)
68-
})
69-
} else if (type === 'async-work-execute') {
70-
this.handleAfterLoad(e, () => {
71-
this.napiModule!.executeAsyncWork(payload.work)
67+
this.napiModule!.initWorker(payload.arg, payload.func)
7268
})
7369
}
7470
} catch (err) {

packages/emnapi/script/build.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,9 @@ async function buildNonEmscriptenPlugin (tsconfigPath, input, output, destructPl
290290
${parsedCode}
291291
return {
292292
importObject: (original) => {
293-
${exportModules.map(modName => {
293+
${typeof exportModules === 'function'
294+
? exportModules(modVar)
295+
: exportModules.map(modName => {
294296
return ` Object.assign(original.${modName}, ${modVar})`
295297
})}
296298
}
@@ -354,7 +356,16 @@ async function build () {
354356
path.join(__dirname, '../src/core/async-work.ts'),
355357
path.join(outputDir, 'async-work.js'),
356358
['emnapiCtx', 'emnapiNodeBinding', 'emnapiAsyncWorkPoolSize'],
357-
['napi']
359+
function (modVar) {
360+
return `
361+
Object.keys(${modVar}).forEach(key => {
362+
if (key.startsWith('napi_') || key.startsWith('node_api_')) {
363+
original.napi[key] = ${modVar}[key]
364+
} else {
365+
original.env[key] = ${modVar}[key]
366+
}
367+
})`
368+
}
358369
),
359370
buildNonEmscriptenPlugin(
360371
coreTsconfigPath,

0 commit comments

Comments
 (0)