Skip to content

Commit

Permalink
perf(transmuxer): various performance improvements (#6003)
Browse files Browse the repository at this point in the history
This change improves loop performance - creating new arrays is not
performant in large loops, and a traditional for loop is more performant
than for .. of for large concatenations

---------

Co-authored-by: Ivan Kohut <ivan.kohut@lamin.ar>
Co-authored-by: Álvaro Velad Galván <ladvan91@hotmail.com>
  • Loading branch information
3 people committed Jan 8, 2024
1 parent 42c235d commit cd326e2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
8 changes: 4 additions & 4 deletions lib/transmuxer/ts_transmuxer.js
Expand Up @@ -705,7 +705,7 @@ shaka.transmuxer.TsTransmuxer = class {
/** @type {?number} */
let baseMediaDecodeTime = null;

let nalus = [];
const nalus = [];
const videoData = tsParser.getVideoData();
if (!videoData.length) {
throw new shaka.util.Error(
Expand All @@ -716,7 +716,7 @@ shaka.transmuxer.TsTransmuxer = class {
for (let i = 0; i < videoData.length; i++) {
const pes = videoData[i];
const dataNalus = pes.nalus;
nalus = nalus.concat(dataNalus);
nalus.push(...dataNalus);
const frame = H264.parseFrame(dataNalus);
if (!frame) {
continue;
Expand Down Expand Up @@ -797,7 +797,7 @@ shaka.transmuxer.TsTransmuxer = class {
/** @type {?number} */
let baseMediaDecodeTime = null;

let nalus = [];
const nalus = [];
const videoData = tsParser.getVideoData();
if (!videoData.length) {
throw new shaka.util.Error(
Expand All @@ -808,7 +808,7 @@ shaka.transmuxer.TsTransmuxer = class {
for (let i = 0; i < videoData.length; i++) {
const pes = videoData[i];
const dataNalus = pes.nalus;
nalus = nalus.concat(dataNalus);
nalus.push(...dataNalus);
const frame = H265.parseFrame(dataNalus);
if (!frame) {
continue;
Expand Down
19 changes: 12 additions & 7 deletions lib/util/uint8array_utils.js
Expand Up @@ -107,21 +107,26 @@ shaka.util.Uint8ArrayUtils = class {
* @export
*/
static concat(...varArgs) {
const BufferUtils = shaka.util.BufferUtils;
let totalLength = 0;
for (const arr of varArgs) {
totalLength += arr.byteLength;
for (let i = 0; i < varArgs.length; ++i) {
const value = varArgs[i];
totalLength += value.byteLength;
}

const result = new Uint8Array(totalLength);
let offset = 0;
for (const arr of varArgs) {
if (arr instanceof Uint8Array) {
result.set(arr, offset);

for (let i = 0; i < varArgs.length; ++i) {
const value = varArgs[i];
if (value instanceof Uint8Array) {
result.set(value, offset);
} else {
result.set(shaka.util.BufferUtils.toUint8(arr), offset);
result.set(BufferUtils.toUint8(value), offset);
}
offset += arr.byteLength;
offset += value.byteLength;
}

return result;
}
};

0 comments on commit cd326e2

Please sign in to comment.