Skip to content

Commit f007f8f

Browse files
vanyaxkavelad
authored andcommitted
perf(mp4generator): stop nesting concat in methods (#6041)
This change removes `concat` util function from mp4 generator iterations and runs it later, as well as stops creating new Uint8 arrays, as `concat` does this within itself.
1 parent 7573f64 commit f007f8f

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

lib/util/mp4_generator.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ shaka.util.Mp4Generator = class {
4747
goog.asserts.assert(this.streamInfos_.length > 0,
4848
'StreamInfos must have elements');
4949
const Mp4Generator = shaka.util.Mp4Generator;
50-
let traks = new Uint8Array([]);
50+
const trakArrays = [];
5151
for (const streamInfo of this.streamInfos_) {
52-
traks = shaka.util.Uint8ArrayUtils.concat(traks, this.trak_(streamInfo));
52+
trakArrays.push(this.trak_(streamInfo));
5353
}
54+
const traks = shaka.util.Uint8ArrayUtils.concat(...trakArrays);
5455
const firstStreamInfo = this.streamInfos_[0];
5556
return Mp4Generator.box('moov',
5657
this.mvhd_(firstStreamInfo),
@@ -693,10 +694,11 @@ shaka.util.Mp4Generator = class {
693694
*/
694695
mvex_() {
695696
const Mp4Generator = shaka.util.Mp4Generator;
696-
let trexs = new Uint8Array([]);
697+
const trexArrays = [];
697698
for (const streamInfo of this.streamInfos_) {
698-
trexs = shaka.util.Uint8ArrayUtils.concat(trexs, this.trex_(streamInfo));
699+
trexArrays.push(this.trex_(streamInfo));
699700
}
701+
const trexs = shaka.util.Uint8ArrayUtils.concat(...trexArrays);
700702
return Mp4Generator.box('mvex', trexs);
701703
}
702704

@@ -730,19 +732,20 @@ shaka.util.Mp4Generator = class {
730732
* @private
731733
*/
732734
pssh_(streamInfo) {
733-
let boxes = new Uint8Array([]);
735+
const initDatas = [];
734736
if (!streamInfo.encrypted) {
735-
return boxes;
737+
return new Uint8Array([]);
736738
}
737739

738740
for (const drmInfo of streamInfo.stream.drmInfos) {
739741
if (!drmInfo.initData) {
740742
continue;
741743
}
742744
for (const initData of drmInfo.initData) {
743-
boxes = shaka.util.Uint8ArrayUtils.concat(boxes, initData.initData);
745+
initDatas.push(initData.initData);
744746
}
745747
}
748+
const boxes = shaka.util.Uint8ArrayUtils.concat(...initDatas);
746749
return boxes;
747750
}
748751

@@ -845,11 +848,12 @@ shaka.util.Mp4Generator = class {
845848
* @return {!Uint8Array}
846849
*/
847850
segmentData() {
848-
let result = new Uint8Array([]);
851+
const segmentDataArray = [];
849852
for (const streamInfo of this.streamInfos_) {
850-
result = shaka.util.Uint8ArrayUtils.concat(result,
851-
this.moof_(streamInfo), this.mdat_(streamInfo));
853+
segmentDataArray.push(
854+
...[this.moof_(streamInfo), this.mdat_(streamInfo)]);
852855
}
856+
const result = shaka.util.Uint8ArrayUtils.concat(...segmentDataArray);
853857
return result;
854858
}
855859

0 commit comments

Comments
 (0)