Merge pull request #20352 from calixteman/improve_struct_tree_parser

Improve performance of the struct tree build (bug 1987914)
This commit is contained in:
calixteman 2025-10-10 13:27:11 +02:00 committed by GitHub
commit c8d8f9fbb4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -35,6 +35,31 @@ class StructTreeRoot {
this.ref = rootRef instanceof Ref ? rootRef : null; this.ref = rootRef instanceof Ref ? rootRef : null;
this.roleMap = new Map(); this.roleMap = new Map();
this.structParentIds = null; this.structParentIds = null;
this.kidRefToPosition = undefined;
}
getKidPosition(kidRef) {
if (this.kidRefToPosition === undefined) {
const obj = this.dict.get("K");
if (Array.isArray(obj)) {
const map = (this.kidRefToPosition = new Map());
for (let i = 0, ii = obj.length; i < ii; i++) {
const ref = obj[i];
if (ref) {
map.set(ref.toString(), i);
}
}
} else if (obj instanceof Dict) {
this.kidRefToPosition = new Map([[obj.objId, 0]]);
} else if (!obj) {
this.kidRefToPosition = new Map();
} else {
this.kidRefToPosition = null;
}
}
return this.kidRefToPosition
? (this.kidRefToPosition.get(kidRef) ?? NaN)
: -1;
} }
init() { init() {
@ -785,33 +810,16 @@ class StructTreePage {
} }
addTopLevelNode(dict, element) { addTopLevelNode(dict, element) {
const obj = this.rootDict.get("K"); const index = this.root.getKidPosition(dict.objId);
if (!obj) { if (isNaN(index)) {
return false; return false;
} }
if (index !== -1) {
if (obj instanceof Dict) { this.nodes[index] = element;
if (obj.objId !== dict.objId) {
return false;
} }
this.nodes[0] = element;
return true; return true;
} }
if (!Array.isArray(obj)) {
return true;
}
let save = false;
for (let i = 0; i < obj.length; i++) {
const kidRef = obj[i];
if (kidRef?.toString() === dict.objId) {
this.nodes[i] = element;
save = true;
}
}
return save;
}
/** /**
* Convert the tree structure into a simplified object literal that can * Convert the tree structure into a simplified object literal that can
* be sent to the main thread. * be sent to the main thread.