From ab5f4a3e5e614b8a8c831b657b2f58c08ada29f1 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 3 Nov 2021 09:28:52 +0100 Subject: [PATCH] Avoid doing unnecessary checks, when pre-rendering page layouts with "holes" (PR 14131 follow-up) *Sometimes I'll hopefully learn to optimize my code directly when writing it, rather than having to do multiple clean-up passes; sorry about the churn here!* For most page layouts there won't be any "holes" in the visible pages (or thumbnails), and in those cases it'd obviously be preferable not having to repeat any checks of already rendered pages. Rather than only checking the "distance" between the first/last pages, we can instead compare the theoretical number of pages (between first/last) with the actually visible number of pages instead. This way, we're able to better detect the "holes"-case and can skip unnecessary parsing in the common case. --- web/pdf_rendering_queue.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/web/pdf_rendering_queue.js b/web/pdf_rendering_queue.js index 06f7016de..1f80b5809 100644 --- a/web/pdf_rendering_queue.js +++ b/web/pdf_rendering_queue.js @@ -115,13 +115,13 @@ class PDFRenderingQueue { * 2. if last scrolled down, the page after the visible pages, or * if last scrolled up, the page before the visible pages */ - const visibleViews = visible.views; + const visibleViews = visible.views, + numVisible = visibleViews.length; - const numVisible = visibleViews.length; if (numVisible === 0) { return null; } - for (let i = 0; i < numVisible; ++i) { + for (let i = 0; i < numVisible; i++) { const view = visibleViews[i].view; if (!this.isViewFinished(view)) { return view; @@ -132,7 +132,7 @@ class PDFRenderingQueue { // All the visible views have rendered; try to handle any "holes" in the // page layout (can happen e.g. with spreadModes at higher zoom levels). - if (lastId - firstId > 1) { + if (lastId - firstId + 1 > numVisible) { for (let i = 1, ii = lastId - firstId; i < ii; i++) { const holeId = scrolledDown ? firstId + i : lastId - i, holeView = views[holeId - 1];