diff --git a/src/core/evaluator.js b/src/core/evaluator.js index df604292b..017e5ae8a 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -609,6 +609,9 @@ class PartialEvaluator { .then(imageObj => { imgData = imageObj.createImageData(/* forceRGBA = */ false); + if (cacheKey && imageRef && cacheGlobally) { + this.globalImageCache.addByteSize(imageRef, imgData.data.length); + } return this._sendImgData(objId, imgData, cacheGlobally); }) .catch(reason => { @@ -633,6 +636,7 @@ class PartialEvaluator { objId, fn: OPS.paintImageXObject, args, + byteSize: 0, // Temporary entry, note `addByteSize` above. }); } } diff --git a/src/core/image_utils.js b/src/core/image_utils.js index 20878e621..5625f5b08 100644 --- a/src/core/image_utils.js +++ b/src/core/image_utils.js @@ -13,7 +13,7 @@ * limitations under the License. */ -import { assert, info, shadow, unreachable } from "../shared/util.js"; +import { assert, shadow, unreachable, warn } from "../shared/util.js"; import { RefSetCache } from "./primitives.js"; class BaseLocalCache { @@ -161,8 +161,12 @@ class GlobalImageCache { return shadow(this, "NUM_PAGES_THRESHOLD", 2); } - static get MAX_IMAGES_TO_CACHE() { - return shadow(this, "MAX_IMAGES_TO_CACHE", 10); + static get MIN_IMAGES_TO_CACHE() { + return shadow(this, "MIN_IMAGES_TO_CACHE", 10); + } + + static get MAX_BYTE_SIZE() { + return shadow(this, "MAX_BYTE_SIZE", /* Forty megabytes = */ 40e6); } constructor() { @@ -179,6 +183,24 @@ class GlobalImageCache { this._imageCache = new RefSetCache(); } + get _byteSize() { + let byteSize = 0; + this._imageCache.forEach(imageData => { + byteSize += imageData.byteSize; + }); + return byteSize; + } + + get _cacheLimitReached() { + if (this._imageCache.size < GlobalImageCache.MIN_IMAGES_TO_CACHE) { + return false; + } + if (this._byteSize < GlobalImageCache.MAX_BYTE_SIZE) { + return false; + } + return true; + } + shouldCache(ref, pageIndex) { const pageIndexSet = this._refCache.get(ref); const numPages = pageIndexSet @@ -188,10 +210,7 @@ class GlobalImageCache { if (numPages < GlobalImageCache.NUM_PAGES_THRESHOLD) { return false; } - if ( - !this._imageCache.has(ref) && - this._imageCache.size >= GlobalImageCache.MAX_IMAGES_TO_CACHE - ) { + if (!this._imageCache.has(ref) && this._cacheLimitReached) { return false; } return true; @@ -206,6 +225,20 @@ class GlobalImageCache { pageIndexSet.add(pageIndex); } + /** + * PLEASE NOTE: Must be called *after* the `setData` method. + */ + addByteSize(ref, byteSize) { + const imageData = this._imageCache.get(ref); + if (!imageData) { + return; // The image data isn't cached (the limit was reached). + } + if (imageData.byteSize) { + return; // The byte-size has already been set. + } + imageData.byteSize = byteSize; + } + getData(ref, pageIndex) { const pageIndexSet = this._refCache.get(ref); if (!pageIndexSet) { @@ -232,10 +265,8 @@ class GlobalImageCache { if (this._imageCache.has(ref)) { return; } - if (this._imageCache.size >= GlobalImageCache.MAX_IMAGES_TO_CACHE) { - info( - "GlobalImageCache.setData - ignoring image above MAX_IMAGES_TO_CACHE." - ); + if (this._cacheLimitReached) { + warn("GlobalImageCache.setData - cache limit reached."); return; } this._imageCache.put(ref, data);