Skip to content

Commit 3c7ad6e

Browse files
committed
perf: reduce pool info property internal usage
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
1 parent 7c74c69 commit 3c7ad6e

2 files changed

Lines changed: 27 additions & 32 deletions

File tree

deno.json

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
"version": "0.5.10",
44
"exports": "./src/mod.ts",
55
"compilerOptions": {
6-
"lib": [
7-
"deno.worker"
8-
],
6+
"lib": ["deno.worker"],
97
"strict": true
108
},
119
"tasks": {
@@ -27,9 +25,7 @@
2725
"documentation": "deno doc ./src/mod.ts"
2826
},
2927
"test": {
30-
"include": [
31-
"./tests/**/*.test.mjs"
32-
]
28+
"include": ["./tests/**/*.test.mjs"]
3329
},
3430
"fmt": {
3531
"semiColons": false,
@@ -42,18 +38,8 @@
4238
"@std/testing": "jsr:@std/testing@^1.0.15"
4339
},
4440
"publish": {
45-
"include": [
46-
"LICENSE",
47-
"README.md",
48-
"deno.json",
49-
"src/**/*.ts"
50-
]
41+
"include": ["LICENSE", "README.md", "deno.json", "src/**/*.ts"]
5142
},
5243
"lock": false,
53-
"exclude": [
54-
"./coverage",
55-
"./dist/browser",
56-
"./dist/esm",
57-
"./npm"
58-
]
44+
"exclude": ["./coverage", "./dist/browser", "./dist/esm", "./npm"]
5945
}

src/pools/abstract-pool.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -358,18 +358,8 @@ export abstract class AbstractPool<
358358
: accumulator,
359359
0,
360360
),
361-
stealingWorkerNodes: this.workerNodes.reduce(
362-
(accumulator, _, workerNodeKey) =>
363-
this.isWorkerNodeStealing(workerNodeKey)
364-
? accumulator + 1
365-
: accumulator,
366-
0,
367-
),
368-
queuedTasks: this.workerNodes.reduce(
369-
(accumulator, workerNode) =>
370-
accumulator + workerNode.usage.tasks.queued,
371-
0,
372-
),
361+
stealingWorkerNodes: this.getStealingWorkerNodes(),
362+
queuedTasks: this.getQueuedTasks(),
373363
maxQueuedTasks: this.workerNodes.reduce(
374364
(accumulator, workerNode) =>
375365
accumulator + (workerNode.usage.tasks.maxQueued ?? 0),
@@ -852,6 +842,9 @@ export abstract class AbstractPool<
852842
message: MessageValue<Data>,
853843
): Promise<boolean> {
854844
const targetWorkerNodeKeys = [...this.workerNodes.keys()]
845+
if (targetWorkerNodeKeys.length === 0) {
846+
return true
847+
}
855848
const responsesReceived: MessageValue<Response>[] = []
856849
const taskFunctionOperationsListener = (
857850
message: MessageValue<Response>,
@@ -1002,6 +995,22 @@ export abstract class AbstractPool<
1002995
: new Error(`Task '${taskName}' id '${taskId}' aborted`)
1003996
}
1004997

998+
private getQueuedTasks(): number {
999+
return this.workerNodes.reduce((accumulator, workerNode) => {
1000+
return accumulator + workerNode.usage.tasks.queued
1001+
}, 0)
1002+
}
1003+
1004+
private getStealingWorkerNodes(): number {
1005+
return this.workerNodes.reduce(
1006+
(accumulator, _, workerNodeKey) =>
1007+
this.isWorkerNodeStealing(workerNodeKey)
1008+
? accumulator + 1
1009+
: accumulator,
1010+
0,
1011+
)
1012+
}
1013+
10051014
/**
10061015
* Gets task function worker choice strategy, if any.
10071016
*
@@ -1827,7 +1836,7 @@ export abstract class AbstractPool<
18271836
!this.started ||
18281837
this.destroying ||
18291838
this.workerNodes.length <= 1 ||
1830-
this.info.queuedTasks === 0
1839+
this.getQueuedTasks() === 0
18311840
)
18321841
}
18331842

@@ -1976,7 +1985,7 @@ export abstract class AbstractPool<
19761985
private readonly isStealingRatioReached = (): boolean => {
19771986
return (
19781987
this.opts.tasksQueueOptions?.tasksStealingRatio === 0 ||
1979-
(this.info.stealingWorkerNodes ?? 0) >
1988+
this.getStealingWorkerNodes() >
19801989
Math.ceil(
19811990
this.workerNodes.length *
19821991
this.opts.tasksQueueOptions!.tasksStealingRatio!,

0 commit comments

Comments
 (0)