From: Jérôme Benoit Date: Wed, 23 Aug 2023 10:29:40 +0000 (+0200) Subject: refactor: remove unused queue code X-Git-Tag: v2.6.32~6 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=251d6ac2d29d7361d671a8df17af51b414ea3b83;p=poolifier.git refactor: remove unused queue code Signed-off-by: Jérôme Benoit --- diff --git a/src/index.ts b/src/index.ts index a4e22756..6f6c5384 100644 --- a/src/index.ts +++ b/src/index.ts @@ -70,5 +70,5 @@ export type { WorkerStatistics } from './utility-types' export type { CircularArray } from './circular-array' -export type { Queue } from './queue' +export type { Deque } from './deque' export { availableParallelism } from './utils' diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index f0119f4e..f0304364 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -1205,12 +1205,12 @@ export abstract class AbstractPool< if (executeTask) { this.executeTask( targetWorkerNodeKey, - this.popTask(workerNodeKey) as Task + this.dequeueTask(workerNodeKey) as Task ) } else { this.enqueueTask( targetWorkerNodeKey, - this.popTask(workerNodeKey) as Task + this.dequeueTask(workerNodeKey) as Task ) } } @@ -1426,10 +1426,6 @@ export abstract class AbstractPool< return this.workerNodes[workerNodeKey].dequeueTask() } - private popTask (workerNodeKey: number): Task | undefined { - return this.workerNodes[workerNodeKey].popTask() - } - private tasksQueueSize (workerNodeKey: number): number { return this.workerNodes[workerNodeKey].tasksQueueSize() } diff --git a/src/queue.ts b/src/queue.ts deleted file mode 100644 index d38296b2..00000000 --- a/src/queue.ts +++ /dev/null @@ -1,102 +0,0 @@ -// Copyright Jerome Benoit. 2022-2023. All Rights Reserved. - -/** - * Queue. - * - * @typeParam T - Type of queue items. - */ -export class Queue { - private items!: T[] - private offset!: number - /** The size of the queue. */ - public size!: number - /** The maximum size of the queue. */ - public maxSize!: number - - public constructor () { - this.clear() - } - - /** - * Enqueue an item. - * - * @param item - Item to enqueue. - * @returns The new size of the queue. - */ - public enqueue (item: T): number { - this.items.push(item) - ++this.size - if (this.size > this.maxSize) { - this.maxSize = this.size - } - return this.size - } - - /** - * Dequeue an item. - * - * @returns The dequeued item or `undefined` if the queue is empty. - */ - public dequeue (): T | undefined { - if (this.size <= 0) { - return undefined - } - const item = this.items[this.offset] - if (++this.offset * 2 >= this.items.length) { - this.items = this.items.slice(this.offset) - this.offset = 0 - } - --this.size - return item - } - - /** - * Peeks at the first item. - * - * @returns The first item or `undefined` if the queue is empty. - */ - public peek (): T | undefined { - if (this.size <= 0) { - return undefined - } - return this.items[this.offset] - } - - /** - * Clears the queue. - */ - public clear (): void { - this.items = [] - this.offset = 0 - this.size = 0 - this.maxSize = 0 - } - - /** - * Returns an iterator for the queue. - * - * @returns An iterator for the queue. - * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols - */ - [Symbol.iterator] (): Iterator { - const items = this.items - let i = this.offset - - return { - next: () => { - if (i >= items.length) { - return { - value: undefined, - done: true - } - } - const value = items[i] - ++i - return { - value, - done: false - } - } - } - } -} diff --git a/tests/queue.test.js b/tests/queue.test.js deleted file mode 100644 index f123a053..00000000 --- a/tests/queue.test.js +++ /dev/null @@ -1,79 +0,0 @@ -// const { expect } = require('expect') -// const { Queue } = require('../lib/queue') - -// describe('Queue test suite', () => { -// it('Verify enqueue() behavior', () => { -// const queue = new Queue() -// let rtSize = queue.enqueue(1) -// expect(queue.size).toBe(1) -// expect(rtSize).toBe(queue.size) -// expect(queue.offset).toBe(0) -// expect(queue.maxSize).toBe(1) -// expect(queue.items).toStrictEqual([1]) -// rtSize = queue.enqueue(2) -// expect(queue.size).toBe(2) -// expect(rtSize).toBe(queue.size) -// expect(queue.offset).toBe(0) -// expect(queue.maxSize).toBe(2) -// expect(queue.items).toStrictEqual([1, 2]) -// rtSize = queue.enqueue(3) -// expect(queue.size).toBe(3) -// expect(rtSize).toBe(queue.size) -// expect(queue.offset).toBe(0) -// expect(queue.maxSize).toBe(3) -// expect(queue.items).toStrictEqual([1, 2, 3]) -// }) - -// it('Verify dequeue() behavior', () => { -// const queue = new Queue() -// queue.enqueue(1) -// queue.enqueue(2) -// queue.enqueue(3) -// let rtItem = queue.dequeue() -// expect(queue.size).toBe(2) -// expect(rtItem).toBe(1) -// expect(queue.offset).toBe(1) -// expect(queue.maxSize).toBe(3) -// expect(queue.items).toStrictEqual([1, 2, 3]) -// rtItem = queue.dequeue() -// expect(queue.size).toBe(1) -// expect(rtItem).toBe(2) -// expect(queue.offset).toBe(0) -// expect(queue.maxSize).toBe(3) -// expect(queue.items).toStrictEqual([3]) -// rtItem = queue.dequeue() -// expect(queue.size).toBe(0) -// expect(rtItem).toBe(3) -// expect(queue.offset).toBe(0) -// expect(queue.maxSize).toBe(3) -// expect(queue.items).toStrictEqual([]) -// }) - -// it('Verify clear() behavior', () => { -// const queue = new Queue() -// queue.enqueue(1) -// queue.enqueue(2) -// queue.enqueue(3) -// expect(queue.size).toBe(3) -// expect(queue.maxSize).toBe(3) -// expect(queue.items).toStrictEqual([1, 2, 3]) -// expect(queue.offset).toBe(0) -// queue.clear() -// expect(queue.size).toBe(0) -// expect(queue.maxSize).toBe(0) -// expect(queue.items).toStrictEqual([]) -// expect(queue.offset).toBe(0) -// }) - -// it('Verify iterator behavior', () => { -// const queue = new Queue() -// queue.enqueue(1) -// queue.enqueue(2) -// queue.enqueue(3) -// let i = 1 -// for (const item of queue) { -// expect(item).toBe(i) -// ++i -// } -// }) -// })