Merge pull request #11076 from Snuffleupagus/XRef-fetch-isRef/cache

Replace the `XRef.cache` Array with a Map instead
This commit is contained in:
Tim van der Meij 2019-08-18 14:44:31 +02:00 committed by GitHub
commit 852fc955bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1043,8 +1043,7 @@ var XRef = (function XRefClosure() {
this.pdfManager = pdfManager; this.pdfManager = pdfManager;
this.entries = []; this.entries = [];
this.xrefstms = Object.create(null); this.xrefstms = Object.create(null);
// prepare the XRef cache this._cacheMap = new Map(); // Prepare the XRef cache.
this.cache = [];
this.stats = { this.stats = {
streamTypes: Object.create(null), streamTypes: Object.create(null),
fontTypes: Object.create(null), fontTypes: Object.create(null),
@ -1626,19 +1625,20 @@ var XRef = (function XRefClosure() {
}, },
fetchIfRef: function XRef_fetchIfRef(obj, suppressEncryption) { fetchIfRef: function XRef_fetchIfRef(obj, suppressEncryption) {
if (!isRef(obj)) { if (obj instanceof Ref) {
return obj; return this.fetch(obj, suppressEncryption);
} }
return this.fetch(obj, suppressEncryption); return obj;
}, },
fetch: function XRef_fetch(ref, suppressEncryption) { fetch: function XRef_fetch(ref, suppressEncryption) {
if (!isRef(ref)) { if (!(ref instanceof Ref)) {
throw new Error('ref object is not a reference'); throw new Error('ref object is not a reference');
} }
var num = ref.num; const num = ref.num;
if (num in this.cache) {
var cacheEntry = this.cache[num]; if (this._cacheMap.has(num)) {
const cacheEntry = this._cacheMap.get(num);
// In documents with Object Streams, it's possible that cached `Dict`s // In documents with Object Streams, it's possible that cached `Dict`s
// have not been assigned an `objId` yet (see e.g. issue3115r.pdf). // have not been assigned an `objId` yet (see e.g. issue3115r.pdf).
if (cacheEntry instanceof Dict && !cacheEntry.objId) { if (cacheEntry instanceof Dict && !cacheEntry.objId) {
@ -1646,12 +1646,11 @@ var XRef = (function XRefClosure() {
} }
return cacheEntry; return cacheEntry;
} }
let xrefEntry = this.getEntry(num);
var xrefEntry = this.getEntry(num); if (xrefEntry === null) { // The referenced entry can be free.
this._cacheMap.set(num, xrefEntry);
// the referenced entry can be free return xrefEntry;
if (xrefEntry === null) {
return (this.cache[num] = null);
} }
if (xrefEntry.uncompressed) { if (xrefEntry.uncompressed) {
@ -1709,7 +1708,7 @@ var XRef = (function XRefClosure() {
xrefEntry = parser.getObj(); xrefEntry = parser.getObj();
} }
if (!isStream(xrefEntry)) { if (!isStream(xrefEntry)) {
this.cache[num] = xrefEntry; this._cacheMap.set(num, xrefEntry);
} }
return xrefEntry; return xrefEntry;
}, },
@ -1757,7 +1756,7 @@ var XRef = (function XRefClosure() {
num = nums[i]; num = nums[i];
var entry = this.entries[num]; var entry = this.entries[num];
if (entry && entry.offset === tableOffset && entry.gen === i) { if (entry && entry.offset === tableOffset && entry.gen === i) {
this.cache[num] = entries[i]; this._cacheMap.set(num, entries[i]);
} }
} }
xrefEntry = entries[xrefEntry.gen]; xrefEntry = entries[xrefEntry.gen];
@ -1768,10 +1767,10 @@ var XRef = (function XRefClosure() {
}, },
async fetchIfRefAsync(obj, suppressEncryption) { async fetchIfRefAsync(obj, suppressEncryption) {
if (!isRef(obj)) { if (obj instanceof Ref) {
return obj; return this.fetchAsync(obj, suppressEncryption);
} }
return this.fetchAsync(obj, suppressEncryption); return obj;
}, },
async fetchAsync(ref, suppressEncryption) { async fetchAsync(ref, suppressEncryption) {