Skip to content

Commit

Permalink
Detect the chunk size the browser supports.
Browse files Browse the repository at this point in the history
Instead of using a hard-coded chunk size when creating large strings,
this now uses a browser check to detect it.  This allows us to use
large chunk sizes on browsers that support it but still support low-end
devices.

Closes #1985
Closes #1994

Change-Id: Ibe45902b659516ae66bd7da33007fd15e7f64207
  • Loading branch information
TheModMaker committed Jun 14, 2019
1 parent 98b8d55 commit 7bbca07
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions lib/util/string_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

goog.provide('shaka.util.StringUtils');

goog.require('goog.asserts');
goog.require('shaka.log');
goog.require('shaka.util.Error');

Expand Down Expand Up @@ -217,13 +218,48 @@ shaka.util.StringUtils = class {
* @return {string}
*/
static fromCharCode(array) {
const max = 16000;
let ret = '';
for (let i = 0; i < array.length; i += max) {
const subArray = array.subarray(i, i + max);
ret += String.fromCharCode(...subArray);
// Check the browser for what chunk sizes it supports. Cache the result
// in an impl method to avoid checking several times.
if (!shaka.util.StringUtils.fromCharCodeImpl_) {
const supportsChunkSize = (size) => {
try {
const buffer = new Uint8Array(size);
// The compiler will complain about suspicious value if this isn't
// stored in a variable and used.
const foo = String.fromCharCode(...buffer);
goog.asserts.assert(foo, 'Should get value');
return true;
} catch (error) {
return false;
}
};

// Different browsers support different chunk sizes; find out the largest
// this browser supports so we can use larger chunks on supported browsers
// but still support lower-end devices that require small chunks.
// 64k is supported on all major desktop browsers.
for (let size = 64 * 1024; size > 0; size /= 2) {
if (supportsChunkSize(size)) {
shaka.util.StringUtils.fromCharCodeImpl_ = (buffer) => {
let ret = '';
for (let i = 0; i < buffer.length; i += size) {
const subArray = buffer.subarray(i, i + size);
ret += String.fromCharCode(...subArray);
}
return ret;
};
break;
}
}
}

return ret;
goog.asserts.assert(
shaka.util.StringUtils.fromCharCodeImpl_,
'Unable to create a fromCharCode method');
return shaka.util.StringUtils.fromCharCodeImpl_(array);
}
};


/** @private {?function(!TypedArray):string} */
shaka.util.StringUtils.fromCharCodeImpl_ = null;

0 comments on commit 7bbca07

Please sign in to comment.