Skip to content

Change the loadedChunks property, on ChunkedStream instances, from an Array to a Set #11972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions src/core/chunked_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class ChunkedStream {
this.pos = 0;
this.end = length;
this.chunkSize = chunkSize;
this.loadedChunks = [];
this.numChunksLoaded = 0;
this._loadedChunks = new Set();
this.numChunks = Math.ceil(length / chunkSize);
this.manager = manager;
this.progressiveDataLength = 0;
Expand All @@ -42,7 +41,7 @@ class ChunkedStream {
getMissingChunks() {
const chunks = [];
for (let chunk = 0, n = this.numChunks; chunk < n; ++chunk) {
if (!this.loadedChunks[chunk]) {
if (!this._loadedChunks.has(chunk)) {
chunks.push(chunk);
}
}
Expand All @@ -53,6 +52,10 @@ class ChunkedStream {
return [this];
}

get numChunksLoaded() {
return this._loadedChunks.size;
}

allChunksLoaded() {
return this.numChunksLoaded === this.numChunks;
}
Expand All @@ -75,10 +78,9 @@ class ChunkedStream {
const endChunk = Math.floor((end - 1) / chunkSize) + 1;

for (let curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
if (!this.loadedChunks[curChunk]) {
this.loadedChunks[curChunk] = true;
++this.numChunksLoaded;
}
// Since a value can only occur *once* in a `Set`, there's no need to
// manually check `Set.prototype.has()` before adding the value here.
this._loadedChunks.add(curChunk);
}
}

Expand All @@ -95,10 +97,9 @@ class ChunkedStream {
: Math.floor(position / this.chunkSize);

for (let curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
if (!this.loadedChunks[curChunk]) {
this.loadedChunks[curChunk] = true;
++this.numChunksLoaded;
}
// Since a value can only occur *once* in a `Set`, there's no need to
// manually check `Set.prototype.has()` before adding the value here.
this._loadedChunks.add(curChunk);
}
}

Expand All @@ -112,7 +113,7 @@ class ChunkedStream {
return;
}

if (!this.loadedChunks[chunk]) {
if (!this._loadedChunks.has(chunk)) {
throw new MissingDataException(pos, pos + 1);
}
this.lastSuccessfulEnsureByteChunk = chunk;
Expand All @@ -130,7 +131,7 @@ class ChunkedStream {
const beginChunk = Math.floor(begin / chunkSize);
const endChunk = Math.floor((end - 1) / chunkSize) + 1;
for (let chunk = beginChunk; chunk < endChunk; ++chunk) {
if (!this.loadedChunks[chunk]) {
if (!this._loadedChunks.has(chunk)) {
throw new MissingDataException(begin, end);
}
}
Expand All @@ -140,15 +141,15 @@ class ChunkedStream {
const numChunks = this.numChunks;
for (let i = 0; i < numChunks; ++i) {
const chunk = (beginChunk + i) % numChunks; // Wrap around to beginning.
if (!this.loadedChunks[chunk]) {
if (!this._loadedChunks.has(chunk)) {
return chunk;
}
}
return null;
}

hasChunk(chunk) {
return !!this.loadedChunks[chunk];
return this._loadedChunks.has(chunk);
}

get length() {
Expand Down Expand Up @@ -286,7 +287,7 @@ class ChunkedStream {
const endChunk = Math.floor((this.end - 1) / chunkSize) + 1;
const missingChunks = [];
for (let chunk = beginChunk; chunk < endChunk; ++chunk) {
if (!this.loadedChunks[chunk]) {
if (!this._loadedChunks.has(chunk)) {
missingChunks.push(chunk);
}
}
Expand Down