Skip to content

Commit aef5ba4

Browse files
dpfister2joeyparrish
authored andcommitted
perf: Remove promises from getAllThumbnails loop (#8118)
`getAllThumbnails` creates an array of promises when grabbing each thumbnail. This is because `getThumbnails` might have to create a segment index, but `getAllThumbnails` already does this. This separates `getThumbnails` into two functions so `getAllThumbnails` can get the thumbnails synchronously.
1 parent b8a61dd commit aef5ba4

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

lib/player.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4394,7 +4394,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
43944394
if (!imageStream.segmentIndex) {
43954395
await imageStream.createSegmentIndex();
43964396
}
4397-
const promises = [];
4397+
const thumbnails = [];
43984398
imageStream.segmentIndex.forEachTopLevelReference((reference) => {
43994399
const dimensions = this.parseTilesLayout_(
44004400
reference.getTilesLayout() || imageStream.tilesLayout);
@@ -4403,12 +4403,16 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
44034403
const duration = reference.trueEndTime - reference.startTime;
44044404
for (let i = 0; i < numThumbnails; i++) {
44054405
const sampleTime = reference.startTime + duration * i / numThumbnails;
4406-
promises.push(this.getThumbnails(trackId, sampleTime));
4406+
const thumbnail =
4407+
this.getThumbnailsByStream_(
4408+
/** @type {shaka.extern.Stream} */ (imageStream), sampleTime);
4409+
if (thumbnail) {
4410+
thumbnails.push(thumbnail);
4411+
}
44074412
}
44084413
}
44094414
});
4410-
const thumbnails = await Promise.all(promises);
4411-
return thumbnails.filter((t) => t);
4415+
return thumbnails;
44124416
}
44134417

44144418
/**
@@ -4465,6 +4469,19 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
44654469
if (!imageStream.segmentIndex) {
44664470
await imageStream.createSegmentIndex();
44674471
}
4472+
4473+
return this.getThumbnailsByStream_(imageStream, time);
4474+
}
4475+
4476+
/**
4477+
* Return a Thumbnail object from a image stream and time.
4478+
*
4479+
* @param {shaka.extern.Stream} imageStream
4480+
* @param {number} time
4481+
* @return {?shaka.extern.Thumbnail}
4482+
* @private
4483+
*/
4484+
getThumbnailsByStream_(imageStream, time) {
44684485
const referencePosition = imageStream.segmentIndex.find(time);
44694486
if (referencePosition == null) {
44704487
return null;

0 commit comments

Comments
 (0)