mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
Add caching to reduce the number of Ref
objects
This is similar to the existing caching used to reduced the number of `Cmd` and `Name` objects. With the `tracemonkey.pdf` file, this patch changes the number of `Ref` objects as follows (in the default viewer): | | Loading the first page | Loading *all* the pages | |----------|------------------------|-------------------------| | `master` | 332 | 3265 | | `patch` | 163 | 996 |
This commit is contained in:
parent
d95145953c
commit
2fe9f3ff8f
9 changed files with 103 additions and 88 deletions
|
@ -627,7 +627,7 @@ class PDFDocument {
|
|||
const { catalog, linearization, } = this;
|
||||
assert(linearization && linearization.pageFirst === pageIndex);
|
||||
|
||||
const ref = new Ref(linearization.objectNumberFirst, 0);
|
||||
const ref = Ref.get(linearization.objectNumberFirst, 0);
|
||||
return this.xref.fetchAsync(ref).then((obj) => {
|
||||
// Ensure that the object that was found is actually a Page dictionary.
|
||||
if (isDict(obj, 'Page') ||
|
||||
|
|
|
@ -1697,7 +1697,7 @@ var XRef = (function XRefClosure() {
|
|||
|
||||
fetchCompressed(ref, xrefEntry, suppressEncryption = false) {
|
||||
var tableOffset = xrefEntry.offset;
|
||||
var stream = this.fetch(new Ref(tableOffset, 0));
|
||||
var stream = this.fetch(Ref.get(tableOffset, 0));
|
||||
if (!isStream(stream)) {
|
||||
throw new FormatError('bad ObjStm stream');
|
||||
}
|
||||
|
|
|
@ -150,7 +150,7 @@ class Parser {
|
|||
if (Number.isInteger(buf1)) { // indirect reference or integer
|
||||
const num = buf1;
|
||||
if (Number.isInteger(this.buf1) && isCmd(this.buf2, 'R')) {
|
||||
const ref = new Ref(num, this.buf1);
|
||||
const ref = Ref.get(num, this.buf1);
|
||||
this.shift();
|
||||
this.shift();
|
||||
return ref;
|
||||
|
|
|
@ -14,6 +14,8 @@
|
|||
*/
|
||||
/* uses XRef */
|
||||
|
||||
import { assert } from '../shared/util';
|
||||
|
||||
var EOF = {};
|
||||
|
||||
var Name = (function NameClosure() {
|
||||
|
@ -176,6 +178,8 @@ var Dict = (function DictClosure() {
|
|||
})();
|
||||
|
||||
var Ref = (function RefClosure() {
|
||||
const refCache = Object.create(null);
|
||||
|
||||
function Ref(num, gen) {
|
||||
this.num = num;
|
||||
this.gen = gen;
|
||||
|
@ -185,13 +189,19 @@ var Ref = (function RefClosure() {
|
|||
toString: function Ref_toString() {
|
||||
// This function is hot, so we make the string as compact as possible.
|
||||
// |this.gen| is almost always zero, so we treat that case specially.
|
||||
if (this.gen !== 0) {
|
||||
return `${this.num}R${this.gen}`;
|
||||
if (this.gen === 0) {
|
||||
return `${this.num}R`;
|
||||
}
|
||||
return `${this.num}R`;
|
||||
return `${this.num}R${this.gen}`;
|
||||
},
|
||||
};
|
||||
|
||||
Ref.get = function(num, gen) {
|
||||
const key = (gen === 0 ? `${num}R` : `${num}R${gen}`);
|
||||
const refValue = refCache[key];
|
||||
return (refValue ? refValue : (refCache[key] = new Ref(num, gen)));
|
||||
};
|
||||
|
||||
return Ref;
|
||||
})();
|
||||
|
||||
|
@ -277,6 +287,11 @@ function isRef(v) {
|
|||
}
|
||||
|
||||
function isRefsEqual(v1, v2) {
|
||||
if (typeof PDFJSDev === 'undefined' ||
|
||||
PDFJSDev.test('!PRODUCTION || TESTING')) {
|
||||
assert(v1 instanceof Ref && v2 instanceof Ref,
|
||||
'isRefsEqual: Both parameters should be `Ref`s.');
|
||||
}
|
||||
return v1.num === v2.num && v1.gen === v2.gen;
|
||||
}
|
||||
|
||||
|
|
|
@ -494,7 +494,7 @@ var WorkerMessageHandler = {
|
|||
});
|
||||
|
||||
handler.on('GetPageIndex', function wphSetupGetPageIndex(data) {
|
||||
var ref = new Ref(data.ref.num, data.ref.gen);
|
||||
var ref = Ref.get(data.ref.num, data.ref.gen);
|
||||
var catalog = pdfManager.pdfDocument.catalog;
|
||||
return catalog.getPageIndex(ref);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue