Reduce usage of Date.now() in src/core/worker.js

Currently for every single parsed/rendered page there's no less than *four* `Date.now()` calls being made on the worker-side. This seems totally unnecessary, since the result of these calls are, by default, not used for anything *unless* the verbosity level is set to `INFO`.
This commit is contained in:
Jonas Jenwald 2019-03-02 11:36:30 +01:00
parent c43396c2b7
commit 4170c414fa
2 changed files with 22 additions and 15 deletions

View File

@ -14,10 +14,10 @@
*/ */
import { import {
arrayByteLength, arraysToBytes, assert, createPromiseCapability, info, arrayByteLength, arraysToBytes, assert, createPromiseCapability,
InvalidPDFException, MissingPDFException, PasswordException, getVerbosityLevel, info, InvalidPDFException, MissingPDFException,
setVerbosityLevel, UnexpectedResponseException, UnknownErrorException, PasswordException, setVerbosityLevel, UnexpectedResponseException,
UNSUPPORTED_FEATURES, warn UnknownErrorException, UNSUPPORTED_FEATURES, VerbosityLevel, warn
} from '../shared/util'; } from '../shared/util';
import { LocalPdfManager, NetworkPdfManager } from './pdf_manager'; import { LocalPdfManager, NetworkPdfManager } from './pdf_manager';
import isNodeJS from '../shared/is_node'; import isNodeJS from '../shared/is_node';
@ -222,6 +222,7 @@ var WorkerMessageHandler = {
var terminated = false; var terminated = false;
var cancelXHRs = null; var cancelXHRs = null;
var WorkerTasks = []; var WorkerTasks = [];
const verbosity = getVerbosityLevel();
let apiVersion = docParams.apiVersion; let apiVersion = docParams.apiVersion;
let workerVersion = let workerVersion =
@ -578,8 +579,9 @@ var WorkerMessageHandler = {
var task = new WorkerTask('RenderPageRequest: page ' + pageIndex); var task = new WorkerTask('RenderPageRequest: page ' + pageIndex);
startWorkerTask(task); startWorkerTask(task);
var pageNum = pageIndex + 1; // NOTE: Keep this condition in sync with the `info` helper function.
var start = Date.now(); const start = (verbosity >= VerbosityLevel.INFOS ? Date.now() : 0);
// Pre compile the pdf page and fetch the fonts/images. // Pre compile the pdf page and fetch the fonts/images.
page.getOperatorList({ page.getOperatorList({
handler, handler,
@ -589,8 +591,10 @@ var WorkerMessageHandler = {
}).then(function(operatorList) { }).then(function(operatorList) {
finishWorkerTask(task); finishWorkerTask(task);
info('page=' + pageNum + ' - getOperatorList: time=' + if (start) {
(Date.now() - start) + 'ms, len=' + operatorList.totalLength); info(`page=${pageIndex + 1} - getOperatorList: time=` +
`${Date.now() - start}ms, len=${operatorList.totalLength}`);
}
}, function(e) { }, function(e) {
finishWorkerTask(task); finishWorkerTask(task);
if (task.terminated) { if (task.terminated) {
@ -626,7 +630,7 @@ var WorkerMessageHandler = {
} }
handler.send('PageError', { handler.send('PageError', {
pageNum, pageIndex,
error: wrappedException, error: wrappedException,
intent: data.intent, intent: data.intent,
}); });
@ -643,8 +647,9 @@ var WorkerMessageHandler = {
var task = new WorkerTask('GetTextContent: page ' + pageIndex); var task = new WorkerTask('GetTextContent: page ' + pageIndex);
startWorkerTask(task); startWorkerTask(task);
var pageNum = pageIndex + 1; // NOTE: Keep this condition in sync with the `info` helper function.
var start = Date.now(); const start = (verbosity >= VerbosityLevel.INFOS ? Date.now() : 0);
page.extractTextContent({ page.extractTextContent({
handler, handler,
task, task,
@ -654,8 +659,10 @@ var WorkerMessageHandler = {
}).then(function() { }).then(function() {
finishWorkerTask(task); finishWorkerTask(task);
info('text indexing: page=' + pageNum + ' - time=' + if (start) {
(Date.now() - start) + 'ms'); info(`page=${pageIndex + 1} - getTextContent: time=` +
`${Date.now() - start}ms`);
}
sink.close(); sink.close();
}, function (reason) { }, function (reason) {
finishWorkerTask(task); finishWorkerTask(task);

View File

@ -2028,11 +2028,11 @@ class WorkerTransport {
return; // Ignore any pending requests if the worker was terminated. return; // Ignore any pending requests if the worker was terminated.
} }
const page = this.pageCache[data.pageNum - 1]; const page = this.pageCache[data.pageIndex];
const intentState = page.intentStates[data.intent]; const intentState = page.intentStates[data.intent];
if (intentState.displayReadyCapability) { if (intentState.displayReadyCapability) {
intentState.displayReadyCapability.reject(data.error); intentState.displayReadyCapability.reject(new Error(data.error));
} else { } else {
throw new Error(data.error); throw new Error(data.error);
} }