Skip to content

Commit

Permalink
chore(perf): Revert commit to bring back up to 20% perf (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
JaoodxD authored May 26, 2024
1 parent 13c8025 commit 5a2f0e0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
9 changes: 2 additions & 7 deletions src/fixed-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,36 +63,32 @@ class FixedCircularBuffer {
top: number
list: Array<Task | undefined>
next: FixedCircularBuffer | null
_size: number

constructor () {
this.bottom = 0;
this.top = 0;
this.list = new Array(kSize);
this.next = null;
this._size = 0;
}

isEmpty () {
return this.top === this.bottom && this._size === 0;
return this.top === this.bottom;
}

isFull () {
return this.top === this.bottom && this._size === kSize;
return ((this.top + 1) & kMask) === this.bottom;
}

push (data:Task) {
this.list[this.top] = data;
this.top = (this.top + 1) & kMask;
this._size++;
}

shift () {
const nextItem = this.list[this.bottom];
if (nextItem === undefined) { return null; }
this.list[this.bottom] = undefined;
this.bottom = (this.bottom + 1) & kMask;
this._size--;
return nextItem;
}

Expand All @@ -114,7 +110,6 @@ class FixedCircularBuffer {
curr = next;
}
this.top = (this.top - 1) & kMask;
this._size--;
}
}

Expand Down
34 changes: 31 additions & 3 deletions test/fixed-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,19 @@ test('remove not queued task should not lead to errors', async ({ equal }) => {
});

test('removing elements from intermediate CircularBuffer should not lead to issues', async ({ equal, same }) => {
/*
The test intends to check following scenario:
1) We fill the queue with 3 full circular buffers amount of items.
2) Empty the middle circular buffer with remove().
3) This should lead to the removal of the middle buffer from the queue:
- Before emptying: tail buffer -> middle buffer -> head buffer.
- After emptying: tail buffer -> head buffer.
*/

const queue = new FixedQueue();

const batchSize = 2048;
// size of single circular buffer
const batchSize = 2047;

const firstBatch = Array.from({ length: batchSize }, () => new QueueTask());
const secondBatch = Array.from({ length: batchSize }, () => new QueueTask());
Expand Down Expand Up @@ -94,9 +104,18 @@ test('removing elements from intermediate CircularBuffer should not lead to issu
});

test('removing elements from first CircularBuffer should not lead to issues', async ({ equal, same }) => {
/*
The test intends to check following scenario:
1) We fill the queue with 3 full circular buffers amount of items.
2) Empty the first circular buffer with remove().
3) This should lead to the removal of the tail buffer from the queue:
- Before emptying: tail buffer -> middle buffer -> head buffer.
- After emptying: tail buffer (previously middle) -> head buffer.
*/
const queue = new FixedQueue();

const batchSize = 2048;
// size of single circular buffer
const batchSize = 2047;

const firstBatch = Array.from({ length: batchSize }, () => new QueueTask());
const secondBatch = Array.from({ length: batchSize }, () => new QueueTask());
Expand Down Expand Up @@ -125,9 +144,18 @@ test('removing elements from first CircularBuffer should not lead to issues', as
});

test('removing elements from last CircularBuffer should not lead to issues', async ({ equal, same }) => {
/*
The test intends to check following scenario:
1) We fill the queue with 3 full circular buffers amount of items.
2) Empty the last circular buffer with remove().
3) This should lead to the removal of the head buffer from the queue:
- Before emptying: tail buffer -> middle buffer -> head buffer.
- After emptying: tail buffer -> head buffer (previously middle).
*/
const queue = new FixedQueue();

const batchSize = 2048;
// size of single circular buffer
const batchSize = 2047;

const firstBatch = Array.from({ length: batchSize }, () => new QueueTask());
const secondBatch = Array.from({ length: batchSize }, () => new QueueTask());
Expand Down

0 comments on commit 5a2f0e0

Please sign in to comment.