Make fnArray always be a plain array.

This makes the code much simpler, and the extra memory use is tiny -- a vanilla
1000 element array is only 4000 bytes larger than a Uint32Array of the same
size.
This commit is contained in:
Nicholas Nethercote 2014-03-15 06:04:13 -07:00
parent 42d175efd4
commit 292b96a0bf

View File

@ -1297,17 +1297,10 @@ var OperatorList = (function OperatorListClosure() {
function OperatorList(intent, messageHandler, pageIndex) { function OperatorList(intent, messageHandler, pageIndex) {
this.messageHandler = messageHandler; this.messageHandler = messageHandler;
// When there isn't a message handler the fn array needs to be able to grow this.fnArray = [];
// since we can't flush the operators.
if (messageHandler) {
this.fnArray = new Uint8Array(CHUNK_SIZE);
} else {
this.fnArray = [];
}
this.argsArray = []; this.argsArray = [];
this.dependencies = {}; this.dependencies = {};
this.pageIndex = pageIndex; this.pageIndex = pageIndex;
this.fnIndex = 0;
this.intent = intent; this.intent = intent;
} }
@ -1318,19 +1311,16 @@ var OperatorList = (function OperatorListClosure() {
}, },
addOp: function(fn, args) { addOp: function(fn, args) {
this.fnArray.push(fn);
this.argsArray.push(args);
if (this.messageHandler) { if (this.messageHandler) {
this.fnArray[this.fnIndex++] = fn; if (this.fnArray.length >= CHUNK_SIZE) {
this.argsArray.push(args);
if (this.fnIndex >= CHUNK_SIZE) {
this.flush(); this.flush();
} else if (this.fnIndex >= CHUNK_SIZE_ABOUT && } else if (this.fnArray.length >= CHUNK_SIZE_ABOUT &&
(fn === OPS.restore || fn === OPS.endText)) { (fn === OPS.restore || fn === OPS.endText)) {
// heuristic to flush on boundary of restore or endText // heuristic to flush on boundary of restore or endText
this.flush(); this.flush();
} }
} else {
this.fnArray.push(fn);
this.argsArray.push(args);
} }
}, },
@ -1376,9 +1366,9 @@ var OperatorList = (function OperatorListClosure() {
pageIndex: this.pageIndex, pageIndex: this.pageIndex,
intent: this.intent intent: this.intent
}, null, transfers); }, null, transfers);
this.dependencies = []; this.dependencies = {};
this.fnIndex = 0; this.fnArray.length = 0;
this.argsArray = []; this.argsArray.length = 0;
} }
}; };
@ -1670,22 +1660,6 @@ var EvaluatorPreprocessor = (function EvaluatorPreprocessor() {
})(); })();
var QueueOptimizer = (function QueueOptimizerClosure() { var QueueOptimizer = (function QueueOptimizerClosure() {
function squash(array, index, howMany, element) {
if (isArray(array)) {
array.splice(index, howMany, element);
} else if (typeof element !== 'undefined') {
// Replace the element.
array[index] = element;
// Shift everything after the element up.
var sub = array.subarray(index + howMany);
array.set(sub, index + 1);
} else {
// Shift everything after the element up.
var sub = array.subarray(index + howMany);
array.set(sub, index);
}
}
function addState(parentState, pattern, fn) { function addState(parentState, pattern, fn) {
var state = parentState; var state = parentState;
for (var i = 0, ii = pattern.length - 1; i < ii; i++) { for (var i = 0, ii = pattern.length - 1; i < ii; i++) {
@ -1709,7 +1683,7 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
var fnArray = context.fnArray, argsArray = context.argsArray; var fnArray = context.fnArray, argsArray = context.argsArray;
var j = context.currentOperation - 3, i = j + 4; var j = context.currentOperation - 3, i = j + 4;
var ii = context.operationsLength; var ii = fnArray.length;
for (; i < ii && fnArray[i - 4] === fnArray[i]; i++) { for (; i < ii && fnArray[i - 4] === fnArray[i]; i++) {
} }
@ -1774,12 +1748,11 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
} }
} }
// replacing queue items // replacing queue items
squash(fnArray, j, count * 4, OPS.paintInlineImageXObjectGroup); fnArray.splice(j, count * 4, OPS.paintInlineImageXObjectGroup);
argsArray.splice(j, count * 4, argsArray.splice(j, count * 4,
[{width: imgWidth, height: imgHeight, kind: ImageKind.RGBA_32BPP, [{width: imgWidth, height: imgHeight, kind: ImageKind.RGBA_32BPP,
data: imgData}, map]); data: imgData}, map]);
context.currentOperation = j; context.currentOperation = j;
context.operationsLength -= count * 4 - 1;
}); });
addState(InitialState, addState(InitialState,
@ -1793,7 +1766,7 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
var fnArray = context.fnArray, argsArray = context.argsArray; var fnArray = context.fnArray, argsArray = context.argsArray;
var j = context.currentOperation - 3, i = j + 4; var j = context.currentOperation - 3, i = j + 4;
var ii = context.operationsLength; var ii = fnArray.length;
for (; i < ii && fnArray[i - 4] === fnArray[i]; i++) { for (; i < ii && fnArray[i - 4] === fnArray[i]; i++) {
} }
@ -1837,12 +1810,11 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
} }
// replacing queue items // replacing queue items
squash(fnArray, j, count * 4, OPS.paintImageMaskXObjectRepeat); fnArray.splice(j, count * 4, OPS.paintImageMaskXObjectRepeat);
argsArray.splice(j, count * 4, [argsArray[j + 2][0], argsArray.splice(j, count * 4, [argsArray[j + 2][0],
argsArray[j + 1][0], argsArray[j + 1][3], positions]); argsArray[j + 1][0], argsArray[j + 1][3], positions]);
context.currentOperation = j; context.currentOperation = j;
context.operationsLength -= count * 4 - 1;
} else { } else {
count = Math.min(count, MAX_IMAGES_IN_MASKS_BLOCK); count = Math.min(count, MAX_IMAGES_IN_MASKS_BLOCK);
var images = []; var images = [];
@ -1854,11 +1826,10 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
} }
// replacing queue items // replacing queue items
squash(fnArray, j, count * 4, OPS.paintImageMaskXObjectGroup); fnArray.splice(j, count * 4, OPS.paintImageMaskXObjectGroup);
argsArray.splice(j, count * 4, [images]); argsArray.splice(j, count * 4, [images]);
context.currentOperation = j; context.currentOperation = j;
context.operationsLength -= count * 4 - 1;
} }
}); });
@ -1873,7 +1844,7 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
if (argsArray[j + 1][1] !== 0 || argsArray[j + 1][2] !== 0) { if (argsArray[j + 1][1] !== 0 || argsArray[j + 1][2] !== 0) {
return; return;
} }
var ii = context.operationsLength; var ii = fnArray.length;
for (; i + 3 < ii && fnArray[i - 4] === fnArray[i]; i += 4) { for (; i + 3 < ii && fnArray[i - 4] === fnArray[i]; i += 4) {
if (fnArray[i - 3] !== fnArray[i + 1] || if (fnArray[i - 3] !== fnArray[i + 1] ||
fnArray[i - 2] !== fnArray[i + 2] || fnArray[i - 2] !== fnArray[i + 2] ||
@ -1909,11 +1880,10 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
var args = [argsArray[j + 2][0], argsArray[j + 1][0], var args = [argsArray[j + 2][0], argsArray[j + 1][0],
argsArray[j + 1][3], positions]; argsArray[j + 1][3], positions];
// replacing queue items // replacing queue items
squash(fnArray, j, count * 4, OPS.paintImageXObjectRepeat); fnArray.splice(j, count * 4, OPS.paintImageXObjectRepeat);
argsArray.splice(j, count * 4, args); argsArray.splice(j, count * 4, args);
context.currentOperation = j; context.currentOperation = j;
context.operationsLength -= count * 4 - 1;
}); });
addState(InitialState, addState(InitialState,
@ -1926,7 +1896,7 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
var fnArray = context.fnArray, argsArray = context.argsArray; var fnArray = context.fnArray, argsArray = context.argsArray;
var j = context.currentOperation - 4, i = j + 5; var j = context.currentOperation - 4, i = j + 5;
var ii = context.operationsLength; var ii = fnArray.length;
for (; i < ii && fnArray[i - 5] === fnArray[i]; i++) { for (; i < ii && fnArray[i - 5] === fnArray[i]; i++) {
if (fnArray[i] === OPS.setFont) { if (fnArray[i] === OPS.setFont) {
@ -1962,12 +1932,10 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
k += 5; k += 5;
} }
var removed = (count - 1) * 3; var removed = (count - 1) * 3;
squash(fnArray, i, removed); fnArray.splice(i, removed);
argsArray.splice(i, removed); argsArray.splice(i, removed);
context.currentOperation = i; context.currentOperation = i;
context.operationsLength -= removed;
}); });
function QueueOptimizer() { function QueueOptimizer() {
@ -1977,7 +1945,6 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
var fnArray = queue.fnArray, argsArray = queue.argsArray; var fnArray = queue.fnArray, argsArray = queue.argsArray;
var context = { var context = {
currentOperation: 0, currentOperation: 0,
operationsLength: argsArray.length,
fnArray: fnArray, fnArray: fnArray,
argsArray: argsArray argsArray: argsArray
}; };
@ -1989,7 +1956,7 @@ var QueueOptimizer = (function QueueOptimizerClosure() {
context.currentOperation = i; context.currentOperation = i;
state = state(context); state = state(context);
i = context.currentOperation; i = context.currentOperation;
ii = context.operationsLength; ii = context.fnArray.length;
} }
} }
} }