Minimize memory usage of font-related arrays.

This patch replaces some vanilla arrays with typed arrays, and avoids
some array copying.

It reduces the peak RSS when viewing
http://www.dynacw.co.jp/Portals/3/fontsamplepdf/sample_4942546800828.pdf
from ~940 MiB to ~750 MiB, and reduces its load time from 83 to 76 ms.
This commit is contained in:
Nicholas Nethercote 2014-07-21 22:19:31 -07:00
parent 584fef90ab
commit c7f02d2c8e
2 changed files with 35 additions and 13 deletions

View File

@ -2433,17 +2433,43 @@ var Font = (function FontClosure() {
// length
var length = data.length;
// Per spec tables must be 4-bytes align so add padding as needed
while (data.length & 3) {
data.push(0x00);
// Per spec tables must be 4-bytes align so add padding as needed.
var paddedLength = length;
while (paddedLength & 3) {
paddedLength++;
}
var i;
var padding = paddedLength - length;
if (padding !== 0) {
// Padding is required. |data| can be an Array, Uint8Array, or
// Uint16Array. In the latter two cases we need to create slightly larger
// typed arrays and copy the old contents in. Fortunately that's not a
// common case.
var data2;
if (data instanceof Array) {
for (i = 0; i < padding; i++) {
data.push(0);
}
} else if (data instanceof Uint8Array) {
data2 = new Uint8Array(paddedLength);
data2.set(data);
data = data2;
} else if (data instanceof Uint16Array) {
data2 = new Uint16Array(paddedLength);
data2.set(data);
data = data2;
} else {
error('bad array kind in createTableEntry');
}
}
while (file.virtualOffset & 3) {
file.virtualOffset++;
}
// checksum
var checksum = 0, n = data.length;
for (var i = 0; i < n; i += 4) {
for (i = 0; i < n; i += 4) {
checksum = (checksum + int32(data[i], data[i + 1], data[i + 2],
data[i + 3])) | 0;
}
@ -2452,6 +2478,8 @@ var Font = (function FontClosure() {
string32(offset) + string32(length));
file.file += tableEntry;
file.virtualOffset += data.length;
return data;
}
function isTrueTypeFile(file) {
@ -4066,13 +4094,7 @@ var Font = (function FontClosure() {
// rewrite the tables but tweak offsets
for (i = 0; i < numTables; i++) {
table = tables[tablesNames[i]];
var data = [];
tableData = table.data;
for (var j = 0, jj = tableData.length; j < jj; j++) {
data.push(tableData[j]);
}
createTableEntry(ttf, table.tag, data);
table.data = createTableEntry(ttf, table.tag, table.data);
}
// Add the table datas
@ -4254,7 +4276,7 @@ var Font = (function FontClosure() {
var field;
for (field in fields) {
createTableEntry(otf, field, fields[field]);
fields[field] = createTableEntry(otf, field, fields[field]);
}
for (field in fields) {
var table = fields[field];

View File

@ -436,7 +436,7 @@ function bytesToString(bytes) {
function stringToArray(str) {
var length = str.length;
var array = [];
var array = new Uint16Array(length);
for (var i = 0; i < length; ++i) {
array[i] = str.charCodeAt(i);
}