From: Jérôme Benoit Date: Tue, 28 May 2024 19:14:59 +0000 (+0200) Subject: perf: remove unneeded counter on fixed priority queue X-Git-Tag: v4.0.13~4 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=10b1b252553551468565e3e3f55d34a4a1858e6d;p=poolifier.git perf: remove unneeded counter on fixed priority queue Signed-off-by: Jérôme Benoit --- diff --git a/src/fixed-priority-queue.ts b/src/fixed-priority-queue.ts index 3b767eb8..2d851ca4 100644 --- a/src/fixed-priority-queue.ts +++ b/src/fixed-priority-queue.ts @@ -23,9 +23,10 @@ export interface FixedPriorityQueueNode { export class FixedPriorityQueue { private start!: number private readonly nodeArray: Array> + /** The fixed priority queue capacity. */ public readonly capacity: number + /** The fixed priority queue size */ public size!: number - public maxSize!: number /** * Constructs a fixed priority queue. @@ -92,7 +93,7 @@ export class FixedPriorityQueue { } this.nodeArray[index] = { data, priority } } - return this.incrementSize() + return ++this.size } /** @@ -136,7 +137,6 @@ export class FixedPriorityQueue { public clear (): void { this.start = 0 this.size = 0 - this.maxSize = 0 } /** @@ -170,19 +170,6 @@ export class FixedPriorityQueue { } } - /** - * Increments the size of the fixed priority queue. - * - * @returns The new size of the fixed priority queue. - */ - private incrementSize (): number { - ++this.size - if (this.size > this.maxSize) { - this.maxSize = this.size - } - return this.size - } - /** * Checks the size. * diff --git a/tests/fixed-priority-queue.test.mjs b/tests/fixed-priority-queue.test.mjs index ddf002f0..be25a8f4 100644 --- a/tests/fixed-priority-queue.test.mjs +++ b/tests/fixed-priority-queue.test.mjs @@ -16,13 +16,11 @@ describe('Fixed priority queue test suite', () => { let fixedPriorityQueue = new FixedPriorityQueue() expect(fixedPriorityQueue.start).toBe(0) expect(fixedPriorityQueue.size).toBe(0) - expect(fixedPriorityQueue.maxSize).toBe(0) expect(fixedPriorityQueue.nodeArray).toBeInstanceOf(Array) expect(fixedPriorityQueue.capacity).toBe(defaultQueueSize) fixedPriorityQueue = new FixedPriorityQueue(2) expect(fixedPriorityQueue.start).toBe(0) expect(fixedPriorityQueue.size).toBe(0) - expect(fixedPriorityQueue.maxSize).toBe(0) expect(fixedPriorityQueue.nodeArray).toBeInstanceOf(Array) expect(fixedPriorityQueue.capacity).toBe(2) }) @@ -33,7 +31,6 @@ describe('Fixed priority queue test suite', () => { let rtSize = fixedPriorityQueue.enqueue(1) expect(fixedPriorityQueue.start).toBe(0) expect(fixedPriorityQueue.size).toBe(1) - expect(fixedPriorityQueue.maxSize).toBe(1) expect(rtSize).toBe(fixedPriorityQueue.size) expect(fixedPriorityQueue.nodeArray).toMatchObject([ { data: 1, priority: 0 } @@ -42,7 +39,6 @@ describe('Fixed priority queue test suite', () => { rtSize = fixedPriorityQueue.enqueue(2) expect(fixedPriorityQueue.start).toBe(0) expect(fixedPriorityQueue.size).toBe(2) - expect(fixedPriorityQueue.maxSize).toBe(2) expect(rtSize).toBe(fixedPriorityQueue.size) expect(fixedPriorityQueue.nodeArray).toMatchObject([ { data: 1, priority: 0 }, @@ -52,7 +48,6 @@ describe('Fixed priority queue test suite', () => { rtSize = fixedPriorityQueue.enqueue(3) expect(fixedPriorityQueue.start).toBe(0) expect(fixedPriorityQueue.size).toBe(3) - expect(fixedPriorityQueue.maxSize).toBe(3) expect(rtSize).toBe(fixedPriorityQueue.size) expect(fixedPriorityQueue.nodeArray).toMatchObject([ { data: 1, priority: 0 }, @@ -63,7 +58,6 @@ describe('Fixed priority queue test suite', () => { rtSize = fixedPriorityQueue.enqueue(3, -1) expect(fixedPriorityQueue.start).toBe(0) expect(fixedPriorityQueue.size).toBe(4) - expect(fixedPriorityQueue.maxSize).toBe(4) expect(rtSize).toBe(fixedPriorityQueue.size) expect(fixedPriorityQueue.nodeArray).toMatchObject([ { data: 3, priority: -1 }, @@ -75,7 +69,6 @@ describe('Fixed priority queue test suite', () => { rtSize = fixedPriorityQueue.enqueue(1, 1) expect(fixedPriorityQueue.start).toBe(0) expect(fixedPriorityQueue.size).toBe(5) - expect(fixedPriorityQueue.maxSize).toBe(5) expect(rtSize).toBe(fixedPriorityQueue.size) expect(fixedPriorityQueue.nodeArray).toMatchObject([ { data: 3, priority: -1 }, @@ -98,12 +91,10 @@ describe('Fixed priority queue test suite', () => { fixedPriorityQueue.enqueue(3) expect(fixedPriorityQueue.start).toBe(0) expect(fixedPriorityQueue.size).toBe(3) - expect(fixedPriorityQueue.maxSize).toBe(3) expect(fixedPriorityQueue.capacity).toBe(queueSize) let rtItem = fixedPriorityQueue.dequeue() expect(fixedPriorityQueue.start).toBe(1) expect(fixedPriorityQueue.size).toBe(2) - expect(fixedPriorityQueue.maxSize).toBe(3) expect(rtItem).toBe(2) expect(fixedPriorityQueue.nodeArray).toMatchObject([ { data: 2, priority: -1 }, @@ -114,7 +105,6 @@ describe('Fixed priority queue test suite', () => { rtItem = fixedPriorityQueue.dequeue() expect(fixedPriorityQueue.start).toBe(2) expect(fixedPriorityQueue.size).toBe(1) - expect(fixedPriorityQueue.maxSize).toBe(3) expect(rtItem).toBe(1) expect(fixedPriorityQueue.nodeArray).toMatchObject([ { data: 2, priority: -1 }, @@ -125,7 +115,6 @@ describe('Fixed priority queue test suite', () => { rtItem = fixedPriorityQueue.dequeue() expect(fixedPriorityQueue.start).toBe(3) expect(fixedPriorityQueue.size).toBe(0) - expect(fixedPriorityQueue.maxSize).toBe(3) expect(rtItem).toBe(3) expect(fixedPriorityQueue.nodeArray).toMatchObject([ { data: 2, priority: -1 }, @@ -136,7 +125,6 @@ describe('Fixed priority queue test suite', () => { rtItem = fixedPriorityQueue.dequeue() expect(fixedPriorityQueue.start).toBe(3) expect(fixedPriorityQueue.size).toBe(0) - expect(fixedPriorityQueue.maxSize).toBe(3) expect(rtItem).toBe(undefined) expect(fixedPriorityQueue.nodeArray).toMatchObject([ { data: 2, priority: -1 }, @@ -188,7 +176,6 @@ describe('Fixed priority queue test suite', () => { const fixedPriorityQueue = new FixedPriorityQueue() fixedPriorityQueue.start = 1 fixedPriorityQueue.size = 2 - fixedPriorityQueue.maxSize = 2 fixedPriorityQueue.nodeArray = [ { data: 2, priority: 0 }, { data: 3, priority: 0 } @@ -196,7 +183,6 @@ describe('Fixed priority queue test suite', () => { fixedPriorityQueue.clear() expect(fixedPriorityQueue.start).toBe(0) expect(fixedPriorityQueue.size).toBe(0) - expect(fixedPriorityQueue.maxSize).toBe(0) expect(fixedPriorityQueue.nodeArray).toMatchObject([ { data: 2, priority: 0 }, { data: 3, priority: 0 }