Optimized function.js / 2x Faster PDFFunction_constructPostScriptFromIR
This commit is contained in:
parent
0d8e3cc8ca
commit
95fda4fcdc
@ -385,66 +385,57 @@ var PDFFunction = (function PDFFunctionClosure() {
|
|||||||
var domain = IR[1];
|
var domain = IR[1];
|
||||||
var range = IR[2];
|
var range = IR[2];
|
||||||
var code = IR[3];
|
var code = IR[3];
|
||||||
var numOutputs = range.length / 2;
|
var numOutputs = range.length >> 1;
|
||||||
|
var numInputs = domain.length >> 1;
|
||||||
var evaluator = new PostScriptEvaluator(code);
|
var evaluator = new PostScriptEvaluator(code);
|
||||||
// Cache the values for a big speed up, the cache size is limited though
|
// Cache the values for a big speed up, the cache size is limited though
|
||||||
// since the number of possible values can be huge from a PS function.
|
// since the number of possible values can be huge from a PS function.
|
||||||
var cache = new FunctionCache();
|
var cache = {};
|
||||||
|
// The MAX_CACHE_SIZE is set to ~4x the maximum number of distinct values
|
||||||
|
// seen in our tests.
|
||||||
|
var MAX_CACHE_SIZE = 2048 * 4;
|
||||||
|
var cache_available = MAX_CACHE_SIZE;
|
||||||
return function constructPostScriptFromIRResult(args) {
|
return function constructPostScriptFromIRResult(args) {
|
||||||
var initialStack = [];
|
var i, value;
|
||||||
for (var i = 0, ii = (domain.length / 2); i < ii; ++i) {
|
var key = '';
|
||||||
initialStack.push(args[i]);
|
var input = new Array(numInputs);
|
||||||
|
for (i = 0; i < numInputs; i++) {
|
||||||
|
value = args[i];
|
||||||
|
input[i] = value;
|
||||||
|
key += value + '_';
|
||||||
}
|
}
|
||||||
|
|
||||||
var key = initialStack.join('_');
|
var cachedValue = cache[key];
|
||||||
if (cache.has(key)) {
|
if (cachedValue !== undefined) {
|
||||||
return cache.get(key);
|
return cachedValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var stack = evaluator.execute(initialStack);
|
var output = new Array(numOutputs);
|
||||||
var transformed = [];
|
var stack = evaluator.execute(input);
|
||||||
for (i = numOutputs - 1; i >= 0; --i) {
|
var stackIndex = stack.length - numOutputs;
|
||||||
var out = stack.pop();
|
for (i = 0; i < numOutputs; i++) {
|
||||||
var rangeIndex = 2 * i;
|
value = stack[stackIndex + i];
|
||||||
if (out < range[rangeIndex]) {
|
var bound = range[i * 2];
|
||||||
out = range[rangeIndex];
|
if (value < bound) {
|
||||||
} else if (out > range[rangeIndex + 1]) {
|
value = bound;
|
||||||
out = range[rangeIndex + 1];
|
} else {
|
||||||
|
bound = range[i * 2 +1];
|
||||||
|
if (value > bound) {
|
||||||
|
value = bound;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
transformed[i] = out;
|
output[i] = value;
|
||||||
}
|
}
|
||||||
cache.set(key, transformed);
|
if (cache_available > 0) {
|
||||||
return transformed;
|
cache_available--;
|
||||||
|
cache[key] = output;
|
||||||
|
}
|
||||||
|
return output;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
||||||
var FunctionCache = (function FunctionCacheClosure() {
|
|
||||||
// Of 10 PDF's with type4 functions the maxium number of distinct values seen
|
|
||||||
// was 256. This still may need some tweaking in the future though.
|
|
||||||
var MAX_CACHE_SIZE = 1024;
|
|
||||||
function FunctionCache() {
|
|
||||||
this.cache = {};
|
|
||||||
this.total = 0;
|
|
||||||
}
|
|
||||||
FunctionCache.prototype = {
|
|
||||||
has: function FunctionCache_has(key) {
|
|
||||||
return key in this.cache;
|
|
||||||
},
|
|
||||||
get: function FunctionCache_get(key) {
|
|
||||||
return this.cache[key];
|
|
||||||
},
|
|
||||||
set: function FunctionCache_set(key, value) {
|
|
||||||
if (this.total < MAX_CACHE_SIZE) {
|
|
||||||
this.cache[key] = value;
|
|
||||||
this.total++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return FunctionCache;
|
|
||||||
})();
|
|
||||||
|
|
||||||
var PostScriptStack = (function PostScriptStackClosure() {
|
var PostScriptStack = (function PostScriptStackClosure() {
|
||||||
var MAX_STACK_SIZE = 100;
|
var MAX_STACK_SIZE = 100;
|
||||||
function PostScriptStack(initialStack) {
|
function PostScriptStack(initialStack) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user