Track previous "XRefStm"s in a Set, rather than an Object

Having just reviewed a patch touching this code, I couldn't help noticing that an `Object` isn't really the optimal data-structure for this and nowadays we can do better by using a `Set` instead.
This commit is contained in:
Jonas Jenwald 2023-03-22 09:38:00 +01:00
parent 9321758d91
commit 122d5e549a

View File

@ -37,7 +37,7 @@ class XRef {
this.stream = stream; this.stream = stream;
this.pdfManager = pdfManager; this.pdfManager = pdfManager;
this.entries = []; this.entries = [];
this.xrefstms = Object.create(null); this._xrefStms = new Set();
this._cacheMap = new Map(); // Prepare the XRef cache. this._cacheMap = new Map(); // Prepare the XRef cache.
this._pendingRefs = new RefSet(); this._pendingRefs = new RefSet();
this._newPersistentRefNum = null; this._newPersistentRefNum = null;
@ -540,7 +540,7 @@ class XRef {
const xrefTagOffset = skipUntil(content, 0, xrefBytes); const xrefTagOffset = skipUntil(content, 0, xrefBytes);
if (xrefTagOffset < contentLength && content[xrefTagOffset + 5] < 64) { if (xrefTagOffset < contentLength && content[xrefTagOffset + 5] < 64) {
xrefStms.push(position - stream.start); xrefStms.push(position - stream.start);
this.xrefstms[position - stream.start] = 1; // Avoid recursion this._xrefStms.add(position - stream.start); // Avoid recursion
} }
position += contentLength; position += contentLength;
@ -700,14 +700,11 @@ class XRef {
// Recursively get other XRefs 'XRefStm', if any // Recursively get other XRefs 'XRefStm', if any
obj = dict.get("XRefStm"); obj = dict.get("XRefStm");
if (Number.isInteger(obj)) { if (Number.isInteger(obj) && !this._xrefStms.has(obj)) {
const pos = obj;
// ignore previously loaded xref streams // ignore previously loaded xref streams
// (possible infinite recursion) // (possible infinite recursion)
if (!(pos in this.xrefstms)) { this._xrefStms.add(obj);
this.xrefstms[pos] = 1; this.startXRefQueue.push(obj);
this.startXRefQueue.push(pos);
}
} }
} else if (Number.isInteger(obj)) { } else if (Number.isInteger(obj)) {
// Parse in-stream XRef // Parse in-stream XRef
@ -757,8 +754,7 @@ class XRef {
} }
get lastXRefStreamPos() { get lastXRefStreamPos() {
const pos = Object.keys(this.xrefstms); return this._xrefStms.size > 0 ? Math.max(...this._xrefStms) : null;
return pos.length === 0 ? null : Math.max(...pos);
} }
getEntry(i) { getEntry(i) {