vomebook commited on
Commit ·
1da97a5
1
Parent(s): e90b86d
Account for retained pixels once per image prefetch trim
Browse files
static/reader/pdf/reader-pdf-scheduler.mjs
CHANGED
|
@@ -63,12 +63,22 @@ export function createReaderPdfScheduler(readerContext) {
|
|
| 63 |
}
|
| 64 |
|
| 65 |
function trimPdfManifestPrefetches() {
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
}
|
| 73 |
}
|
| 74 |
|
|
|
|
| 63 |
}
|
| 64 |
|
| 65 |
function trimPdfManifestPrefetches() {
|
| 66 |
+
const records = readerContext.pdfManifestPrefetches;
|
| 67 |
+
if (!records.size) return;
|
| 68 |
+
let pixels = null, budget = null;
|
| 69 |
+
for (const record of records.values()) {
|
| 70 |
+
if (readerContext.pdfManifestPrefetchActive.has(record)) continue;
|
| 71 |
+
// The count limit can free slots before any pixel scan. Evicting a
|
| 72 |
+
// detached image then changes only its own contribution to the total.
|
| 73 |
+
if (records.size <= readerContext.PDF_MANIFEST_PREFETCH_LIMIT) {
|
| 74 |
+
pixels ??= readerContext.pdfRetainedPixels();
|
| 75 |
+
budget ??= readerContext.pdfPixelBudget();
|
| 76 |
+
if (pixels <= budget) return;
|
| 77 |
+
}
|
| 78 |
+
if (pixels !== null) pixels -= readerContext.pdfSurfacePixels(record.image);
|
| 79 |
+
records.delete(record.url);
|
| 80 |
+
record.image.onload = record.image.onerror = null;
|
| 81 |
+
record.image.removeAttribute("src");
|
| 82 |
}
|
| 83 |
}
|
| 84 |
|
tests/reader/images/test_reader_image_memory.mjs
CHANGED
|
@@ -11,6 +11,52 @@ globalThis.window = {devicePixelRatio: 1};
|
|
| 11 |
Object.defineProperty(globalThis, 'navigator', {configurable: true, value: {connection: {saveData: true}}});
|
| 12 |
globalThis.matchMedia = () => ({matches: false});
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
// An anonymous preview holder cannot be inspected or preempted as a page shell.
|
| 15 |
{
|
| 16 |
const abort = new AbortController();
|
|
|
|
| 11 |
Object.defineProperty(globalThis, 'navigator', {configurable: true, value: {connection: {saveData: true}}});
|
| 12 |
globalThis.matchMedia = () => ({matches: false});
|
| 13 |
|
| 14 |
+
// Batch trims preserve oldest-inactive eviction and do not rescan mounted
|
| 15 |
+
// canvases for each detached image. An active-only pool cannot be trimmed.
|
| 16 |
+
{
|
| 17 |
+
let scans = 0;
|
| 18 |
+
const removed = [];
|
| 19 |
+
const reader = {
|
| 20 |
+
content: {querySelectorAll() { scans++; return [{tagName:'CANVAS', width:20, height:10}]; }},
|
| 21 |
+
PDF_MANIFEST_PREFETCH_LIMIT: 5,
|
| 22 |
+
pdfManifestPrefetches: new Map(), pdfManifestPrefetchActive: new Set(),
|
| 23 |
+
pdfPixelBudget: () => 450
|
| 24 |
+
};
|
| 25 |
+
Object.assign(reader, createReaderPdfScheduler(reader));
|
| 26 |
+
reader.pdfPixelBudget = () => 450;
|
| 27 |
+
const add = (url, width, active = false) => {
|
| 28 |
+
const image = {tagName:'IMG', naturalWidth:width, naturalHeight:1, onload() {}, onerror() {},
|
| 29 |
+
removeAttribute(name) {
|
| 30 |
+
assert.equal(name,'src');assert.equal(this.onload,null);assert.equal(this.onerror,null);
|
| 31 |
+
assert.equal(reader.pdfManifestPrefetches.has(url),false);
|
| 32 |
+
removed.push(url);
|
| 33 |
+
}};
|
| 34 |
+
const record = {url,image};
|
| 35 |
+
reader.pdfManifestPrefetches.set(url,record);
|
| 36 |
+
if (active) reader.pdfManifestPrefetchActive.add(record);
|
| 37 |
+
};
|
| 38 |
+
reader.trimPdfManifestPrefetches();
|
| 39 |
+
assert.equal(scans,0,'empty prefetch pool needs no DOM accounting');
|
| 40 |
+
add('active',240,true);
|
| 41 |
+
add('count',1);
|
| 42 |
+
add('zero',0);
|
| 43 |
+
add('first',35);
|
| 44 |
+
add('second',60);
|
| 45 |
+
add('reusable',10);
|
| 46 |
+
reader.trimPdfManifestPrefetches();
|
| 47 |
+
assert.deepEqual(removed,['count','zero','first','second']);
|
| 48 |
+
assert.deepEqual([...reader.pdfManifestPrefetches.keys()],['active','reusable']);
|
| 49 |
+
assert.equal(scans,1);
|
| 50 |
+
assert.equal(reader.pdfRetainedPixels(),450);
|
| 51 |
+
reader.pdfPixelBudget = () => 100;
|
| 52 |
+
reader.trimPdfManifestPrefetches();
|
| 53 |
+
assert.equal(removed.at(-1),'reusable');
|
| 54 |
+
assert.deepEqual([...reader.pdfManifestPrefetches.keys()],['active']);
|
| 55 |
+
scans = 0;
|
| 56 |
+
reader.trimPdfManifestPrefetches();
|
| 57 |
+
assert.equal(scans,0,'active images must not be evicted even above budget');
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
// An anonymous preview holder cannot be inspected or preempted as a page shell.
|
| 61 |
{
|
| 62 |
const abort = new AbortController();
|