Skip to content

Commit

Permalink
perf: Region timeline improvements (#8203)
Browse files Browse the repository at this point in the history
- Only run filter timer if we have any region cached
- use map for faster lookup
  • Loading branch information
tykus160 authored Mar 4, 2025
1 parent 31b32d3 commit 235fbea
Showing 1 changed file with 39 additions and 37 deletions.
76 changes: 39 additions & 37 deletions lib/media/region_timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ shaka.media.RegionTimeline = class extends shaka.util.FakeEventTarget {
constructor(getSeekRange) {
super();

/** @private {!Set<T>} */
this.regions_ = new Set();
/** @private {!Map<string, T>} */
this.regions_ = new Map();

/** @private {!function():{start: number, end: number}} */
this.getSeekRange_ = getSeekRange;
Expand All @@ -41,33 +41,31 @@ shaka.media.RegionTimeline = class extends shaka.util.FakeEventTarget {
*
* @private {shaka.util.Timer}
*/
this.filterTimer_ = new shaka.util.Timer(() => {
this.filterBySeekRange_();
}).tickEvery(
/* seconds= */ shaka.media.RegionTimeline.REGION_FILTER_INTERVAL);
this.filterTimer_ = null;
}

/** @override */
release() {
this.regions_.clear();
this.filterTimer_.stop();
this.releaseTimer_();
super.release();
}

/**
* @param {T} region
*/
addRegion(region) {
const similarRegion = this.findSimilarRegion_(region);
const key = this.generateKey_(region);

// Make sure we don't add duplicate regions. We keep track of this here
// instead of making the parser track it.
if (similarRegion == null) {
this.regions_.add(region);
if (!this.regions_.has(key)) {
this.regions_.set(key, region);
const event = new shaka.util.FakeEvent('regionadd', new Map([
['region', region],
]));
this.dispatchEvent(event);
this.setupTimer_();
}
}

Expand All @@ -76,48 +74,42 @@ shaka.media.RegionTimeline = class extends shaka.util.FakeEventTarget {
*/
filterBySeekRange_() {
const seekRange = this.getSeekRange_();
for (const region of this.regions_) {
for (const [key, region] of this.regions_) {
// Only consider the seek range start here.
// Future regions might become relevant eventually,
// but regions that are in the past and can't ever be
// seeked to will never come up again, and there's no
// reason to store or process them.
if (region.endTime < seekRange.start) {
this.regions_.delete(region);
this.regions_.delete(key);
const event = new shaka.util.FakeEvent('regionremove', new Map([
['region', region],
]));
this.dispatchEvent(event);
}
}
if (!this.regions_.size) {
this.releaseTimer_();
}
}

/**
* Find a region in the timeline that has the same scheme id uri, event id,
* start time and end time. If these four parameters match, we assume it
* to be the same region. If no similar region can be found, |null| will be
* returned.
*
* @param {T} region
* @return {?T}
* @private
*/
findSimilarRegion_(region) {
const isDiffNegligible = (a, b) => Math.abs(a - b) < 0.1;
for (const existing of this.regions_) {
// The same scheme ID and time range means that it is similar-enough to
// be the same region.
const isSimilar = existing.schemeIdUri == region.schemeIdUri &&
existing.id == region.id &&
isDiffNegligible(existing.startTime, region.startTime) &&
isDiffNegligible(existing.endTime, region.endTime);

if (isSimilar) {
return existing;
}
/** @private */
setupTimer_() {
if (this.filterTimer_) {
return;
}
this.filterTimer_ = new shaka.util.Timer(() => {
this.filterBySeekRange_();
}).tickEvery(
/* seconds= */ shaka.media.RegionTimeline.REGION_FILTER_INTERVAL);
}

return null;
/** @private */
releaseTimer_() {
if (this.filterTimer_) {
this.filterTimer_.stop();
this.filterTimer_ = null;
}
}

/**
Expand All @@ -128,7 +120,17 @@ shaka.media.RegionTimeline = class extends shaka.util.FakeEventTarget {
* @return {!Iterable<T>}
*/
regions() {
return this.regions_;
return this.regions_.values();
}

/**
* @param {T} region
* @return {string}
* @private
*/
generateKey_(region) {
return `${region.schemeIdUri}_${region.id}_` +
`${region.startTime.toFixed(1)}_${region.endTime.toFixed(1)}`;
}
};

Expand Down

0 comments on commit 235fbea

Please sign in to comment.