-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref: performance fix for v large queues.
Fixes #1
- Loading branch information
1 parent
ee1c581
commit 1128f0d
Showing
6 changed files
with
78 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* streams/queue - simple queue type with chunked array backing | ||
* Part of Stardazed | ||
* (c) 2018 by Arthur Langereis - @zenmumbler | ||
* https://github.com/stardazed/sd-streams | ||
*/ | ||
|
||
const CHUNK_SIZE = 16384; | ||
|
||
export interface Queue<T> { | ||
push(t: T): void; | ||
shift(): T | undefined; | ||
front(): T | undefined; | ||
readonly length: number; | ||
} | ||
|
||
export class QueueImpl<T> implements Queue<T> { | ||
private readonly chunks_: T[][]; | ||
private readChunk_: T[]; | ||
private writeChunk_: T[]; | ||
private length_: number; | ||
|
||
constructor() { | ||
this.chunks_ = [[]]; | ||
this.readChunk_ = this.writeChunk_ = this.chunks_[0]; | ||
this.length_ = 0; | ||
} | ||
|
||
push(t: T): void { | ||
this.writeChunk_.push(t); | ||
this.length_ += 1; | ||
if (this.writeChunk_.length === CHUNK_SIZE) { | ||
this.writeChunk_ = []; | ||
this.chunks_.push(this.writeChunk_); | ||
} | ||
} | ||
|
||
front(): T | undefined { | ||
if (this.length_ === 0) { | ||
return undefined; | ||
} | ||
return this.readChunk_[0]; | ||
} | ||
|
||
shift(): T | undefined { | ||
if (this.length_ === 0) { | ||
return undefined; | ||
} | ||
const t = this.readChunk_.shift(); | ||
|
||
this.length_ -= 1; | ||
if (this.readChunk_.length === 0 && this.readChunk_ !== this.writeChunk_) { | ||
this.chunks_.shift(); | ||
this.readChunk_ = this.chunks_[0]; | ||
} | ||
return t; | ||
} | ||
|
||
get length() { | ||
return this.length_; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters