Skip to content

Commit

Permalink
fix(dash): Fix performance regression (#4064)
Browse files Browse the repository at this point in the history
  • Loading branch information
Álvaro Velad Galván authored and joeyparrish committed Apr 21, 2022
1 parent c71ca9c commit e313815
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 27 deletions.
10 changes: 4 additions & 6 deletions lib/dash/dash_parser.js
Expand Up @@ -21,7 +21,6 @@ goog.require('shaka.net.NetworkingEngine');
goog.require('shaka.text.TextEngine');
goog.require('shaka.util.Error');
goog.require('shaka.util.Functional');
goog.require('shaka.util.Iterables');
goog.require('shaka.util.LanguageUtils');
goog.require('shaka.util.ManifestParserUtils');
goog.require('shaka.util.MimeUtils');
Expand Down Expand Up @@ -103,7 +102,7 @@ shaka.dash.DashParser = class {

/**
* Period IDs seen in previous manifest.
* @private {!Array.<number>}
* @private {!Array.<string>}
*/
this.lastManifestUpdatePeriodIds_ = [];

Expand Down Expand Up @@ -546,10 +545,9 @@ shaka.dash.DashParser = class {
const periods = [];
let prevEnd = 0;
const periodNodes = XmlUtils.findChildren(mpd, 'Period');
// This uses a for-loop rather than a for-of loop because this needs to look
// ahead to the next element.
const enumerate = (it) => shaka.util.Iterables.enumerate(it);
for (const {i, item: elem, next} of enumerate(periodNodes)) {
for (let i = 0; i < periodNodes.length; i++) {
const elem = periodNodes[i];
const next = periodNodes[i + 1];
const start = /** @type {number} */ (
XmlUtils.parseAttr(elem, 'start', XmlUtils.parseDuration, prevEnd));
const periodId = elem.id;
Expand Down
9 changes: 4 additions & 5 deletions lib/dash/mpd_utils.js
Expand Up @@ -12,7 +12,6 @@ goog.require('shaka.net.NetworkingEngine');
goog.require('shaka.util.AbortableOperation');
goog.require('shaka.util.Error');
goog.require('shaka.util.Functional');
goog.require('shaka.util.Iterables');
goog.require('shaka.util.ManifestParserUtils');
goog.require('shaka.util.XmlUtils');
goog.requireType('shaka.dash.DashParser');
Expand Down Expand Up @@ -142,8 +141,9 @@ shaka.dash.MpdUtils = class {
const timeline = [];
let lastEndTime = -unscaledPresentationTimeOffset;

const enumerate = (it) => shaka.util.Iterables.enumerate(it);
for (const {item: timePoint, next} of enumerate(timePoints)) {
for (let i = 0; i < timePoints.length; ++i) {
const timePoint = timePoints[i];
const next = timePoints[i + 1];
let t = XmlUtils.parseAttr(timePoint, 't', XmlUtils.parseNonNegativeInt);
const d =
XmlUtils.parseAttr(timePoint, 'd', XmlUtils.parseNonNegativeInt);
Expand Down Expand Up @@ -225,8 +225,7 @@ shaka.dash.MpdUtils = class {
timeline[timeline.length - 1].end = startTime / timescale;
}

for (const _ of shaka.util.Iterables.range(repeat + 1)) {
shaka.util.Functional.ignored(_);
for (let j = 0; j <= repeat; ++j) {
const endTime = startTime + d;
const item = {
start: startTime / timescale,
Expand Down
6 changes: 3 additions & 3 deletions lib/dash/segment_template.js
Expand Up @@ -14,7 +14,6 @@ goog.require('shaka.media.InitSegmentReference');
goog.require('shaka.media.SegmentIndex');
goog.require('shaka.media.SegmentReference');
goog.require('shaka.util.Error');
goog.require('shaka.util.Iterables');
goog.require('shaka.util.ManifestParserUtils');
goog.require('shaka.util.ObjectUtils');
goog.requireType('shaka.dash.DashParser');
Expand Down Expand Up @@ -491,8 +490,9 @@ shaka.dash.SegmentTemplate = class {

/** @type {!Array.<!shaka.media.SegmentReference>} */
const references = [];
const enum_ = (it) => shaka.util.Iterables.enumerate(it);
for (const {i, item: {start, unscaledStart, end}} of enum_(info.timeline)) {
for (let i = 0; i < info.timeline.length; i++) {
const {start, unscaledStart, end} = info.timeline[i];

// Note: i = k - 1, where k indicates the k'th segment listed in the MPD.
// (See section 5.3.9.5.3 of the DASH spec.)
const segmentReplacement = i + info.startNumber;
Expand Down
5 changes: 2 additions & 3 deletions lib/util/string_utils.js
Expand Up @@ -158,9 +158,8 @@ shaka.util.StringUtils = class {
static toUTF16(str, littleEndian) {
const result = new ArrayBuffer(str.length * 2);
const view = new DataView(result);
const enumerate = (it) => shaka.util.Iterables.enumerate(it);
for (const {i, item} of enumerate(str)) {
const value = item.charCodeAt(0);
for (let i = 0; i < str.length; ++i) {
const value = str.charCodeAt(i);
view.setUint16(/* position= */ i * 2, value, littleEndian);
}
return result;
Expand Down
5 changes: 2 additions & 3 deletions lib/util/uint8array_utils.js
Expand Up @@ -74,9 +74,8 @@ shaka.util.Uint8ArrayUtils = class {
// byte.
const bytes = window.atob(str.replace(/-/g, '+').replace(/_/g, '/'));
const result = new Uint8Array(bytes.length);
const enumerate = (it) => shaka.util.Iterables.enumerate(it);
for (const {i, item} of enumerate(bytes)) {
result[i] = item.charCodeAt(0);
for (let i = 0; i < bytes.length; ++i) {
result[i] = bytes.charCodeAt(i);
}
return result;
}
Expand Down
22 changes: 15 additions & 7 deletions lib/util/xml_utils.js
Expand Up @@ -57,9 +57,13 @@ shaka.util.XmlUtils = class {
* @return {!Array.<!Element>} The child XML elements.
*/
static findChildren(elem, name) {
return Array.from(elem.childNodes).filter((child) => {
return child instanceof Element && child.tagName == name;
});
const found = [];
for (const child of elem.childNodes) {
if (child instanceof Element && child.tagName == name) {
found.push(child);
}
}
return found;
}


Expand All @@ -82,10 +86,14 @@ shaka.util.XmlUtils = class {
* @return {!Array.<!Element>} The child XML elements.
*/
static findChildrenNS(elem, ns, name) {
return Array.from(elem.childNodes).filter((child) => {
return child instanceof Element && child.localName == name &&
child.namespaceURI == ns;
});
const found = [];
for (const child of elem.childNodes) {
if (child instanceof Element && child.localName == name &&
child.namespaceURI == ns) {
found.push(child);
}
}
return found;
}


Expand Down

0 comments on commit e313815

Please sign in to comment.