forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use SimpleQueue in WritableStream implementation
Split out Queue into a separate file from ReadableStream and rename is SimpleQueue to reduce the risk of name collisions on the binding object. Add a peek() method as it is needed by WritableStream. Modify WritableStream to use SimpleQueue. BUG=681493 Review-Url: https://codereview.chromium.org/2752133003 Cr-Commit-Position: refs/heads/master@{#458324}
- Loading branch information
Showing
4 changed files
with
107 additions
and
92 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
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,91 @@ | ||
// Copyright 2017 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// Queue implementation used by ReadableStream and WritableStream. | ||
|
||
(function(global, binding, v8) { | ||
'use strict'; | ||
|
||
// Simple queue structure. Avoids scalability issues with using | ||
// InternalPackedArray directly by using multiple arrays in a linked list and | ||
// keeping the array size bounded. | ||
const QUEUE_MAX_ARRAY_SIZE = 16384; | ||
class SimpleQueue { | ||
constructor() { | ||
this.front = { | ||
elements: new v8.InternalPackedArray(), | ||
next: undefined, | ||
}; | ||
this.back = this.front; | ||
// The cursor is used to avoid calling InternalPackedArray.shift(). | ||
this.cursor = 0; | ||
this.size = 0; | ||
} | ||
|
||
get length() { | ||
return this.size; | ||
} | ||
|
||
push(element) { | ||
++this.size; | ||
if (this.back.elements.length === QUEUE_MAX_ARRAY_SIZE) { | ||
const oldBack = this.back; | ||
this.back = { | ||
elements: new v8.InternalPackedArray(), | ||
next: undefined, | ||
}; | ||
oldBack.next = this.back; | ||
} | ||
this.back.elements.push(element); | ||
} | ||
|
||
shift() { | ||
// assert(this.size > 0); | ||
--this.size; | ||
if (this.front.elements.length === this.cursor) { | ||
// assert(this.cursor === QUEUE_MAX_ARRAY_SIZE); | ||
// assert(this.front.next !== undefined); | ||
this.front = this.front.next; | ||
this.cursor = 0; | ||
} | ||
const element = this.front.elements[this.cursor]; | ||
// Permit shifted element to be garbage collected. | ||
this.front.elements[this.cursor] = undefined; | ||
++this.cursor; | ||
|
||
return element; | ||
} | ||
|
||
forEach(callback) { | ||
let i = this.cursor; | ||
let node = this.front; | ||
let elements = node.elements; | ||
while (i !== elements.length || node.next !== undefined) { | ||
if (i === elements.length) { | ||
// assert(node.next !== undefined); | ||
// assert(i === QUEUE_MAX_ARRAY_SIZE); | ||
node = node.next; | ||
elements = node.elements; | ||
i = 0; | ||
} | ||
callback(elements[i]); | ||
++i; | ||
} | ||
} | ||
|
||
// Return the element that would be returned if shift() was called now, | ||
// without modifying the queue. | ||
peek() { | ||
// assert(this.size > 0); | ||
if (this.front.elements.length === this.cursor) { | ||
// assert(this.cursor === QUEUE_MAX_ARRAY_SIZE) | ||
// assert(this.front.next !== undefined); | ||
return this.front.next.elements[0]; | ||
} | ||
return this.front.elements[this.cursor]; | ||
} | ||
} | ||
|
||
binding.SimpleQueue = SimpleQueue; | ||
}); |
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