Skip to content

Commit

Permalink
fix(performance): Eliminate use of ES6 generators
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyparrish committed Mar 25, 2022
1 parent f6d5b10 commit 810df3d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
13 changes: 13 additions & 0 deletions build/conformance.textproto
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,16 @@ requirement: {
"instead."
whitelist_regexp: "lib/net/http_fetch_plugin.js"
}

# Disallow the use of generators, which are a major performance issue. See
# https://github.com/shaka-project/shaka-player/issues/4062#issuecomment-1079428268
requirement: {
type: BANNED_CODE_PATTERN
value:
"/** @param {*} x */ "
"function *template(x) { yield x; }"
error_message:
"ES6 generators are a major performance issue! Find another solution. "
"See also https://bit.ly/3wAsoj5"
whitelist_regexp: "node_modules/"
}
8 changes: 5 additions & 3 deletions lib/cea/cea708_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,17 +300,19 @@ shaka.cea.Cea708Service = class {
/**
* Yields each non-null window specified in the 8-bit bitmap.
* @param {number} bitmap 8 bits corresponding to each of the 8 windows.
* @return {!Iterable.<number>}
* @return {!Array.<number>}
* @private
*/
* getSpecifiedWindowIds_(bitmap) {
getSpecifiedWindowIds_(bitmap) {
const ids = [];
for (let i = 0; i < 8; i++) {
const windowSpecified = (bitmap & 0x01) === 0x01;
if (windowSpecified && this.windows_[i]) {
yield i;
ids.push(i);
}
bitmap >>= 1;
}
return ids;
}

/**
Expand Down
9 changes: 6 additions & 3 deletions lib/cea/sei_processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ shaka.cea.SeiProcessor = class {
/**
* Processes supplemental enhancement information data.
* @param {!Uint8Array} naluData NALU from which SEI data is to be processed.
* @return {!Iterable.<!Uint8Array>}
* @return {!Array.<!Uint8Array>}
*/
* process(naluData) {
process(naluData) {
const seiPayloads = [];
const naluClone = this.removeEmu(naluData);

// The following is an implementation of section 7.3.2.3.1
Expand All @@ -41,10 +42,12 @@ shaka.cea.SeiProcessor = class {
// Payload type 4 is user_data_registered_itu_t_t35, as per the H.264
// spec. This payload type contains caption data.
if (payloadType == 0x04) {
yield naluClone.subarray(offset, offset + payloadSize);
seiPayloads.push(naluClone.subarray(offset, offset + payloadSize));
}
offset += payloadSize;
}

return seiPayloads;
}


Expand Down
26 changes: 18 additions & 8 deletions lib/util/iterables.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ shaka.util.Iterables = class {
* Returns an iterable that contains numbers in the range [0, end).
*
* @param {number} end The exclusive end of the list.
* @return {!Iterable.<number>}
* @return {!Array.<number>}
*/
static* range(end) {
static range(end) {
const counters = [];
for (let i = 0; i < end; i++) {
yield i;
counters.push(i);
}
return counters;
}

/**
Expand All @@ -96,11 +98,17 @@ shaka.util.Iterables = class {
* - The previous item in the list, if it exists.
*
* @param {!Iterable.<T>} iterable
* @return {!Iterable.<
* {i: number, item: T, prev: (T|undefined), next: (T|undefined)}>}
* @return {!Array.<{
* i: number,
* item: T,
* prev: (T|undefined),
* next: (T|undefined)
* }>}
* @template T
*/
static* enumerate(iterable) {
static enumerate(iterable) {
const enumeration = [];

// Since we want the "next" item, we need to skip the first item and return
// elements one in the past. So as we iterate, we are getting the "next"
// element and yielding the one from the previous iteration.
Expand All @@ -109,7 +117,7 @@ shaka.util.Iterables = class {
let item = undefined;
for (const next of iterable) {
if (i >= 0) {
yield {i, item, prev, next};
enumeration.push({i, item, prev, next});
}
i++;
prev = item;
Expand All @@ -118,7 +126,9 @@ shaka.util.Iterables = class {
if (i != -1) {
// If it's still -1, there were no items. Otherwise we need to yield
// the last item.
yield {i, prev, item, next: undefined};
enumeration.push({i, prev, item, next: undefined});
}

return enumeration;
}
};

0 comments on commit 810df3d

Please sign in to comment.