Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the number of function calls in the Dict class #11169

Merged
merged 1 commit into from
Sep 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 25 additions & 37 deletions src/core/primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,59 +81,47 @@ var Dict = (function DictClosure() {
},

// automatically dereferences Ref objects
get: function Dict_get(key1, key2, key3) {
var value;
var xref = this.xref, suppressEncryption = this.suppressEncryption;
if (typeof (value = this._map[key1]) !== 'undefined' ||
key1 in this._map || typeof key2 === 'undefined') {
return xref ? xref.fetchIfRef(value, suppressEncryption) : value;
get(key1, key2, key3) {
let value = this._map[key1];
if (value === undefined && !(key1 in this._map) && key2 !== undefined) {
value = this._map[key2];
if (value === undefined && !(key2 in this._map) && key3 !== undefined) {
value = this._map[key3];
}
}
if (typeof (value = this._map[key2]) !== 'undefined' ||
key2 in this._map || typeof key3 === 'undefined') {
return xref ? xref.fetchIfRef(value, suppressEncryption) : value;
if (value instanceof Ref && this.xref) {
timvandermeij marked this conversation as resolved.
Show resolved Hide resolved
return this.xref.fetch(value, this.suppressEncryption);
}
value = this._map[key3];
return xref ? xref.fetchIfRef(value, suppressEncryption) : value;
return value;
},

// Same as get(), but returns a promise and uses fetchIfRefAsync().
getAsync: function Dict_getAsync(key1, key2, key3) {
var value;
var xref = this.xref, suppressEncryption = this.suppressEncryption;
if (typeof (value = this._map[key1]) !== 'undefined' ||
key1 in this._map || typeof key2 === 'undefined') {
if (xref) {
return xref.fetchIfRefAsync(value, suppressEncryption);
async getAsync(key1, key2, key3) {
let value = this._map[key1];
if (value === undefined && !(key1 in this._map) && key2 !== undefined) {
value = this._map[key2];
if (value === undefined && !(key2 in this._map) && key3 !== undefined) {
value = this._map[key3];
}
return Promise.resolve(value);
}
if (typeof (value = this._map[key2]) !== 'undefined' ||
key2 in this._map || typeof key3 === 'undefined') {
if (xref) {
return xref.fetchIfRefAsync(value, suppressEncryption);
}
return Promise.resolve(value);
if (value instanceof Ref && this.xref) {
return this.xref.fetchAsync(value, this.suppressEncryption);
}
value = this._map[key3];
if (xref) {
return xref.fetchIfRefAsync(value, suppressEncryption);
}
return Promise.resolve(value);
return value;
},

// Same as get(), but dereferences all elements if the result is an Array.
getArray: function Dict_getArray(key1, key2, key3) {
var value = this.get(key1, key2, key3);
var xref = this.xref, suppressEncryption = this.suppressEncryption;
if (!Array.isArray(value) || !xref) {
getArray(key1, key2, key3) {
let value = this.get(key1, key2, key3);
if (!Array.isArray(value) || !this.xref) {
return value;
}
value = value.slice(); // Ensure that we don't modify the Dict data.
for (var i = 0, ii = value.length; i < ii; i++) {
if (!isRef(value[i])) {
for (let i = 0, ii = value.length; i < ii; i++) {
if (!(value[i] instanceof Ref)) {
continue;
}
value[i] = xref.fetch(value[i], suppressEncryption);
value[i] = this.xref.fetch(value[i], this.suppressEncryption);
}
return value;
},
Expand Down