Skip to content

Commit 067a378

Browse files
committed
perf: optimize tasks stealing in corner case
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
1 parent 6dc5fb8 commit 067a378

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to
88

99
## [Unreleased]
1010

11+
### Changed
12+
13+
- Add a fastpath when tasks stealing or redistribution is impossible
14+
1115
## [0.0.13] - 2023-11-27
1216

1317
### Fixed

src/pools/abstract-pool.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,9 @@ export abstract class AbstractPool<
14371437
}
14381438

14391439
private redistributeQueuedTasks(workerNodeKey: number): void {
1440+
if (this.workerNodes.length <= 1) {
1441+
return
1442+
}
14401443
while (this.tasksQueueSize(workerNodeKey) > 0) {
14411444
const destinationWorkerNodeKey = this.workerNodes.reduce(
14421445
(minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => {
@@ -1530,6 +1533,9 @@ export abstract class AbstractPool<
15301533
event: CustomEvent<WorkerNodeEventDetail>,
15311534
previousStolenTask?: Task<Data>,
15321535
): void => {
1536+
if (this.workerNodes.length <= 1) {
1537+
return
1538+
}
15331539
const { workerNodeKey } = event.detail
15341540
if (workerNodeKey == null) {
15351541
throw new Error(
@@ -1627,6 +1633,9 @@ export abstract class AbstractPool<
16271633
private readonly handleBackPressureEvent = (
16281634
event: CustomEvent<WorkerNodeEventDetail>,
16291635
): void => {
1636+
if (this.workerNodes.length <= 1) {
1637+
return
1638+
}
16301639
const { workerId } = event.detail
16311640
const sizeOffset = 1
16321641
if ((this.opts.tasksQueueOptions?.size as number) <= sizeOffset) {

0 commit comments

Comments
 (0)