From 5f295ba280aecaaeb4408fe1b15e685e5035eb45 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 6 Dec 2021 11:27:47 +0100 Subject: [PATCH] Improve caching in `Catalog.getPageDict` (PR 8207 follow-up) PR 8207 added caching to improve the performance of `Catalog.getPageDict`, by not having to repeatedly fetch the same data and also reducing the asynchronicity of that method. However, because of annoying off-by-one errors[1] the caching became less efficient than it could/should be.[2] Note here that the /Pages-tree is zero-indexed, and that e.g. `pageIndex = 5` thus correspond to the *sixth* page of the document. --- [1] In particular the `currentPageIndex + count < pageIndex` part. [2] For example, even when loading a relatively small/simple document such as `tracemonkey.pdf` in the viewer, the number of `xref.fetchAsync(currentNode)` calls are reduced from `56` to `44` with this patch. --- src/core/catalog.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/catalog.js b/src/core/catalog.js index 71b3a2af8..5a6296557 100644 --- a/src/core/catalog.js +++ b/src/core/catalog.js @@ -1102,7 +1102,7 @@ class Catalog { if (isRef(currentNode)) { const count = pageKidsCountCache.get(currentNode); // Skip nodes where the page can't be. - if (count > 0 && currentPageIndex + count < pageIndex) { + if (count >= 0 && currentPageIndex + count <= pageIndex) { currentPageIndex += count; continue; }