From 4170c414faf3014636114b7cf4454809c2e6baf2 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 2 Mar 2019 11:36:30 +0100 Subject: [PATCH] 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`. --- src/core/worker.js | 33 ++++++++++++++++++++------------- src/display/api.js | 4 ++-- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/core/worker.js b/src/core/worker.js index 8827c1099..f0eba694f 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -14,10 +14,10 @@ */ import { - arrayByteLength, arraysToBytes, assert, createPromiseCapability, info, - InvalidPDFException, MissingPDFException, PasswordException, - setVerbosityLevel, UnexpectedResponseException, UnknownErrorException, - UNSUPPORTED_FEATURES, warn + arrayByteLength, arraysToBytes, assert, createPromiseCapability, + getVerbosityLevel, info, InvalidPDFException, MissingPDFException, + PasswordException, setVerbosityLevel, UnexpectedResponseException, + UnknownErrorException, UNSUPPORTED_FEATURES, VerbosityLevel, warn } from '../shared/util'; import { LocalPdfManager, NetworkPdfManager } from './pdf_manager'; import isNodeJS from '../shared/is_node'; @@ -222,6 +222,7 @@ var WorkerMessageHandler = { var terminated = false; var cancelXHRs = null; var WorkerTasks = []; + const verbosity = getVerbosityLevel(); let apiVersion = docParams.apiVersion; let workerVersion = @@ -578,8 +579,9 @@ var WorkerMessageHandler = { var task = new WorkerTask('RenderPageRequest: page ' + pageIndex); startWorkerTask(task); - var pageNum = pageIndex + 1; - var start = Date.now(); + // NOTE: Keep this condition in sync with the `info` helper function. + const start = (verbosity >= VerbosityLevel.INFOS ? Date.now() : 0); + // Pre compile the pdf page and fetch the fonts/images. page.getOperatorList({ handler, @@ -589,8 +591,10 @@ var WorkerMessageHandler = { }).then(function(operatorList) { finishWorkerTask(task); - info('page=' + pageNum + ' - getOperatorList: time=' + - (Date.now() - start) + 'ms, len=' + operatorList.totalLength); + if (start) { + info(`page=${pageIndex + 1} - getOperatorList: time=` + + `${Date.now() - start}ms, len=${operatorList.totalLength}`); + } }, function(e) { finishWorkerTask(task); if (task.terminated) { @@ -626,7 +630,7 @@ var WorkerMessageHandler = { } handler.send('PageError', { - pageNum, + pageIndex, error: wrappedException, intent: data.intent, }); @@ -643,8 +647,9 @@ var WorkerMessageHandler = { var task = new WorkerTask('GetTextContent: page ' + pageIndex); startWorkerTask(task); - var pageNum = pageIndex + 1; - var start = Date.now(); + // NOTE: Keep this condition in sync with the `info` helper function. + const start = (verbosity >= VerbosityLevel.INFOS ? Date.now() : 0); + page.extractTextContent({ handler, task, @@ -654,8 +659,10 @@ var WorkerMessageHandler = { }).then(function() { finishWorkerTask(task); - info('text indexing: page=' + pageNum + ' - time=' + - (Date.now() - start) + 'ms'); + if (start) { + info(`page=${pageIndex + 1} - getTextContent: time=` + + `${Date.now() - start}ms`); + } sink.close(); }, function (reason) { finishWorkerTask(task); diff --git a/src/display/api.js b/src/display/api.js index 8041183ec..dfe7fec7b 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -2028,11 +2028,11 @@ class WorkerTransport { 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]; if (intentState.displayReadyCapability) { - intentState.displayReadyCapability.reject(data.error); + intentState.displayReadyCapability.reject(new Error(data.error)); } else { throw new Error(data.error); }