Merge pull request #10608 from Snuffleupagus/worker-less-Date-usage

Reduce usage of `Date.now()` in `src/core/worker.js`
This commit is contained in:
Tim van der Meij 2019-03-06 00:40:58 +01:00 committed by GitHub
commit e9661edda7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 28 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

@ -16,15 +16,14 @@
/* eslint no-var: error */ /* eslint no-var: error */
import { import {
assert, createPromiseCapability, deprecated, getVerbosityLevel, info, assert, createPromiseCapability, getVerbosityLevel, info, InvalidPDFException,
InvalidPDFException, isArrayBuffer, isSameOrigin, MissingPDFException, isArrayBuffer, isSameOrigin, MissingPDFException, NativeImageDecoding,
NativeImageDecoding, PasswordException, setVerbosityLevel, shadow, PasswordException, setVerbosityLevel, shadow, stringToBytes,
stringToBytes, UnexpectedResponseException, UnknownErrorException, UnexpectedResponseException, UnknownErrorException, unreachable, URL, warn
unreachable, URL, warn
} from '../shared/util'; } from '../shared/util';
import { import {
DOMCanvasFactory, DOMCMapReaderFactory, DummyStatTimer, loadScript, deprecated, DOMCanvasFactory, DOMCMapReaderFactory, DummyStatTimer,
PageViewport, RenderingCancelledException, StatTimer loadScript, PageViewport, RenderingCancelledException, StatTimer
} from './display_utils'; } from './display_utils';
import { FontFaceObject, FontLoader } from './font_loader'; import { FontFaceObject, FontLoader } from './font_loader';
import { apiCompatibilityParams } from './api_compatibility'; import { apiCompatibilityParams } from './api_compatibility';
@ -2028,11 +2027,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);
} }

View File

@ -477,6 +477,11 @@ function loadScript(src) {
}); });
} }
// Deprecated API function -- display regardless of the `verbosity` setting.
function deprecated(details) {
console.log('Deprecated API usage: ' + details);
}
export { export {
PageViewport, PageViewport,
RenderingCancelledException, RenderingCancelledException,
@ -492,4 +497,5 @@ export {
isFetchSupported, isFetchSupported,
isValidFetchUrl, isValidFetchUrl,
loadScript, loadScript,
deprecated,
}; };

View File

@ -307,11 +307,6 @@ function warn(msg) {
} }
} }
// Deprecated API function -- display regardless of the `verbosity` setting.
function deprecated(details) {
console.log('Deprecated API usage: ' + details);
}
function unreachable(msg) { function unreachable(msg) {
throw new Error(msg); throw new Error(msg);
} }
@ -929,7 +924,6 @@ export {
bytesToString, bytesToString,
createPromiseCapability, createPromiseCapability,
createObjectURL, createObjectURL,
deprecated,
getVerbosityLevel, getVerbosityLevel,
info, info,
isArrayBuffer, isArrayBuffer,