From 1c132fec1844432e7bfea1bd5d384a9880b91dbb Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 16 Aug 2023 18:34:42 +0200 Subject: [PATCH] perf: remove unneeded branching in message handling code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- benchmarks/internal/cluster-worker.mjs | 4 ++-- benchmarks/internal/thread-worker.mjs | 2 +- examples/javascript/dynamicExample.js | 8 ++++---- examples/javascript/fixedExample.js | 6 +++--- examples/javascript/multiFunctionWorker.js | 4 ++-- examples/javascript/yourWorker.js | 2 +- src/worker/abstract-worker.ts | 3 --- 7 files changed, 13 insertions(+), 16 deletions(-) diff --git a/benchmarks/internal/cluster-worker.mjs b/benchmarks/internal/cluster-worker.mjs index bc354839..fee9d3f8 100644 --- a/benchmarks/internal/cluster-worker.mjs +++ b/benchmarks/internal/cluster-worker.mjs @@ -1,4 +1,4 @@ -import { isMaster } from 'cluster' +import { isPrimary } from 'cluster' import { ClusterWorker } from '../../lib/index.mjs' import { executeTaskFunction } from '../benchmarks-utils.mjs' import { TaskFunctions } from '../benchmarks-types.mjs' @@ -9,7 +9,7 @@ const taskFunction = (data) => { data = data || {} data.function = data.function || TaskFunctions.jsonIntegerSerialization const res = executeTaskFunction(data) - debug === true && console.debug('This is the main thread ' + isMaster) + debug === true && console.debug(`This is the main thread ${isPrimary}`) return res } diff --git a/benchmarks/internal/thread-worker.mjs b/benchmarks/internal/thread-worker.mjs index 855ba622..f88cf076 100644 --- a/benchmarks/internal/thread-worker.mjs +++ b/benchmarks/internal/thread-worker.mjs @@ -9,7 +9,7 @@ const taskFunction = (data) => { data = data || {} data.function = data.function || TaskFunctions.jsonIntegerSerialization const res = executeTaskFunction(data) - debug === true && console.debug('This is the main thread ' + isMainThread) + debug === true && console.debug(`This is the main thread ${isMainThread}`) return res } diff --git a/examples/javascript/dynamicExample.js b/examples/javascript/dynamicExample.js index 21fe9050..34281709 100644 --- a/examples/javascript/dynamicExample.js +++ b/examples/javascript/dynamicExample.js @@ -30,10 +30,10 @@ for (let i = 1; i <= iterations; i++) { .then(() => { resolved++ if (resolved === iterations) { - console.info('Time taken is ' + (performance.now() - start)) - console.info('The pool was full for ' + poolFull + ' times') - console.info('The pool was ready for ' + poolReady + ' times') - return console.info('The pool was busy for ' + poolBusy + ' times') + console.info(`Time taken is ${performance.now() - start}`) + console.info(`The pool was full for ${poolFull} times`) + console.info(`The pool was ready for ${poolReady} times`) + return console.info(`The pool was busy for ${poolBusy} times`) } return null }) diff --git a/examples/javascript/fixedExample.js b/examples/javascript/fixedExample.js index 85f159d3..99d1a5e1 100644 --- a/examples/javascript/fixedExample.js +++ b/examples/javascript/fixedExample.js @@ -23,9 +23,9 @@ for (let i = 1; i <= iterations; i++) { .then(() => { resolved++ if (resolved === iterations) { - console.info('Time taken is ' + (performance.now() - start)) - console.info('The pool was ready for ' + poolReady + ' times') - return console.info('The pool was busy for ' + poolBusy + ' times') + console.info(`Time taken is ${performance.now() - start}`) + console.info(`The pool was ready for ${poolReady} times`) + return console.info(`The pool was busy for ${poolBusy} times`) } return null }) diff --git a/examples/javascript/multiFunctionWorker.js b/examples/javascript/multiFunctionWorker.js index 406afa69..20c41695 100644 --- a/examples/javascript/multiFunctionWorker.js +++ b/examples/javascript/multiFunctionWorker.js @@ -3,12 +3,12 @@ const { ThreadWorker } = require('poolifier') function fn0 (data) { console.info('Executing function 0') - return { data: 'fn0 your input text was' + data.text } + return { data: `fn0 your input text was ${data.text}` } } function fn1 (data) { console.info('Executing function 1') - return { data: 'fn1 your input text was' + data.text } + return { data: `fn1 your input text was ${data.text}` } } module.exports = new ThreadWorker({ fn0, fn1 }) diff --git a/examples/javascript/yourWorker.js b/examples/javascript/yourWorker.js index b2df8d19..c21f911d 100644 --- a/examples/javascript/yourWorker.js +++ b/examples/javascript/yourWorker.js @@ -11,7 +11,7 @@ function yourFunction () { } JSON.stringify(o) } - debug === true && console.info('This is the main thread ' + isMainThread) + debug === true && console.info(`This is the main thread ${isMainThread}`) return { ok: 1 } } diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index b475bd98..98b32fe4 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -318,9 +318,6 @@ export abstract class AbstractWorker< * @param message - The received message. */ protected messageListener (message: MessageValue): void { - if (this.isMain) { - throw new Error('Cannot handle message to worker in main worker') - } this.checkMessageWorkerId(message) if (message.statistics != null) { // Statistics message received -- 2.34.1