Skip to content
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

fix(dash): Fix performance regression #4064

Merged
merged 1 commit into from Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 4 additions & 6 deletions lib/dash/dash_parser.js
Expand Up @@ -22,7 +22,6 @@ goog.require('shaka.text.TextEngine');
goog.require('shaka.util.CmcdManager');
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 @@ -104,7 +103,7 @@ shaka.dash.DashParser = class {

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

Expand Down Expand Up @@ -556,10 +555,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) {
avelad marked this conversation as resolved.
Show resolved Hide resolved
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For historical context, we used to have frequent issues with mistakes in code like this. It is easy to mix up i and j for example, and easy to overlook in code review. So Iterables was a way to avoid the need for those.

Clearly the performance penalty is too great, though. We should figure out what other things transpile into poorly-performing ES5 and eliminate them. We could even write a test that would check the binary for poorly-performing polyfills, to catch regressions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From Monday I will continue working on other improvements, but I will create other PRs.

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 @@ -504,8 +503,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