From e313815ead460f3c3bc0005252c171dcaa37a103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Velad=20Galv=C3=A1n?= Date: Fri, 25 Mar 2022 21:56:38 +0100 Subject: [PATCH] fix(dash): Fix performance regression (#4064) See: https://github.com/shaka-project/shaka-player/issues/4062#issuecomment-1077826210 --- lib/dash/dash_parser.js | 10 ++++------ lib/dash/mpd_utils.js | 9 ++++----- lib/dash/segment_template.js | 6 +++--- lib/util/string_utils.js | 5 ++--- lib/util/uint8array_utils.js | 5 ++--- lib/util/xml_utils.js | 22 +++++++++++++++------- 6 files changed, 30 insertions(+), 27 deletions(-) diff --git a/lib/dash/dash_parser.js b/lib/dash/dash_parser.js index f29afa7832..5004cb062d 100644 --- a/lib/dash/dash_parser.js +++ b/lib/dash/dash_parser.js @@ -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'); @@ -103,7 +102,7 @@ shaka.dash.DashParser = class { /** * Period IDs seen in previous manifest. - * @private {!Array.} + * @private {!Array.} */ this.lastManifestUpdatePeriodIds_ = []; @@ -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; diff --git a/lib/dash/mpd_utils.js b/lib/dash/mpd_utils.js index c18200f338..806485f672 100644 --- a/lib/dash/mpd_utils.js +++ b/lib/dash/mpd_utils.js @@ -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'); @@ -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); @@ -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, diff --git a/lib/dash/segment_template.js b/lib/dash/segment_template.js index 680b6a163f..d5e61948f0 100644 --- a/lib/dash/segment_template.js +++ b/lib/dash/segment_template.js @@ -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'); @@ -491,8 +490,9 @@ shaka.dash.SegmentTemplate = class { /** @type {!Array.} */ 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; diff --git a/lib/util/string_utils.js b/lib/util/string_utils.js index c0539308bf..76f9f834ee 100644 --- a/lib/util/string_utils.js +++ b/lib/util/string_utils.js @@ -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; diff --git a/lib/util/uint8array_utils.js b/lib/util/uint8array_utils.js index 903203daae..0ddcdd1a43 100644 --- a/lib/util/uint8array_utils.js +++ b/lib/util/uint8array_utils.js @@ -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; } diff --git a/lib/util/xml_utils.js b/lib/util/xml_utils.js index 84f4bf3cde..8d3c3773c4 100644 --- a/lib/util/xml_utils.js +++ b/lib/util/xml_utils.js @@ -57,9 +57,13 @@ shaka.util.XmlUtils = class { * @return {!Array.} 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; } @@ -82,10 +86,14 @@ shaka.util.XmlUtils = class { * @return {!Array.} 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; }